aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2022-12-21 10:11:22 +0900
committerMichael Paquier <michael@paquier.xyz>2022-12-21 10:11:22 +0900
commit22e3b558052aa209cba2a8fec192d76b5faef19e (patch)
treeb4626ba839fad837c50e039ce56e6819a5e0f9f6 /src/backend/utils
parentf03bd5717eaf31569ca797a2f7d65608f88ac2a2 (diff)
downloadpostgresql-22e3b558052aa209cba2a8fec192d76b5faef19e.tar.gz
postgresql-22e3b558052aa209cba2a8fec192d76b5faef19e.zip
Switch some system functions to use get_call_result_type()
This shaves some code by replacing the combinations of CreateTemplateTupleDesc()/TupleDescInitEntry() hardcoding a mapping of the attributes listed in pg_proc.dat by get_call_result_type() to build the TupleDesc needed for the rows generated. get_call_result_type() is more expensive than the former style, but this removes some duplication with the lists of OUT parameters (pg_proc.dat and the attributes hardcoded in these code paths). This is applied to functions that are not considered as critical (aka that could be called repeatedly for monitoring purposes). Author: Bharath Rupireddy Reviewed-by: Robert Haas, Álvaro Herrera, Tom Lane, Michael Paquier Discussion: https://postgr.es/m/CALj2ACV23HW5HP5hFjd89FNS-z5X8r2jNXdMXcpN2BgTtKd87w@mail.gmail.com
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/datetime.c17
-rw-r--r--src/backend/utils/adt/misc.c31
-rw-r--r--src/backend/utils/adt/partitionfuncs.c14
-rw-r--r--src/backend/utils/adt/tsvector_op.c15
-rw-r--r--src/backend/utils/misc/pg_controldata.c108
5 files changed, 26 insertions, 159 deletions
diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c
index b5b117a8ca7..8afda0e5d22 100644
--- a/src/backend/utils/adt/datetime.c
+++ b/src/backend/utils/adt/datetime.c
@@ -4983,19 +4983,10 @@ pg_timezone_abbrevs(PG_FUNCTION_ARGS)
*pindex = 0;
funcctx->user_fctx = (void *) pindex;
- /*
- * build tupdesc for result tuples. This must match this function's
- * pg_proc entry!
- */
- tupdesc = CreateTemplateTupleDesc(3);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "abbrev",
- TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "utc_offset",
- INTERVALOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "is_dst",
- BOOLOID, -1, 0);
-
- funcctx->tuple_desc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+ funcctx->tuple_desc = tupdesc;
+
MemoryContextSwitchTo(oldcontext);
}
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index d678d286009..7808fbd4486 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -427,18 +427,9 @@ pg_get_keywords(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
- tupdesc = CreateTemplateTupleDesc(5);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
- TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "catcode",
- CHAROID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "barelabel",
- BOOLOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "catdesc",
- TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "baredesc",
- TEXTOID, -1, 0);
-
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+ funcctx->tuple_desc = tupdesc;
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
MemoryContextSwitchTo(oldcontext);
@@ -515,20 +506,8 @@ pg_get_catalog_foreign_keys(PG_FUNCTION_ARGS)
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
- tupdesc = CreateTemplateTupleDesc(6);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "fktable",
- REGCLASSOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "fkcols",
- TEXTARRAYOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "pktable",
- REGCLASSOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "pkcols",
- TEXTARRAYOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "is_array",
- BOOLOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 6, "is_opt",
- BOOLOID, -1, 0);
-
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
/*
diff --git a/src/backend/utils/adt/partitionfuncs.c b/src/backend/utils/adt/partitionfuncs.c
index 96b5ae52d27..84518630a56 100644
--- a/src/backend/utils/adt/partitionfuncs.c
+++ b/src/backend/utils/adt/partitionfuncs.c
@@ -88,17 +88,9 @@ pg_partition_tree(PG_FUNCTION_ARGS)
*/
partitions = find_all_inheritors(rootrelid, AccessShareLock, NULL);
- tupdesc = CreateTemplateTupleDesc(PG_PARTITION_TREE_COLS);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "relid",
- REGCLASSOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "parentid",
- REGCLASSOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "isleaf",
- BOOLOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "level",
- INT4OID, -1, 0);
-
- funcctx->tuple_desc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+ funcctx->tuple_desc = tupdesc;
/* The only state we need is the partition list */
funcctx->user_fctx = (void *) partitions;
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index f7c1e3d6d65..caeb85b4ca1 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -647,7 +647,9 @@ tsvector_unnest(PG_FUNCTION_ARGS)
INT2ARRAYOID, -1, 0);
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "weights",
TEXTARRAYOID, -1, 0);
- funcctx->tuple_desc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+ funcctx->tuple_desc = tupdesc;
funcctx->user_fctx = PG_GETARG_TSVECTOR_COPY(0);
@@ -2302,14 +2304,9 @@ ts_setup_firstcall(FunctionCallInfo fcinfo, FuncCallContext *funcctx,
}
Assert(stat->stackpos <= stat->maxdepth);
- tupdesc = CreateTemplateTupleDesc(3);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "word",
- TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "ndoc",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "nentry",
- INT4OID, -1, 0);
- funcctx->tuple_desc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+ funcctx->tuple_desc = tupdesc;
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
MemoryContextSwitchTo(oldcontext);
diff --git a/src/backend/utils/misc/pg_controldata.c b/src/backend/utils/misc/pg_controldata.c
index 781f8b87580..0703892ed4a 100644
--- a/src/backend/utils/misc/pg_controldata.c
+++ b/src/backend/utils/misc/pg_controldata.c
@@ -38,20 +38,8 @@ pg_control_system(PG_FUNCTION_ARGS)
ControlFileData *ControlFile;
bool crc_ok;
- /*
- * Construct a tuple descriptor for the result row. This must match this
- * function's pg_proc entry!
- */
- tupdesc = CreateTemplateTupleDesc(4);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pg_control_version",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "catalog_version_no",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "system_identifier",
- INT8OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "pg_control_last_modified",
- TIMESTAMPTZOID, -1, 0);
- tupdesc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
/* read the control file */
ControlFile = get_controlfile(DataDir, &crc_ok);
@@ -88,48 +76,8 @@ pg_control_checkpoint(PG_FUNCTION_ARGS)
char xlogfilename[MAXFNAMELEN];
bool crc_ok;
- /*
- * Construct a tuple descriptor for the result row. This must match this
- * function's pg_proc entry!
- */
- tupdesc = CreateTemplateTupleDesc(18);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "checkpoint_lsn",
- PG_LSNOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "redo_lsn",
- PG_LSNOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "redo_wal_file",
- TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "timeline_id",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "prev_timeline_id",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 6, "full_page_writes",
- BOOLOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 7, "next_xid",
- TEXTOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 8, "next_oid",
- OIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 9, "next_multixact_id",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 10, "next_multi_offset",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 11, "oldest_xid",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 12, "oldest_xid_dbid",
- OIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 13, "oldest_active_xid",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 14, "oldest_multi_xid",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 15, "oldest_multi_dbid",
- OIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 16, "oldest_commit_ts_xid",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 17, "newest_commit_ts_xid",
- XIDOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 18, "checkpoint_time",
- TIMESTAMPTZOID, -1, 0);
- tupdesc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
/* Read the control file. */
ControlFile = get_controlfile(DataDir, &crc_ok);
@@ -217,22 +165,8 @@ pg_control_recovery(PG_FUNCTION_ARGS)
ControlFileData *ControlFile;
bool crc_ok;
- /*
- * Construct a tuple descriptor for the result row. This must match this
- * function's pg_proc entry!
- */
- tupdesc = CreateTemplateTupleDesc(5);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "min_recovery_end_lsn",
- PG_LSNOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "min_recovery_end_timeline",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "backup_start_lsn",
- PG_LSNOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "backup_end_lsn",
- PG_LSNOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "end_of_backup_record_required",
- BOOLOID, -1, 0);
- tupdesc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
/* read the control file */
ControlFile = get_controlfile(DataDir, &crc_ok);
@@ -270,34 +204,8 @@ pg_control_init(PG_FUNCTION_ARGS)
ControlFileData *ControlFile;
bool crc_ok;
- /*
- * Construct a tuple descriptor for the result row. This must match this
- * function's pg_proc entry!
- */
- tupdesc = CreateTemplateTupleDesc(11);
- TupleDescInitEntry(tupdesc, (AttrNumber) 1, "max_data_alignment",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 2, "database_block_size",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 3, "blocks_per_segment",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_block_size",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 5, "bytes_per_wal_segment",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 6, "max_identifier_length",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 7, "max_index_columns",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 8, "max_toast_chunk_size",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 9, "large_object_chunk_size",
- INT4OID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 10, "float8_pass_by_value",
- BOOLOID, -1, 0);
- TupleDescInitEntry(tupdesc, (AttrNumber) 11, "data_page_checksum_version",
- INT4OID, -1, 0);
- tupdesc = BlessTupleDesc(tupdesc);
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
/* read the control file */
ControlFile = get_controlfile(DataDir, &crc_ok);