mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Simplify SRFs using materialize mode in contrib/ modules
9e98583
introduced a helper to centralize building their needed state
(tuplestore, tuple descriptors, etc.), checking for any errors. This
commit updates all places of contrib/ that can be switched to use
SetSingleFuncCall() as a drop-in replacement, resulting in the removal
of a lot of boilerplate code in all the modules updated by this commit.
Per analysis, some places remain as they are:
- pg_logdir_ls() in adminpack/ uses historically TYPEFUNC_RECORD as
return type, and I suspect that changing it may cause issues at run-time
with some of its past versions, down to 1.0.
- dblink/ uses a wrapper function doing exactly the work of
SetSingleFuncCall(). Here the switch should be possible, but rather
invasive so it does not seem the extra backpatch maintenance cost.
- tablefunc/, similarly, uses multiple helper functions with portions of
SetSingleFuncCall() spread across the code paths of this module.
Author: Melanie Plageman
Discussion: https://postgr.es/m/CAAKRu_bvDPJoL9mH6eYwvBpPtTGQwbDzfJbCM-OjkSZDu5yTPg@mail.gmail.com
This commit is contained in:
@ -126,9 +126,6 @@ brin_page_items(PG_FUNCTION_ARGS)
|
||||
bytea *raw_page = PG_GETARG_BYTEA_P(0);
|
||||
Oid indexRelid = PG_GETARG_OID(1);
|
||||
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
|
||||
TupleDesc tupdesc;
|
||||
MemoryContext oldcontext;
|
||||
Tuplestorestate *tupstore;
|
||||
Relation indexRel;
|
||||
brin_column_state **columns;
|
||||
BrinDesc *bdesc;
|
||||
@ -143,29 +140,7 @@ brin_page_items(PG_FUNCTION_ARGS)
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
errmsg("must be superuser to use raw page functions")));
|
||||
|
||||
/* check to see if caller supports us returning a tuplestore */
|
||||
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("set-valued function called in context that cannot accept a set")));
|
||||
if (!(rsinfo->allowedModes & SFRM_Materialize))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("materialize mode required, but it is not allowed in this context")));
|
||||
|
||||
/* 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");
|
||||
|
||||
/* Build tuplestore to hold the result rows */
|
||||
oldcontext = MemoryContextSwitchTo(rsinfo->econtext->ecxt_per_query_memory);
|
||||
|
||||
tupstore = tuplestore_begin_heap(true, false, work_mem);
|
||||
rsinfo->returnMode = SFRM_Materialize;
|
||||
rsinfo->setResult = tupstore;
|
||||
rsinfo->setDesc = tupdesc;
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
SetSingleFuncCall(fcinfo, 0);
|
||||
|
||||
indexRel = index_open(indexRelid, AccessShareLock);
|
||||
bdesc = brin_build_desc(indexRel);
|
||||
@ -251,7 +226,7 @@ brin_page_items(PG_FUNCTION_ARGS)
|
||||
int att = attno - 1;
|
||||
|
||||
values[0] = UInt16GetDatum(offset);
|
||||
switch (TupleDescAttr(tupdesc, 1)->atttypid)
|
||||
switch (TupleDescAttr(rsinfo->setDesc, 1)->atttypid)
|
||||
{
|
||||
case INT8OID:
|
||||
values[1] = Int64GetDatum((int64) dtup->bt_blkno);
|
||||
@ -301,7 +276,7 @@ brin_page_items(PG_FUNCTION_ARGS)
|
||||
}
|
||||
}
|
||||
|
||||
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
|
||||
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
|
||||
|
||||
/*
|
||||
* If the item was unused, jump straight to the next one; otherwise,
|
||||
|
Reference in New Issue
Block a user