/* * CopyShardInterval copies fields from the specified source ShardInterval * into the fields of the provided destination ShardInterval. */ void CopyShardInterval(ShardInterval *srcInterval, ShardInterval *destInterval) { destInterval->type = srcInterval->type; destInterval->relationId = srcInterval->relationId; destInterval->storageType = srcInterval->storageType; destInterval->valueTypeId = srcInterval->valueTypeId; destInterval->valueTypeLen = srcInterval->valueTypeLen; destInterval->valueByVal = srcInterval->valueByVal; destInterval->minValueExists = srcInterval->minValueExists; destInterval->maxValueExists = srcInterval->maxValueExists; destInterval->shardId = srcInterval->shardId; destInterval->minValue = 0; if (destInterval->minValueExists) { destInterval->minValue = datumCopy(srcInterval->minValue, srcInterval->valueByVal, srcInterval->valueTypeLen); } destInterval->maxValue = 0; if (destInterval->maxValueExists) { destInterval->maxValue = datumCopy(srcInterval->maxValue, srcInterval->valueByVal, srcInterval->valueTypeLen); } }
void setHstoreFromDatum(lua_State *L, Datum datum) { Lua_Hstore * strg; BEGINLUA; strg = (Lua_Hstore *)lua_newuserdata(L, sizeof(Lua_Hstore)); MTOLUA(L); datum = datumCopy(datum, hs_type.byval, hs_type.len); MTOPG; strg->hstore = DatumGetHStoreP(datum); strg->datum = datum; strg->issync = 1; strg->havetodel = 1; lua_pushlightuserdata(L, strg); lua_newtable(L); lua_settable(L, LUA_REGISTRYINDEX); luaP_getfield(L, hstore_type_name); lua_setmetatable(L, -2); ENDLUAV(1); }
/* * Copy a ParamListInfo structure. * * The result is allocated in CurrentMemoryContext. */ ParamListInfo copyParamList(ParamListInfo from) { ParamListInfo retval; Size size; int i; if (from == NULL || from->numParams <= 0) return NULL; /* sizeof(ParamListInfoData) includes the first array element */ size = sizeof(ParamListInfoData) + (from->numParams - 1) *sizeof(ParamExternData); retval = (ParamListInfo) palloc(size); memcpy(retval, from, size); /* * Flat-copy is not good enough for pass-by-ref data values, so make a * pass over the array to copy those. */ for (i = 0; i < retval->numParams; i++) { ParamExternData *prm = &retval->params[i]; int16 typLen; bool typByVal; if (prm->isnull || !OidIsValid(prm->ptype)) continue; get_typlenbyval(prm->ptype, &typLen, &typByVal); prm->value = datumCopy(prm->value, typByVal, typLen); } return retval; }
/*------------------------------------------------------------------------- * datumTransfer * * Transfer a non-NULL datum into the current memory context. * * This is equivalent to datumCopy() except when the datum is a read-write * pointer to an expanded object. In that case we merely reparent the object * into the current context, and return its standard R/W pointer (in case the * given one is a transient pointer of shorter lifespan). *------------------------------------------------------------------------- */ Datum datumTransfer(Datum value, bool typByVal, int typLen) { if (!typByVal && typLen == -1 && VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(value))) value = TransferExpandedObject(value, CurrentMemoryContext); else value = datumCopy(value, typByVal, typLen); return value; }
/* * This is basically the same as datumCopy(), but extended to count * palloc'd space in accum->allocatedMemory. */ static Datum getDatumCopy(BuildAccumulator *accum, OffsetNumber attnum, Datum value) { Form_pg_attribute att = accum->ginstate->origTupdesc->attrs[attnum - 1]; Datum res; if (att->attbyval) res = value; else { res = datumCopy(value, false, att->attlen); accum->allocatedMemory += GetMemoryChunkSpace(DatumGetPointer(res)); } return res; }
Datum anyold_transfn(PG_FUNCTION_ARGS) { Oid type; Datum state; MemoryContext aggcontext, oldcontext; int16 typlen; bool typbyval; char typalign; if (!AggCheckCallContext(fcinfo, &aggcontext)) { /* cannot be called directly because of internal-type argument */ elog(ERROR, "anyold_transfn called in non-aggregate context"); } if (PG_ARGISNULL(0)) { if (PG_ARGISNULL(1)) PG_RETURN_NULL(); /* First non-null value --- initialize */ oldcontext = MemoryContextSwitchTo(aggcontext); type = get_fn_expr_argtype(fcinfo->flinfo, 1); if (type == InvalidOid) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("could not determine input data type"))); get_typlenbyvalalign(type, &typlen, &typbyval, &typalign); /* Copy initial value */ if (typlen == -1) state = PointerGetDatum(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1))); else state = datumCopy(PG_GETARG_DATUM(1), typbyval, typlen); MemoryContextSwitchTo(oldcontext); } else { state = PG_GETARG_DATUM(0); } PG_RETURN_DATUM(state); }
/* * Get datum representations of the attoptions field in pg_attribute_encoding * for the given relation. */ Datum * get_rel_attoptions(Oid relid, AttrNumber max_attno) { Form_pg_attribute attform; HeapTuple tuple; cqContext cqc; cqContext *pcqCtx; Datum *dats; Relation pgae = heap_open(AttributeEncodingRelationId, AccessShareLock); /* used for attbyval and len below */ attform = pgae->rd_att->attrs[Anum_pg_attribute_encoding_attoptions - 1]; dats = palloc0(max_attno * sizeof(Datum)); pcqCtx = caql_beginscan( caql_addrel(cqclr(&cqc), pgae), cql("SELECT * FROM pg_attribute_encoding " " WHERE attrelid = :1 ", ObjectIdGetDatum(relid))); while (HeapTupleIsValid(tuple = caql_getnext(pcqCtx))) { Form_pg_attribute_encoding a = (Form_pg_attribute_encoding)GETSTRUCT(tuple); int16 attnum = a->attnum; Datum attoptions; bool isnull; Insist(attnum > 0 && attnum <= max_attno); attoptions = heap_getattr(tuple, Anum_pg_attribute_encoding_attoptions, RelationGetDescr(pgae), &isnull); Insist(!isnull); dats[attnum - 1] = datumCopy(attoptions, attform->attbyval, attform->attlen); } caql_endscan(pcqCtx); heap_close(pgae, AccessShareLock); return dats; }
static void argm_copy_datum(bool is_null, Datum src, ArgmDatumWithType *dest, bool free) { if (free && !dest->typbyval && !dest->is_null) pfree(DatumGetPointer(dest->value)); if (is_null) dest->is_null = true; else { dest->is_null = false; if (dest->typlen == -1) dest->value = PointerGetDatum(PG_DETOAST_DATUM_COPY(src)); else dest->value = datumCopy(src, dest->typbyval, dest->typlen); } }
/* * Copy a ParamListInfo structure. * * The result is allocated in CurrentMemoryContext. * * Note: the intent of this function is to make a static, self-contained * set of parameter values. If dynamic parameter hooks are present, we * intentionally do not copy them into the result. Rather, we forcibly * instantiate all available parameter values and copy the datum values. */ ParamListInfo copyParamList(ParamListInfo from) { ParamListInfo retval; Size size; int i; if (from == NULL || from->numParams <= 0) return NULL; /* sizeof(ParamListInfoData) includes the first array element */ size = sizeof(ParamListInfoData) + (from->numParams - 1) * sizeof(ParamExternData); retval = (ParamListInfo) palloc(size); retval->paramFetch = NULL; retval->paramFetchArg = NULL; retval->parserSetup = NULL; retval->parserSetupArg = NULL; retval->numParams = from->numParams; for (i = 0; i < from->numParams; i++) { ParamExternData *oprm = &from->params[i]; ParamExternData *nprm = &retval->params[i]; int16 typLen; bool typByVal; /* give hook a chance in case parameter is dynamic */ if (!OidIsValid(oprm->ptype) && from->paramFetch != NULL) (*from->paramFetch) (from, i + 1); /* flat-copy the parameter info */ *nprm = *oprm; /* need datumCopy in case it's a pass-by-reference datatype */ if (nprm->isnull || !OidIsValid(nprm->ptype)) continue; get_typlenbyval(nprm->ptype, &typLen, &typByVal); nprm->value = datumCopy(nprm->value, typByVal, typLen); } return retval; }
Datum jsonb_numeric(PG_FUNCTION_ARGS) { Jsonb *j = PG_GETARG_JSONB(0); if (JB_ROOT_IS_SCALAR(j)) { JsonbValue *jv; jv = getIthJsonbValueFromContainer(&j->root, 0); if (jv->type == jbvNumeric) PG_RETURN_DATUM(datumCopy(NumericGetDatum(jv->val.numeric), false, -1)); } ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("%s cannot be converted to numeric", JsonbToCString(NULL, &j->root, VARSIZE(j))))); PG_RETURN_NULL(); }
Datum spg_text_inner_consistent(PG_FUNCTION_ARGS) { spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0); spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1); StrategyNumber strategy = in->strategy; text *inText; int inSize; int i; text *reconstrText = NULL; int maxReconstrLen = 0; text *prefixText = NULL; int prefixSize = 0; /* * If it's a collation-aware operator, but the collation is C, we can * treat it as non-collation-aware. */ if (strategy > 10 && lc_collate_is_c(PG_GET_COLLATION())) strategy -= 10; inText = DatumGetTextPP(in->query); inSize = VARSIZE_ANY_EXHDR(inText); /* * Reconstruct values represented at this tuple, including parent data, * prefix of this tuple if any, and the node label if any. in->level * should be the length of the previously reconstructed value, and the * number of bytes added here is prefixSize or prefixSize + 1. * * Note: we assume that in->reconstructedValue isn't toasted and doesn't * have a short varlena header. This is okay because it must have been * created by a previous invocation of this routine, and we always emit * long-format reconstructed values. */ Assert(in->level == 0 ? DatumGetPointer(in->reconstructedValue) == NULL : VARSIZE_ANY_EXHDR(DatumGetPointer(in->reconstructedValue)) == in->level); maxReconstrLen = in->level + 1; if (in->hasPrefix) { prefixText = DatumGetTextPP(in->prefixDatum); prefixSize = VARSIZE_ANY_EXHDR(prefixText); maxReconstrLen += prefixSize; } reconstrText = palloc(VARHDRSZ + maxReconstrLen); SET_VARSIZE(reconstrText, VARHDRSZ + maxReconstrLen); if (in->level) memcpy(VARDATA(reconstrText), VARDATA(DatumGetPointer(in->reconstructedValue)), in->level); if (prefixSize) memcpy(((char *) VARDATA(reconstrText)) + in->level, VARDATA_ANY(prefixText), prefixSize); /* last byte of reconstrText will be filled in below */ /* * Scan the child nodes. For each one, complete the reconstructed value * and see if it's consistent with the query. If so, emit an entry into * the output arrays. */ out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes); out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes); out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes); out->nNodes = 0; for (i = 0; i < in->nNodes; i++) { uint8 nodeChar = DatumGetUInt8(in->nodeLabels[i]); int thisLen; int r; bool res = false; /* If nodeChar is zero, don't include it in data */ if (nodeChar == '\0') thisLen = maxReconstrLen - 1; else { ((char *) VARDATA(reconstrText))[maxReconstrLen - 1] = nodeChar; thisLen = maxReconstrLen; } r = memcmp(VARDATA(reconstrText), VARDATA_ANY(inText), Min(inSize, thisLen)); switch (strategy) { case BTLessStrategyNumber: case BTLessEqualStrategyNumber: if (r <= 0) res = true; break; case BTEqualStrategyNumber: if (r == 0 && inSize >= thisLen) res = true; break; case BTGreaterEqualStrategyNumber: case BTGreaterStrategyNumber: if (r >= 0) res = true; break; case BTLessStrategyNumber + 10: case BTLessEqualStrategyNumber + 10: case BTGreaterEqualStrategyNumber + 10: case BTGreaterStrategyNumber + 10: /* * with non-C collation we need to traverse whole tree :-( */ res = true; break; default: elog(ERROR, "unrecognized strategy number: %d", in->strategy); break; } if (res) { out->nodeNumbers[out->nNodes] = i; out->levelAdds[out->nNodes] = thisLen - in->level; SET_VARSIZE(reconstrText, VARHDRSZ + thisLen); out->reconstructedValues[out->nNodes] = datumCopy(PointerGetDatum(reconstrText), false, -1); out->nNodes++; } } PG_RETURN_VOID(); }
/* * SPI_cursor_open() * * Open a prepared SPI plan as a portal */ Portal SPI_cursor_open(const char *name, void *plan, Datum *Values, const char *Nulls, bool read_only) { _SPI_plan *spiplan = (_SPI_plan *) plan; List *qtlist = spiplan->qtlist; List *ptlist = spiplan->ptlist; Query *queryTree; Plan *planTree; ParamListInfo paramLI; Snapshot snapshot; MemoryContext oldcontext; Portal portal; int k; /* Ensure that the plan contains only one query */ if (list_length(ptlist) != 1 || list_length(qtlist) != 1) ereport(ERROR, (errcode(ERRCODE_INVALID_CURSOR_DEFINITION), errmsg("cannot open multi-query plan as cursor"))); queryTree = (Query *) linitial((List *) linitial(qtlist)); planTree = (Plan *) linitial(ptlist); /* Must be a query that returns tuples */ switch (queryTree->commandType) { case CMD_SELECT: if (queryTree->into != NULL) ereport(ERROR, (errcode(ERRCODE_INVALID_CURSOR_DEFINITION), errmsg("cannot open SELECT INTO query as cursor"))); break; case CMD_UTILITY: if (!UtilityReturnsTuples(queryTree->utilityStmt)) ereport(ERROR, (errcode(ERRCODE_INVALID_CURSOR_DEFINITION), errmsg("cannot open non-SELECT query as cursor"))); break; default: ereport(ERROR, (errcode(ERRCODE_INVALID_CURSOR_DEFINITION), errmsg("cannot open non-SELECT query as cursor"))); break; } /* Reset SPI result */ SPI_processed = 0; SPI_tuptable = NULL; _SPI_current->processed = 0; _SPI_current->tuptable = NULL; /* Create the portal */ if (name == NULL || name[0] == '\0') { /* Use a random nonconflicting name */ portal = CreateNewPortal(); } else { /* In this path, error if portal of same name already exists */ portal = CreatePortal(name, false, false); } /* Switch to portals memory and copy the parsetree and plan to there */ oldcontext = MemoryContextSwitchTo(PortalGetHeapMemory(portal)); queryTree = copyObject(queryTree); planTree = copyObject(planTree); /* If the plan has parameters, set them up */ if (spiplan->nargs > 0) { paramLI = (ParamListInfo) palloc0((spiplan->nargs + 1) * sizeof(ParamListInfoData)); for (k = 0; k < spiplan->nargs; k++) { paramLI[k].kind = PARAM_NUM; paramLI[k].id = k + 1; paramLI[k].ptype = spiplan->argtypes[k]; paramLI[k].isnull = (Nulls && Nulls[k] == 'n'); if (paramLI[k].isnull) { /* nulls just copy */ paramLI[k].value = Values[k]; } else { /* pass-by-ref values must be copied into portal context */ int16 paramTypLen; bool paramTypByVal; get_typlenbyval(spiplan->argtypes[k], ¶mTypLen, ¶mTypByVal); paramLI[k].value = datumCopy(Values[k], paramTypByVal, paramTypLen); } } paramLI[k].kind = PARAM_INVALID; } else paramLI = NULL; /* * Set up the portal. */ PortalDefineQuery(portal, NULL, /* unfortunately don't have sourceText */ "SELECT", /* nor the raw parse tree... */ list_make1(queryTree), list_make1(planTree), PortalGetHeapMemory(portal)); MemoryContextSwitchTo(oldcontext); /* * Set up options for portal. */ portal->cursorOptions &= ~(CURSOR_OPT_SCROLL | CURSOR_OPT_NO_SCROLL); if (planTree == NULL || ExecSupportsBackwardScan(planTree)) portal->cursorOptions |= CURSOR_OPT_SCROLL; else portal->cursorOptions |= CURSOR_OPT_NO_SCROLL; /* * Set up the snapshot to use. (PortalStart will do CopySnapshot, * so we skip that here.) */ if (read_only) snapshot = ActiveSnapshot; else { CommandCounterIncrement(); snapshot = GetTransactionSnapshot(); } /* * Start portal execution. */ PortalStart(portal, paramLI, snapshot); Assert(portal->strategy == PORTAL_ONE_SELECT || portal->strategy == PORTAL_UTIL_SELECT); /* Return the created portal */ return portal; }
/* * get_attstatsslot * * Extract the contents of a "slot" of a pg_statistic tuple. * Returns TRUE if requested slot type was found, else FALSE. * * Unlike other routines in this file, this takes a pointer to an * already-looked-up tuple in the pg_statistic cache. We do this since * most callers will want to extract more than one value from the cache * entry, and we don't want to repeat the cache lookup unnecessarily. * * statstuple: pg_statistics tuple to be examined. * atttype: type OID of attribute (can be InvalidOid if values == NULL). * atttypmod: typmod of attribute (can be 0 if values == NULL). * reqkind: STAKIND code for desired statistics slot kind. * reqop: STAOP value wanted, or InvalidOid if don't care. * values, nvalues: if not NULL, the slot's stavalues are extracted. * numbers, nnumbers: if not NULL, the slot's stanumbers are extracted. * * If assigned, values and numbers are set to point to palloc'd arrays. * If the attribute type is pass-by-reference, the values referenced by * the values array are themselves palloc'd. The palloc'd stuff can be * freed by calling free_attstatsslot. */ bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers) { Form_pg_statistic stats = (Form_pg_statistic) GETSTRUCT(statstuple); int i, j; Datum val; bool isnull; ArrayType *statarray; int narrayelem; HeapTuple typeTuple; Form_pg_type typeForm; for (i = 0; i < STATISTIC_NUM_SLOTS; i++) { if ((&stats->stakind1)[i] == reqkind && (reqop == InvalidOid || (&stats->staop1)[i] == reqop)) break; } if (i >= STATISTIC_NUM_SLOTS) return false; /* not there */ if (values) { val = SysCacheGetAttr(STATRELATT, statstuple, Anum_pg_statistic_stavalues1 + i, &isnull); if (isnull) elog(ERROR, "stavalues is null"); statarray = DatumGetArrayTypeP(val); /* Need to get info about the array element type */ typeTuple = SearchSysCache(TYPEOID, ObjectIdGetDatum(atttype), 0, 0, 0); if (!HeapTupleIsValid(typeTuple)) elog(ERROR, "cache lookup failed for type %u", atttype); typeForm = (Form_pg_type) GETSTRUCT(typeTuple); /* Deconstruct array into Datum elements; NULLs not expected */ deconstruct_array(statarray, atttype, typeForm->typlen, typeForm->typbyval, typeForm->typalign, values, NULL, nvalues); /* * If the element type is pass-by-reference, we now have a bunch of * Datums that are pointers into the syscache value. Copy them to * avoid problems if syscache decides to drop the entry. */ if (!typeForm->typbyval) { for (j = 0; j < *nvalues; j++) { (*values)[j] = datumCopy((*values)[j], typeForm->typbyval, typeForm->typlen); } } ReleaseSysCache(typeTuple); /* * Free statarray if it's a detoasted copy. */ if ((Pointer) statarray != DatumGetPointer(val)) pfree(statarray); } if (numbers) { val = SysCacheGetAttr(STATRELATT, statstuple, Anum_pg_statistic_stanumbers1 + i, &isnull); if (isnull) elog(ERROR, "stanumbers is null"); statarray = DatumGetArrayTypeP(val); /* * We expect the array to be a 1-D float4 array; verify that. We don't * need to use deconstruct_array() since the array data is just going * to look like a C array of float4 values. */ narrayelem = ARR_DIMS(statarray)[0]; if (ARR_NDIM(statarray) != 1 || narrayelem <= 0 || ARR_HASNULL(statarray) || ARR_ELEMTYPE(statarray) != FLOAT4OID) elog(ERROR, "stanumbers is not a 1-D float4 array"); *numbers = (float4 *) palloc(narrayelem * sizeof(float4)); memcpy(*numbers, ARR_DATA_PTR(statarray), narrayelem * sizeof(float4)); *nnumbers = narrayelem; /* * Free statarray if it's a detoasted copy. */ if ((Pointer) statarray != DatumGetPointer(val)) pfree(statarray); } return true; }
Datum spg_text_inner_consistent(PG_FUNCTION_ARGS) { spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0); spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1); bool collate_is_c = lc_collate_is_c(PG_GET_COLLATION()); text *reconstrText = NULL; int maxReconstrLen = 0; text *prefixText = NULL; int prefixSize = 0; int i; /* * Reconstruct values represented at this tuple, including parent data, * prefix of this tuple if any, and the node label if it's non-dummy. * in->level should be the length of the previously reconstructed value, * and the number of bytes added here is prefixSize or prefixSize + 1. * * Note: we assume that in->reconstructedValue isn't toasted and doesn't * have a short varlena header. This is okay because it must have been * created by a previous invocation of this routine, and we always emit * long-format reconstructed values. */ Assert(in->level == 0 ? DatumGetPointer(in->reconstructedValue) == NULL : VARSIZE_ANY_EXHDR(DatumGetPointer(in->reconstructedValue)) == in->level); maxReconstrLen = in->level + 1; if (in->hasPrefix) { prefixText = DatumGetTextPP(in->prefixDatum); prefixSize = VARSIZE_ANY_EXHDR(prefixText); maxReconstrLen += prefixSize; } reconstrText = palloc(VARHDRSZ + maxReconstrLen); SET_VARSIZE(reconstrText, VARHDRSZ + maxReconstrLen); if (in->level) memcpy(VARDATA(reconstrText), VARDATA(DatumGetPointer(in->reconstructedValue)), in->level); if (prefixSize) memcpy(((char *) VARDATA(reconstrText)) + in->level, VARDATA_ANY(prefixText), prefixSize); /* last byte of reconstrText will be filled in below */ /* * Scan the child nodes. For each one, complete the reconstructed value * and see if it's consistent with the query. If so, emit an entry into * the output arrays. */ out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes); out->levelAdds = (int *) palloc(sizeof(int) * in->nNodes); out->reconstructedValues = (Datum *) palloc(sizeof(Datum) * in->nNodes); out->nNodes = 0; for (i = 0; i < in->nNodes; i++) { int16 nodeChar = DatumGetInt16(in->nodeLabels[i]); int thisLen; bool res = true; int j; /* If nodeChar is a dummy value, don't include it in data */ if (nodeChar <= 0) thisLen = maxReconstrLen - 1; else { ((unsigned char *) VARDATA(reconstrText))[maxReconstrLen - 1] = nodeChar; thisLen = maxReconstrLen; } for (j = 0; j < in->nkeys; j++) { StrategyNumber strategy = in->scankeys[j].sk_strategy; text *inText; int inSize; int r; /* * If it's a collation-aware operator, but the collation is C, we * can treat it as non-collation-aware. With non-C collation we * need to traverse whole tree :-( so there's no point in making * any check here. (Note also that our reconstructed value may * well end with a partial multibyte character, so that applying * any encoding-sensitive test to it would be risky anyhow.) */ if (strategy > 10) { if (collate_is_c) strategy -= 10; else continue; } inText = DatumGetTextPP(in->scankeys[j].sk_argument); inSize = VARSIZE_ANY_EXHDR(inText); r = memcmp(VARDATA(reconstrText), VARDATA_ANY(inText), Min(inSize, thisLen)); switch (strategy) { case BTLessStrategyNumber: case BTLessEqualStrategyNumber: if (r > 0) res = false; break; case BTEqualStrategyNumber: if (r != 0 || inSize < thisLen) res = false; break; case BTGreaterEqualStrategyNumber: case BTGreaterStrategyNumber: if (r < 0) res = false; break; default: elog(ERROR, "unrecognized strategy number: %d", in->scankeys[j].sk_strategy); break; } if (!res) break; /* no need to consider remaining conditions */ } if (res) { out->nodeNumbers[out->nNodes] = i; out->levelAdds[out->nNodes] = thisLen - in->level; SET_VARSIZE(reconstrText, VARHDRSZ + thisLen); out->reconstructedValues[out->nNodes] = datumCopy(PointerGetDatum(reconstrText), false, -1); out->nNodes++; } } PG_RETURN_VOID(); }
/* * Examine the given index tuple (which contains partial status of a certain * page range) by comparing it to the given value that comes from another heap * tuple. If the new value is outside the min/max range specified by the * existing tuple values, update the index tuple and return true. Otherwise, * return false and do not modify in this case. */ Datum brin_minmax_add_value(PG_FUNCTION_ARGS) { BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0); BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1); Datum newval = PG_GETARG_DATUM(2); bool isnull = PG_GETARG_DATUM(3); Oid colloid = PG_GET_COLLATION(); FmgrInfo *cmpFn; Datum compar; bool updated = false; Form_pg_attribute attr; AttrNumber attno; /* * If the new value is null, we record that we saw it if it's the first * one; otherwise, there's nothing to do. */ if (isnull) { if (column->bv_hasnulls) PG_RETURN_BOOL(false); column->bv_hasnulls = true; PG_RETURN_BOOL(true); } attno = column->bv_attno; attr = bdesc->bd_tupdesc->attrs[attno - 1]; /* * If the recorded value is null, store the new value (which we know to be * not null) as both minimum and maximum, and we're done. */ if (column->bv_allnulls) { column->bv_values[0] = datumCopy(newval, attr->attbyval, attr->attlen); column->bv_values[1] = datumCopy(newval, attr->attbyval, attr->attlen); column->bv_allnulls = false; PG_RETURN_BOOL(true); } /* * Otherwise, need to compare the new value with the existing boundaries * and update them accordingly. First check if it's less than the * existing minimum. */ cmpFn = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid, BTLessStrategyNumber); compar = FunctionCall2Coll(cmpFn, colloid, newval, column->bv_values[0]); if (DatumGetBool(compar)) { if (!attr->attbyval) pfree(DatumGetPointer(column->bv_values[0])); column->bv_values[0] = datumCopy(newval, attr->attbyval, attr->attlen); updated = true; } /* * And now compare it to the existing maximum. */ cmpFn = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid, BTGreaterStrategyNumber); compar = FunctionCall2Coll(cmpFn, colloid, newval, column->bv_values[1]); if (DatumGetBool(compar)) { if (!attr->attbyval) pfree(DatumGetPointer(column->bv_values[1])); column->bv_values[1] = datumCopy(newval, attr->attbyval, attr->attlen); updated = true; } PG_RETURN_BOOL(updated); }
/* * Given two BrinValues, update the first of them as a union of the summary * values contained in both. The second one is untouched. */ Datum brin_minmax_union(PG_FUNCTION_ARGS) { BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0); BrinValues *col_a = (BrinValues *) PG_GETARG_POINTER(1); BrinValues *col_b = (BrinValues *) PG_GETARG_POINTER(2); Oid colloid = PG_GET_COLLATION(); AttrNumber attno; Form_pg_attribute attr; FmgrInfo *finfo; bool needsadj; Assert(col_a->bv_attno == col_b->bv_attno); /* Adjust "hasnulls" */ if (!col_a->bv_hasnulls && col_b->bv_hasnulls) col_a->bv_hasnulls = true; /* If there are no values in B, there's nothing left to do */ if (col_b->bv_allnulls) PG_RETURN_VOID(); attno = col_a->bv_attno; attr = bdesc->bd_tupdesc->attrs[attno - 1]; /* * Adjust "allnulls". If A doesn't have values, just copy the values * from B into A, and we're done. We cannot run the operators in this * case, because values in A might contain garbage. Note we already * established that B contains values. */ if (col_a->bv_allnulls) { col_a->bv_allnulls = false; col_a->bv_values[0] = datumCopy(col_b->bv_values[0], attr->attbyval, attr->attlen); col_a->bv_values[1] = datumCopy(col_b->bv_values[1], attr->attbyval, attr->attlen); PG_RETURN_VOID(); } /* Adjust minimum, if B's min is less than A's min */ finfo = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid, BTLessStrategyNumber); needsadj = FunctionCall2Coll(finfo, colloid, col_b->bv_values[0], col_a->bv_values[0]); if (needsadj) { if (!attr->attbyval) pfree(DatumGetPointer(col_a->bv_values[0])); col_a->bv_values[0] = datumCopy(col_b->bv_values[0], attr->attbyval, attr->attlen); } /* Adjust maximum, if B's max is greater than A's max */ finfo = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid, BTGreaterStrategyNumber); needsadj = FunctionCall2Coll(finfo, colloid, col_b->bv_values[1], col_a->bv_values[1]); if (needsadj) { if (!attr->attbyval) pfree(DatumGetPointer(col_a->bv_values[1])); col_a->bv_values[1] = datumCopy(col_b->bv_values[1], attr->attbyval, attr->attlen); } PG_RETURN_VOID(); }
/* * compute_array_stats() -- compute statistics for an array column * * This function computes statistics useful for determining selectivity of * the array operators <@, &&, and @>. It is invoked by ANALYZE via the * compute_stats hook after sample rows have been collected. * * We also invoke the standard compute_stats function, which will compute * "scalar" statistics relevant to the btree-style array comparison operators. * However, exact duplicates of an entire array may be rare despite many * arrays sharing individual elements. This especially afflicts long arrays, * which are also liable to lack all scalar statistics due to the low * WIDTH_THRESHOLD used in analyze.c. So, in addition to the standard stats, * we find the most common array elements and compute a histogram of distinct * element counts. * * The algorithm used is Lossy Counting, as proposed in the paper "Approximate * frequency counts over data streams" by G. S. Manku and R. Motwani, in * Proceedings of the 28th International Conference on Very Large Data Bases, * Hong Kong, China, August 2002, section 4.2. The paper is available at * http://www.vldb.org/conf/2002/S10P03.pdf * * The Lossy Counting (aka LC) algorithm goes like this: * Let s be the threshold frequency for an item (the minimum frequency we * are interested in) and epsilon the error margin for the frequency. Let D * be a set of triples (e, f, delta), where e is an element value, f is that * element's frequency (actually, its current occurrence count) and delta is * the maximum error in f. We start with D empty and process the elements in * batches of size w. (The batch size is also known as "bucket size" and is * equal to 1/epsilon.) Let the current batch number be b_current, starting * with 1. For each element e we either increment its f count, if it's * already in D, or insert a new___ triple into D with values (e, 1, b_current * - 1). After processing each batch we prune D, by removing from it all * elements with f + delta <= b_current. After the algorithm finishes we * suppress all elements from D that do not satisfy f >= (s - epsilon) * N, * where N is the total number of elements in the input. We emit the * remaining elements with estimated frequency f/N. The LC paper proves * that this algorithm finds all elements with true frequency at least s, * and that no frequency is overestimated or is underestimated by more than * epsilon. Furthermore, given reasonable assumptions about the input * distribution, the required table size is no more than about 7 times w. * * In the absence of a principled basis for other particular values, we * follow ts_typanalyze() and use parameters s = 0.07/K, epsilon = s/10. * But we leave out the correction for stopwords, which do not apply to * arrays. These parameters give bucket width w = K/0.007 and maximum * expected hashtable size of about 1000 * K. * * Elements may repeat within an array. Since duplicates do not change the * behavior of <@, && or @>, we want to count each element only once per * array. Therefore, we store in the finished pg_statistic entry each * element's frequency as the fraction of all non-null rows that contain it. * We divide the raw counts by nonnull_cnt to get those figures. */ static void compute_array_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc, int samplerows, double totalrows) { ArrayAnalyzeExtraData *extra_data; int num_mcelem; int null_cnt = 0; int null_elem_cnt = 0; int analyzed_rows = 0; /* This is D from the LC algorithm. */ HTAB *elements_tab; HASHCTL elem_hash_ctl; HASH_SEQ_STATUS scan_status; /* This is the current bucket number from the LC algorithm */ int b_current; /* This is 'w' from the LC algorithm */ int bucket_width; int array_no; int64 element_no; TrackItem *item; int slot_idx; HTAB *count_tab; HASHCTL count_hash_ctl; DECountItem *count_item; extra_data = (ArrayAnalyzeExtraData *) stats->extra_data; /* * Invoke analyze.c's standard analysis function to create scalar-style * stats for the column. It will expect its own extra_data pointer, so * temporarily install that. */ stats->extra_data = extra_data->std_extra_data; (*extra_data->std_compute_stats) (stats, fetchfunc, samplerows, totalrows); stats->extra_data = extra_data; /* * Set up static pointer for use by subroutines. We wait till here in * case std_compute_stats somehow recursively invokes us (probably not * possible, but ...) */ array_extra_data = extra_data; /* * We want statistics_target * 10 elements in the MCELEM array. This * multiplier is pretty arbitrary, but is meant to reflect the fact that * the number of individual elements tracked in pg_statistic ought to be * more than the number of values for a simple scalar column. */ num_mcelem = stats->attr->attstattarget * 10; /* * We set bucket width equal to num_mcelem / 0.007 as per the comment * above. */ bucket_width = num_mcelem * 1000 / 7; /* * Create the hashtable. It will be in local memory, so we don't need to * worry about overflowing the initial size. Also we don't need to pay any * attention to locking and memory management. */ MemSet(&elem_hash_ctl, 0, sizeof(elem_hash_ctl)); elem_hash_ctl.keysize = sizeof(Datum); elem_hash_ctl.entrysize = sizeof(TrackItem); elem_hash_ctl.hash = element_hash; elem_hash_ctl.match = element_match; elem_hash_ctl.hcxt = CurrentMemoryContext; elements_tab = hash_create("Analyzed elements table", num_mcelem, &elem_hash_ctl, HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT); /* hashtable for array distinct elements counts */ MemSet(&count_hash_ctl, 0, sizeof(count_hash_ctl)); count_hash_ctl.keysize = sizeof(int); count_hash_ctl.entrysize = sizeof(DECountItem); count_hash_ctl.hcxt = CurrentMemoryContext; count_tab = hash_create("Array distinct element count table", 64, &count_hash_ctl, HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); /* Initialize counters. */ b_current = 1; element_no = 0; /* Loop over the arrays. */ for (array_no = 0; array_no < samplerows; array_no++) { Datum value; bool isnull; ArrayType *array; int num_elems; Datum *elem_values; bool *elem_nulls; bool null_present; int j; int64 prev_element_no = element_no; int distinct_count; bool count_item_found; vacuum_delay_point(); value = fetchfunc(stats, array_no, &isnull); if (isnull) { /* array is null, just count that */ null_cnt++; continue; } /* Skip too-large values. */ if (toast_raw_datum_size(value) > ARRAY_WIDTH_THRESHOLD) continue; else analyzed_rows++; /* * Now detoast the array if needed, and deconstruct into datums. */ array = DatumGetArrayTypeP(value); Assert(ARR_ELEMTYPE(array) == extra_data->type_id); deconstruct_array(array, extra_data->type_id, extra_data->typlen, extra_data->typbyval, extra_data->typalign, &elem_values, &elem_nulls, &num_elems); /* * We loop through the elements in the array and add them to our * tracking hashtable. */ null_present = false; for (j = 0; j < num_elems; j++) { Datum elem_value; bool found; /* No null element processing other than flag setting here */ if (elem_nulls[j]) { null_present = true; continue; } /* Lookup current element in hashtable, adding it if new___ */ elem_value = elem_values[j]; item = (TrackItem *) hash_search(elements_tab, (const void *) &elem_value, HASH_ENTER, &found); if (found) { /* The element value is already on the tracking list */ /* * The operators we assist ignore duplicate array elements, so * count a given distinct element only once per array. */ if (item->last_container == array_no) continue; item->frequency++; item->last_container = array_no; } else { /* Initialize new___ tracking list element */ /* * If element type is pass-by-reference, we must copy it into * palloc'd space, so that we can release the array below. (We * do this so that the space needed for element values is * limited by the size of the hashtable; if we kept all the * array values around, it could be much more.) */ item->key = datumCopy(elem_value, extra_data->typbyval, extra_data->typlen); item->frequency = 1; item->delta = b_current - 1; item->last_container = array_no; } /* element_no is the number of elements processed (ie N) */ element_no++; /* We prune the D structure after processing each bucket */ if (element_no % bucket_width == 0) { prune_element_hashtable(elements_tab, b_current); b_current++; } } /* Count null element presence once per array. */ if (null_present) null_elem_cnt++; /* Update frequency of the particular array distinct element count. */ distinct_count = (int) (element_no - prev_element_no); count_item = (DECountItem *) hash_search(count_tab, &distinct_count, HASH_ENTER, &count_item_found); if (count_item_found) count_item->frequency++; else count_item->frequency = 1; /* Free memory allocated while detoasting. */ if (PointerGetDatum(array) != value) pfree(array); pfree(elem_values); pfree(elem_nulls); } /* Skip pg_statistic slots occupied by standard statistics */ slot_idx = 0; while (slot_idx < STATISTIC_NUM_SLOTS && stats->stakind[slot_idx] != 0) slot_idx++; if (slot_idx > STATISTIC_NUM_SLOTS - 2) elog(ERROR, "insufficient pg_statistic slots for array stats"); /* We can only compute real stats if we found some non-null values. */ if (analyzed_rows > 0) { int nonnull_cnt = analyzed_rows; int count_items_count; int i; TrackItem **sort_table; int track_len; int64 cutoff_freq; int64 minfreq, maxfreq; /* * We assume the standard stats code already took care of setting * stats_valid, stanullfrac, stawidth, stadistinct. We'd have to * re-compute those values if we wanted to not store the standard * stats. */ /* * Construct an array of the interesting hashtable items, that is, * those meeting the cutoff frequency (s - epsilon)*N. Also identify * the minimum and maximum frequencies among these items. * * Since epsilon = s/10 and bucket_width = 1/epsilon, the cutoff * frequency is 9*N / bucket_width. */ cutoff_freq = 9 * element_no / bucket_width; i = hash_get_num_entries(elements_tab); /* surely enough space */ sort_table = (TrackItem **) palloc(sizeof(TrackItem *) * i); hash_seq_init(&scan_status, elements_tab); track_len = 0; minfreq = element_no; maxfreq = 0; while ((item = (TrackItem *) hash_seq_search(&scan_status)) != NULL) { if (item->frequency > cutoff_freq) { sort_table[track_len++] = item; minfreq = Min(minfreq, item->frequency); maxfreq = Max(maxfreq, item->frequency); } } Assert(track_len <= i); /* emit some statistics for debug purposes */ elog(DEBUG3, "compute_array_stats: target # mces = %d, " "bucket width = %d, " "# elements = " INT64_FORMAT ", hashtable size = %d, " "usable entries = %d", num_mcelem, bucket_width, element_no, i, track_len); /* * If we obtained more elements than we really want, get rid of those * with least frequencies. The easiest way is to qsort the array into * descending frequency order and truncate the array. */ if (num_mcelem < track_len) { qsort(sort_table, track_len, sizeof(TrackItem *), trackitem_compare_frequencies_desc); /* reset minfreq to the smallest frequency we're keeping */ minfreq = sort_table[num_mcelem - 1]->frequency; } else num_mcelem = track_len; /* Generate MCELEM slot entry */ if (num_mcelem > 0) { MemoryContext old_context; Datum *mcelem_values; float4 *mcelem_freqs; /* * We want to store statistics sorted on the element value using * the element type's default comparison function. This permits * fast binary searches in selectivity estimation functions. */ qsort(sort_table, num_mcelem, sizeof(TrackItem *), trackitem_compare_element); /* Must copy the target values into anl_context */ old_context = MemoryContextSwitchTo(stats->anl_context); /* * We sorted statistics on the element value, but we want to be * able to find the minimal and maximal frequencies without going * through all the values. We also want the frequency of null * elements. Store these three values at the end of mcelem_freqs. */ mcelem_values = (Datum *) palloc(num_mcelem * sizeof(Datum)); mcelem_freqs = (float4 *) palloc((num_mcelem + 3) * sizeof(float4)); /* * See comments above about use of nonnull_cnt as the divisor for * the final frequency estimates. */ for (i = 0; i < num_mcelem; i++) { TrackItem *item = sort_table[i]; mcelem_values[i] = datumCopy(item->key, extra_data->typbyval, extra_data->typlen); mcelem_freqs[i] = (double) item->frequency / (double) nonnull_cnt; } mcelem_freqs[i++] = (double) minfreq / (double) nonnull_cnt; mcelem_freqs[i++] = (double) maxfreq / (double) nonnull_cnt; mcelem_freqs[i++] = (double) null_elem_cnt / (double) nonnull_cnt; MemoryContextSwitchTo(old_context); stats->stakind[slot_idx] = STATISTIC_KIND_MCELEM; stats->staop[slot_idx] = extra_data->eq_opr; stats->stanumbers[slot_idx] = mcelem_freqs; /* See above comment about extra stanumber entries */ stats->numnumbers[slot_idx] = num_mcelem + 3; stats->stavalues[slot_idx] = mcelem_values; stats->numvalues[slot_idx] = num_mcelem; /* We are storing values of element type */ stats->statypid[slot_idx] = extra_data->type_id; stats->statyplen[slot_idx] = extra_data->typlen; stats->statypbyval[slot_idx] = extra_data->typbyval; stats->statypalign[slot_idx] = extra_data->typalign; slot_idx++; } /* Generate DECHIST slot entry */ count_items_count = hash_get_num_entries(count_tab); if (count_items_count > 0) { int num_hist = stats->attr->attstattarget; DECountItem **sorted_count_items; int j; int delta; int64 frac; float4 *hist; /* num_hist must be at least 2 for the loop below to work */ num_hist = Max(num_hist, 2); /* * Create an array of DECountItem pointers, and sort them into * increasing count order. */ sorted_count_items = (DECountItem **) palloc(sizeof(DECountItem *) * count_items_count); hash_seq_init(&scan_status, count_tab); j = 0; while ((count_item = (DECountItem *) hash_seq_search(&scan_status)) != NULL) { sorted_count_items[j++] = count_item; } qsort(sorted_count_items, count_items_count, sizeof(DECountItem *), countitem_compare_count); /* * Prepare to fill stanumbers with the histogram, followed by the * average count. This array must be stored in anl_context. */ hist = (float4 *) MemoryContextAlloc(stats->anl_context, sizeof(float4) * (num_hist + 1)); hist[num_hist] = (double) element_no / (double) nonnull_cnt; /*---------- * Construct the histogram of distinct-element counts (DECs). * * The object of this loop is to copy the min and max DECs to * hist[0] and hist[num_hist - 1], along with evenly-spaced DECs * in between (where "evenly-spaced" is with reference to the * whole input population of arrays). If we had a complete sorted * array of DECs, one per analyzed row, the i'th hist value would * come from DECs[i * (analyzed_rows - 1) / (num_hist - 1)] * (compare the histogram-making loop in compute_scalar_stats()). * But instead of that we have the sorted_count_items[] array, * which holds unique DEC values with their frequencies (that is, * a run-length-compressed version of the full array). So we * control advancing through sorted_count_items[] with the * variable "frac", which is defined as (x - y) * (num_hist - 1), * where x is the index in the notional DECs array corresponding * to the start of the next sorted_count_items[] element's run, * and y is the index in DECs from which we should take the next * histogram value. We have to advance whenever x <= y, that is * frac <= 0. The x component is the sum of the frequencies seen * so far (up through the current sorted_count_items[] element), * and of course y * (num_hist - 1) = i * (analyzed_rows - 1), * per the subscript calculation above. (The subscript calculation * implies dropping any fractional part of y; in this formulation * that's handled by not advancing until frac reaches 1.) * * Even though frac has a bounded range, it could overflow int32 * when working with very large statistics targets, so we do that * math in int64. *---------- */ delta = analyzed_rows - 1; j = 0; /* current index in sorted_count_items */ /* Initialize frac for sorted_count_items[0]; y is initially 0 */ frac = (int64) sorted_count_items[0]->frequency * (num_hist - 1); for (i = 0; i < num_hist; i++) { while (frac <= 0) { /* Advance, and update x component of frac */ j++; frac += (int64) sorted_count_items[j]->frequency * (num_hist - 1); } hist[i] = sorted_count_items[j]->count; frac -= delta; /* update y for upcoming i increment */ } Assert(j == count_items_count - 1); stats->stakind[slot_idx] = STATISTIC_KIND_DECHIST; stats->staop[slot_idx] = extra_data->eq_opr; stats->stanumbers[slot_idx] = hist; stats->numnumbers[slot_idx] = num_hist + 1; slot_idx++; } } /* * We don't need to bother cleaning up any of our temporary palloc's. The * hashtable should also go away, as it used a child memory context. */ }
/* * Convert a BrinTuple back to a BrinMemTuple. This is the reverse of * brin_form_tuple. * * As an optimization, the caller can pass a previously allocated 'dMemtuple'. * This avoids having to allocate it here, which can be useful when this * function is called many times in a loop. It is caller's responsibility * that the given BrinMemTuple matches what we need here. * * Note we don't need the "on disk tupdesc" here; we rely on our own routine to * deconstruct the tuple from the on-disk format. */ BrinMemTuple * brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple) { BrinMemTuple *dtup; Datum *values; bool *allnulls; bool *hasnulls; char *tp; bits8 *nullbits; int keyno; int valueno; MemoryContext oldcxt; dtup = dMemtuple ? brin_memtuple_initialize(dMemtuple, brdesc) : brin_new_memtuple(brdesc); if (BrinTupleIsPlaceholder(tuple)) dtup->bt_placeholder = true; dtup->bt_blkno = tuple->bt_blkno; values = dtup->bt_values; allnulls = dtup->bt_allnulls; hasnulls = dtup->bt_hasnulls; tp = (char *) tuple + BrinTupleDataOffset(tuple); if (BrinTupleHasNulls(tuple)) nullbits = (bits8 *) ((char *) tuple + SizeOfBrinTuple); else nullbits = NULL; brin_deconstruct_tuple(brdesc, tp, nullbits, BrinTupleHasNulls(tuple), values, allnulls, hasnulls); /* * Iterate to assign each of the values to the corresponding item in the * values array of each column. The copies occur in the tuple's context. */ oldcxt = MemoryContextSwitchTo(dtup->bt_context); for (valueno = 0, keyno = 0; keyno < brdesc->bd_tupdesc->natts; keyno++) { int i; if (allnulls[keyno]) { valueno += brdesc->bd_info[keyno]->oi_nstored; continue; } /* * We would like to skip datumCopy'ing the values datum in some cases, * caller permitting ... */ for (i = 0; i < brdesc->bd_info[keyno]->oi_nstored; i++) dtup->bt_columns[keyno].bv_values[i] = datumCopy(values[valueno++], brdesc->bd_info[keyno]->oi_typcache[i]->typbyval, brdesc->bd_info[keyno]->oi_typcache[i]->typlen); dtup->bt_columns[keyno].bv_hasnulls = hasnulls[keyno]; dtup->bt_columns[keyno].bv_allnulls = false; } MemoryContextSwitchTo(oldcxt); return dtup; }
/* * Copied from Postgres' src/backend/optimizer/util/clauses.c * * evaluate_expr: pre-evaluate a constant expression * * We use the executor's routine ExecEvalExpr() to avoid duplication of * code and ensure we get the same result as the executor would get. */ Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation) { EState *estate; ExprState *exprstate; MemoryContext oldcontext; Datum const_val; bool const_is_null; int16 resultTypLen; bool resultTypByVal; /* * To use the executor, we need an EState. */ estate = CreateExecutorState(); /* We can use the estate's working context to avoid memory leaks. */ oldcontext = MemoryContextSwitchTo(estate->es_query_cxt); /* Make sure any opfuncids are filled in. */ fix_opfuncids((Node *) expr); /* * Prepare expr for execution. (Note: we can't use ExecPrepareExpr * because it'd result in recursively invoking eval_const_expressions.) */ exprstate = ExecInitExpr(expr, NULL); /* * And evaluate it. * * It is OK to use a default econtext because none of the ExecEvalExpr() * code used in this situation will use econtext. That might seem * fortuitous, but it's not so unreasonable --- a constant expression does * not depend on context, by definition, n'est ce pas? */ const_val = ExecEvalExprSwitchContext(exprstate, GetPerTupleExprContext(estate), &const_is_null, NULL); /* Get info needed about result datatype */ get_typlenbyval(result_type, &resultTypLen, &resultTypByVal); /* Get back to outer memory context */ MemoryContextSwitchTo(oldcontext); /* * Must copy result out of sub-context used by expression eval. * * Also, if it's varlena, forcibly detoast it. This protects us against * storing TOAST pointers into plans that might outlive the referenced * data. (makeConst would handle detoasting anyway, but it's worth a few * extra lines here so that we can do the copy and detoast in one step.) */ if (!const_is_null) { if (resultTypLen == -1) const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val)); else const_val = datumCopy(const_val, resultTypByVal, resultTypLen); } /* Release all the junk we just created */ FreeExecutorState(estate); /* * Make the constant result node. */ return (Expr *) makeConst(result_type, result_typmod, result_collation, resultTypLen, const_val, const_is_null, resultTypByVal); }