From 8a81f53b6cd043ed923b521d2917292f6726df64 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 28 Jun 2023 17:16:49 +0200 Subject: [PATCH 13/17] Move BuildDescForRelation() from tupdesc.c to tablecmds.c BuildDescForRelation() main job is to convert ColumnDef lists to pg_attribute/tuple descriptor arrays, which is really mostly an internal subroutine of DefineRelation() and some related functions, which is more the remit of tablecmds.c and doesn't have much to do with the basic tuple descriptor interfaces in tupdesc.c. This is also supported by observing the header includes we can remove in tupdesc.c. By moving it over, we can also (in the future) make BuildDescForRelation() use more internals of tablecmds.c that are not sensible to be exposed in tupdesc.c. --- src/backend/access/common/tupdesc.c | 109 +--------------------------- src/backend/commands/tablecmds.c | 102 ++++++++++++++++++++++++++ src/include/access/tupdesc.h | 2 - src/include/commands/tablecmds.h | 2 + 4 files changed, 105 insertions(+), 110 deletions(-) diff --git a/src/backend/access/common/tupdesc.c b/src/backend/access/common/tupdesc.c index bc1b67e6c9..5d85831339 100644 --- a/src/backend/access/common/tupdesc.c +++ b/src/backend/access/common/tupdesc.c @@ -25,9 +25,6 @@ #include "catalog/pg_collation.h" #include "catalog/pg_type.h" #include "common/hashfn.h" -#include "miscadmin.h" -#include "parser/parse_type.h" -#include "utils/acl.h" #include "utils/builtins.h" #include "utils/datum.h" #include "utils/resowner_private.h" @@ -778,109 +775,6 @@ TupleDescInitEntryCollation(TupleDesc desc, TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid; } - -/* - * BuildDescForRelation - * - * Given a list of ColumnDef nodes, build a TupleDesc. - * - * Note: tdtypeid will need to be filled in later on. - */ -TupleDesc -BuildDescForRelation(const List *columns) -{ - int natts; - AttrNumber attnum; - ListCell *l; - TupleDesc desc; - bool has_not_null; - char *attname; - Oid atttypid; - int32 atttypmod; - Oid attcollation; - int attdim; - - /* - * allocate a new tuple descriptor - */ - natts = list_length(columns); - desc = CreateTemplateTupleDesc(natts); - has_not_null = false; - - attnum = 0; - - foreach(l, columns) - { - ColumnDef *entry = lfirst(l); - AclResult aclresult; - Form_pg_attribute att; - - /* - * for each entry in the list, get the name and type information from - * the list and have TupleDescInitEntry fill in the attribute - * information we need. - */ - attnum++; - - attname = entry->colname; - typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod); - - aclresult = object_aclcheck(TypeRelationId, atttypid, GetUserId(), ACL_USAGE); - if (aclresult != ACLCHECK_OK) - aclcheck_error_type(aclresult, atttypid); - - attcollation = GetColumnDefCollation(NULL, entry, atttypid); - attdim = list_length(entry->typeName->arrayBounds); - if (attdim > PG_INT16_MAX) - ereport(ERROR, - errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), - errmsg("too many array dimensions")); - - if (entry->typeName->setof) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("column \"%s\" cannot be declared SETOF", - attname))); - - TupleDescInitEntry(desc, attnum, attname, - atttypid, atttypmod, attdim); - att = TupleDescAttr(desc, attnum - 1); - - /* Override TupleDescInitEntry's settings as requested */ - TupleDescInitEntryCollation(desc, attnum, attcollation); - if (entry->storage) - att->attstorage = entry->storage; - - /* Fill in additional stuff not handled by TupleDescInitEntry */ - att->attnotnull = entry->is_not_null; - has_not_null |= entry->is_not_null; - att->attislocal = entry->is_local; - att->attinhcount = entry->inhcount; - att->attidentity = entry->identity; - att->attgenerated = entry->generated; - } - - if (has_not_null) - { - TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr)); - - constr->has_not_null = true; - constr->has_generated_stored = false; - constr->defval = NULL; - constr->missing = NULL; - constr->num_defval = 0; - constr->check = NULL; - constr->num_check = 0; - desc->constr = constr; - } - else - { - desc->constr = NULL; - } - - return desc; -} - /* * BuildDescFromLists * @@ -889,8 +783,7 @@ BuildDescForRelation(const List *columns) * * No constraints are generated. * - * This is essentially a cut-down version of BuildDescForRelation for use - * with functions returning RECORD. + * This is for use with functions returning RECORD. */ TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 06e4527dd1..2760b8b111 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1252,6 +1252,108 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, return address; } +/* + * BuildDescForRelation + * + * Given a list of ColumnDef nodes, build a TupleDesc. + * + * Note: tdtypeid will need to be filled in later on. + */ +TupleDesc +BuildDescForRelation(const List *columns) +{ + int natts; + AttrNumber attnum; + ListCell *l; + TupleDesc desc; + bool has_not_null; + char *attname; + Oid atttypid; + int32 atttypmod; + Oid attcollation; + int attdim; + + /* + * allocate a new tuple descriptor + */ + natts = list_length(columns); + desc = CreateTemplateTupleDesc(natts); + has_not_null = false; + + attnum = 0; + + foreach(l, columns) + { + ColumnDef *entry = lfirst(l); + AclResult aclresult; + Form_pg_attribute att; + + /* + * for each entry in the list, get the name and type information from + * the list and have TupleDescInitEntry fill in the attribute + * information we need. + */ + attnum++; + + attname = entry->colname; + typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod); + + aclresult = object_aclcheck(TypeRelationId, atttypid, GetUserId(), ACL_USAGE); + if (aclresult != ACLCHECK_OK) + aclcheck_error_type(aclresult, atttypid); + + attcollation = GetColumnDefCollation(NULL, entry, atttypid); + attdim = list_length(entry->typeName->arrayBounds); + if (attdim > PG_INT16_MAX) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many array dimensions")); + + if (entry->typeName->setof) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("column \"%s\" cannot be declared SETOF", + attname))); + + TupleDescInitEntry(desc, attnum, attname, + atttypid, atttypmod, attdim); + att = TupleDescAttr(desc, attnum - 1); + + /* Override TupleDescInitEntry's settings as requested */ + TupleDescInitEntryCollation(desc, attnum, attcollation); + if (entry->storage) + att->attstorage = entry->storage; + + /* Fill in additional stuff not handled by TupleDescInitEntry */ + att->attnotnull = entry->is_not_null; + has_not_null |= entry->is_not_null; + att->attislocal = entry->is_local; + att->attinhcount = entry->inhcount; + att->attidentity = entry->identity; + att->attgenerated = entry->generated; + } + + if (has_not_null) + { + TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr)); + + constr->has_not_null = true; + constr->has_generated_stored = false; + constr->defval = NULL; + constr->missing = NULL; + constr->num_defval = 0; + constr->check = NULL; + constr->num_check = 0; + desc->constr = constr; + } + else + { + desc->constr = NULL; + } + + return desc; +} + /* * Emit the right error or warning message for a "DROP" command issued on a * non-existent relation diff --git a/src/include/access/tupdesc.h b/src/include/access/tupdesc.h index f6cc28a661..f61c7cc784 100644 --- a/src/include/access/tupdesc.h +++ b/src/include/access/tupdesc.h @@ -147,8 +147,6 @@ extern void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid); -extern TupleDesc BuildDescForRelation(const List *columns); - extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations); #endif /* TUPDESC_H */ diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index 250d89ff88..8a8a9cd0ae 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -27,6 +27,8 @@ struct AlterTableUtilityContext; /* avoid including tcop/utility.h here */ extern ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ObjectAddress *typaddress, const char *queryString); +extern TupleDesc BuildDescForRelation(const List *columns); + extern void RemoveRelations(DropStmt *drop); extern Oid AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode); -- 2.41.0