Datum spg_text_picksplit(PG_FUNCTION_ARGS) { spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0); spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1); text *text0 = DatumGetTextPP(in->datums[0]); int i, commonLen; spgNodePtr *nodes; /* Identify longest common prefix, if any */ commonLen = VARSIZE_ANY_EXHDR(text0); for (i = 1; i < in->nTuples && commonLen > 0; i++) { text *texti = DatumGetTextPP(in->datums[i]); int tmp = commonPrefix(VARDATA_ANY(text0), VARDATA_ANY(texti), VARSIZE_ANY_EXHDR(text0), VARSIZE_ANY_EXHDR(texti)); if (tmp < commonLen) commonLen = tmp; } /* * Limit the prefix length, if necessary, to ensure that the resulting * inner tuple will fit on a page. */ commonLen = Min(commonLen, SPGIST_MAX_PREFIX_LENGTH); /* Set node prefix to be that string, if it's not empty */ if (commonLen == 0) { out->hasPrefix = false; } else { out->hasPrefix = true; out->prefixDatum = formTextDatum(VARDATA_ANY(text0), commonLen); } /* Extract the node label (first non-common byte) from each value */ nodes = (spgNodePtr *) palloc(sizeof(spgNodePtr) * in->nTuples); for (i = 0; i < in->nTuples; i++) { text *texti = DatumGetTextPP(in->datums[i]); if (commonLen < VARSIZE_ANY_EXHDR(texti)) nodes[i].c = *(uint8 *) (VARDATA_ANY(texti) + commonLen); else nodes[i].c = '\0'; /* use \0 if string is all common */ nodes[i].i = i; nodes[i].d = in->datums[i]; } /* * Sort by label bytes so that we can group the values into nodes. This * also ensures that the nodes are ordered by label value, allowing the * use of binary search in searchChar. */ qsort(nodes, in->nTuples, sizeof(*nodes), cmpNodePtr); /* And emit results */ out->nNodes = 0; out->nodeLabels = (Datum *) palloc(sizeof(Datum) * in->nTuples); out->mapTuplesToNodes = (int *) palloc(sizeof(int) * in->nTuples); out->leafTupleDatums = (Datum *) palloc(sizeof(Datum) * in->nTuples); for (i = 0; i < in->nTuples; i++) { text *texti = DatumGetTextPP(nodes[i].d); Datum leafD; if (i == 0 || nodes[i].c != nodes[i - 1].c) { out->nodeLabels[out->nNodes] = UInt8GetDatum(nodes[i].c); out->nNodes++; } if (commonLen < VARSIZE_ANY_EXHDR(texti)) leafD = formTextDatum(VARDATA_ANY(texti) + commonLen + 1, VARSIZE_ANY_EXHDR(texti) - commonLen - 1); else leafD = formTextDatum(NULL, 0); out->leafTupleDatums[nodes[i].i] = leafD; out->mapTuplesToNodes[nodes[i].i] = out->nNodes - 1; } PG_RETURN_VOID(); }
Datum heap_page_items(PG_FUNCTION_ARGS) { bytea *raw_page = PG_GETARG_BYTEA_P(0); heap_page_items_state *inter_call_data = NULL; FuncCallContext *fctx; int raw_page_size; if (!superuser()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("must be superuser to use raw page functions")))); raw_page_size = VARSIZE(raw_page) - VARHDRSZ; if (SRF_IS_FIRSTCALL()) { TupleDesc tupdesc; MemoryContext mctx; if (raw_page_size < SizeOfPageHeaderData) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("input page too small (%d bytes)", raw_page_size))); fctx = SRF_FIRSTCALL_INIT(); mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx); inter_call_data = palloc(sizeof(heap_page_items_state)); /* Build a tuple descriptor for our result type */ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) elog(ERROR, "return type must be a row type"); inter_call_data->tupd = tupdesc; inter_call_data->offset = FirstOffsetNumber; inter_call_data->page = VARDATA(raw_page); fctx->max_calls = PageGetMaxOffsetNumber(inter_call_data->page); fctx->user_fctx = inter_call_data; MemoryContextSwitchTo(mctx); } fctx = SRF_PERCALL_SETUP(); inter_call_data = fctx->user_fctx; if (fctx->call_cntr < fctx->max_calls) { Page page = inter_call_data->page; HeapTuple resultTuple; Datum result; ItemId id; Datum values[13]; bool nulls[13]; uint16 lp_offset; uint16 lp_flags; uint16 lp_len; memset(nulls, 0, sizeof(nulls)); /* Extract information from the line pointer */ id = PageGetItemId(page, inter_call_data->offset); lp_offset = ItemIdGetOffset(id); lp_flags = ItemIdGetFlags(id); lp_len = ItemIdGetLength(id); values[0] = UInt16GetDatum(inter_call_data->offset); values[1] = UInt16GetDatum(lp_offset); values[2] = UInt16GetDatum(lp_flags); values[3] = UInt16GetDatum(lp_len); /* * We do just enough validity checking to make sure we don't reference * data outside the page passed to us. The page could be corrupt in * many other ways, but at least we won't crash. */ if (ItemIdHasStorage(id) && lp_len >= sizeof(HeapTupleHeader) && lp_offset == MAXALIGN(lp_offset) && lp_offset + lp_len <= raw_page_size) { HeapTupleHeader tuphdr; int bits_len; /* Extract information from the tuple header */ tuphdr = (HeapTupleHeader) PageGetItem(page, id); values[4] = UInt32GetDatum(HeapTupleHeaderGetXmin(tuphdr)); values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr)); values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr)); /* shared with xvac */ values[7] = PointerGetDatum(&tuphdr->t_ctid); values[8] = UInt32GetDatum(tuphdr->t_infomask2); values[9] = UInt32GetDatum(tuphdr->t_infomask); values[10] = UInt8GetDatum(tuphdr->t_hoff); /* * We already checked that the item as is completely within the * raw page passed to us, with the length given in the line * pointer.. Let's check that t_hoff doesn't point over lp_len, * before using it to access t_bits and oid. */ if (tuphdr->t_hoff >= sizeof(HeapTupleHeader) && tuphdr->t_hoff <= lp_len) { if (tuphdr->t_infomask & HEAP_HASNULL) { bits_len = tuphdr->t_hoff - (((char *) tuphdr->t_bits) -((char *) tuphdr)); values[11] = CStringGetTextDatum( bits_to_text(tuphdr->t_bits, bits_len * 8)); } else nulls[11] = true; if (tuphdr->t_infomask & HEAP_HASOID) values[12] = HeapTupleHeaderGetOid(tuphdr); else nulls[12] = true; } else { nulls[11] = true; nulls[12] = true; } } else { /* * The line pointer is not used, or it's invalid. Set the rest of * the fields to NULL */ int i; for (i = 4; i <= 12; i++) nulls[i] = true; } /* Build and return the result tuple. */ resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls); result = HeapTupleGetDatum(resultTuple); inter_call_data->offset++; SRF_RETURN_NEXT(fctx, result); } else SRF_RETURN_DONE(fctx); }
Datum spg_text_choose(PG_FUNCTION_ARGS) { spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0); spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1); text *inText = DatumGetTextPP(in->datum); char *inStr = VARDATA_ANY(inText); int inSize = VARSIZE_ANY_EXHDR(inText); uint8 nodeChar = '\0'; int i = 0; int commonLen = 0; /* Check for prefix match, set nodeChar to first byte after prefix */ if (in->hasPrefix) { text *prefixText = DatumGetTextPP(in->prefixDatum); char *prefixStr = VARDATA_ANY(prefixText); int prefixSize = VARSIZE_ANY_EXHDR(prefixText); commonLen = commonPrefix(inStr + in->level, prefixStr, inSize - in->level, prefixSize); if (commonLen == prefixSize) { if (inSize - in->level > commonLen) nodeChar = *(uint8 *) (inStr + in->level + commonLen); else nodeChar = '\0'; } else { /* Must split tuple because incoming value doesn't match prefix */ out->resultType = spgSplitTuple; if (commonLen == 0) { out->result.splitTuple.prefixHasPrefix = false; } else { out->result.splitTuple.prefixHasPrefix = true; out->result.splitTuple.prefixPrefixDatum = formTextDatum(prefixStr, commonLen); } out->result.splitTuple.nodeLabel = UInt8GetDatum(*(prefixStr + commonLen)); if (prefixSize - commonLen == 1) { out->result.splitTuple.postfixHasPrefix = false; } else { out->result.splitTuple.postfixHasPrefix = true; out->result.splitTuple.postfixPrefixDatum = formTextDatum(prefixStr + commonLen + 1, prefixSize - commonLen - 1); } PG_RETURN_VOID(); } } else if (inSize > in->level) { nodeChar = *(uint8 *) (inStr + in->level); } else { nodeChar = '\0'; } /* Look up nodeChar in the node label array */ if (searchChar(in->nodeLabels, in->nNodes, nodeChar, &i)) { /* * Descend to existing node. (If in->allTheSame, the core code will * ignore our nodeN specification here, but that's OK. We still * have to provide the correct levelAdd and restDatum values, and * those are the same regardless of which node gets chosen by core.) */ out->resultType = spgMatchNode; out->result.matchNode.nodeN = i; out->result.matchNode.levelAdd = commonLen + 1; if (inSize - in->level - commonLen - 1 > 0) out->result.matchNode.restDatum = formTextDatum(inStr + in->level + commonLen + 1, inSize - in->level - commonLen - 1); else out->result.matchNode.restDatum = formTextDatum(NULL, 0); } else if (in->allTheSame) { /* * Can't use AddNode action, so split the tuple. The upper tuple * has the same prefix as before and uses an empty node label for * the lower tuple. The lower tuple has no prefix and the same * node labels as the original tuple. */ out->resultType = spgSplitTuple; out->result.splitTuple.prefixHasPrefix = in->hasPrefix; out->result.splitTuple.prefixPrefixDatum = in->prefixDatum; out->result.splitTuple.nodeLabel = UInt8GetDatum('\0'); out->result.splitTuple.postfixHasPrefix = false; } else { /* Add a node for the not-previously-seen nodeChar value */ out->resultType = spgAddNode; out->result.addNode.nodeLabel = UInt8GetDatum(nodeChar); out->result.addNode.nodeN = i; } PG_RETURN_VOID(); }