1
0
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:
Michael Paquier
2022-03-08 10:12:22 +09:00
parent d5ed9da41d
commit 5b81703787
8 changed files with 32 additions and 301 deletions

View File

@ -165,7 +165,6 @@ static bool check_tuple_visibility(HeapCheckContext *ctx);
static void report_corruption(HeapCheckContext *ctx, char *msg);
static void report_toast_corruption(HeapCheckContext *ctx,
ToastedAttribute *ta, char *msg);
static TupleDesc verify_heapam_tupdesc(void);
static FullTransactionId FullTransactionIdFromXidAndCtx(TransactionId xid,
const HeapCheckContext *ctx);
static void update_cached_xid_range(HeapCheckContext *ctx);
@ -214,8 +213,6 @@ Datum
verify_heapam(PG_FUNCTION_ARGS)
{
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
MemoryContext old_context;
bool random_access;
HeapCheckContext ctx;
Buffer vmbuffer = InvalidBuffer;
Oid relid;
@ -227,16 +224,6 @@ verify_heapam(PG_FUNCTION_ARGS)
BlockNumber nblocks;
const char *skip;
/* 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_SYNTAX_ERROR),
errmsg("materialize mode required, but it is not allowed in this context")));
/* Check supplied arguments */
if (PG_ARGISNULL(0))
ereport(ERROR,
@ -290,15 +277,10 @@ verify_heapam(PG_FUNCTION_ARGS)
*/
ctx.attnum = -1;
/* The tupdesc and tuplestore must be created in ecxt_per_query_memory */
old_context = MemoryContextSwitchTo(rsinfo->econtext->ecxt_per_query_memory);
random_access = (rsinfo->allowedModes & SFRM_Materialize_Random) != 0;
ctx.tupdesc = verify_heapam_tupdesc();
ctx.tupstore = tuplestore_begin_heap(random_access, false, work_mem);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = ctx.tupstore;
rsinfo->setDesc = ctx.tupdesc;
MemoryContextSwitchTo(old_context);
/* Construct the tuplestore and tuple descriptor */
SetSingleFuncCall(fcinfo, 0);
ctx.tupdesc = rsinfo->setDesc;
ctx.tupstore = rsinfo->setResult;
/* Open relation, check relkind and access method */
ctx.rel = relation_open(relid, AccessShareLock);
@ -630,26 +612,6 @@ report_toast_corruption(HeapCheckContext *ctx, ToastedAttribute *ta,
ctx->is_corrupt = true;
}
/*
* Construct the TupleDesc used to report messages about corruptions found
* while scanning the heap.
*/
static TupleDesc
verify_heapam_tupdesc(void)
{
TupleDesc tupdesc;
AttrNumber a = 0;
tupdesc = CreateTemplateTupleDesc(HEAPCHECK_RELATION_COLS);
TupleDescInitEntry(tupdesc, ++a, "blkno", INT8OID, -1, 0);
TupleDescInitEntry(tupdesc, ++a, "offnum", INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, ++a, "attnum", INT4OID, -1, 0);
TupleDescInitEntry(tupdesc, ++a, "msg", TEXTOID, -1, 0);
Assert(a == HEAPCHECK_RELATION_COLS);
return BlessTupleDesc(tupdesc);
}
/*
* Check for tuple header corruption.
*