Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 04e4852

Browse files
committed
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. Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da@eisentraut.org
1 parent 6d34140 commit 04e4852

File tree

4 files changed

+105
-110
lines changed

4 files changed

+105
-110
lines changed

src/backend/access/common/tupdesc.c

+1-108
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525
#include "catalog/pg_collation.h"
2626
#include "catalog/pg_type.h"
2727
#include "common/hashfn.h"
28-
#include "miscadmin.h"
29-
#include "parser/parse_type.h"
30-
#include "utils/acl.h"
3128
#include "utils/builtins.h"
3229
#include "utils/datum.h"
3330
#include "utils/resowner_private.h"
@@ -778,109 +775,6 @@ TupleDescInitEntryCollation(TupleDesc desc,
778775
TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
779776
}
780777

781-
782-
/*
783-
* BuildDescForRelation
784-
*
785-
* Given a list of ColumnDef nodes, build a TupleDesc.
786-
*
787-
* Note: tdtypeid will need to be filled in later on.
788-
*/
789-
TupleDesc
790-
BuildDescForRelation(const List *columns)
791-
{
792-
int natts;
793-
AttrNumber attnum;
794-
ListCell *l;
795-
TupleDesc desc;
796-
bool has_not_null;
797-
char *attname;
798-
Oid atttypid;
799-
int32 atttypmod;
800-
Oid attcollation;
801-
int attdim;
802-
803-
/*
804-
* allocate a new tuple descriptor
805-
*/
806-
natts = list_length(columns);
807-
desc = CreateTemplateTupleDesc(natts);
808-
has_not_null = false;
809-
810-
attnum = 0;
811-
812-
foreach(l, columns)
813-
{
814-
ColumnDef *entry = lfirst(l);
815-
AclResult aclresult;
816-
Form_pg_attribute att;
817-
818-
/*
819-
* for each entry in the list, get the name and type information from
820-
* the list and have TupleDescInitEntry fill in the attribute
821-
* information we need.
822-
*/
823-
attnum++;
824-
825-
attname = entry->colname;
826-
typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod);
827-
828-
aclresult = object_aclcheck(TypeRelationId, atttypid, GetUserId(), ACL_USAGE);
829-
if (aclresult != ACLCHECK_OK)
830-
aclcheck_error_type(aclresult, atttypid);
831-
832-
attcollation = GetColumnDefCollation(NULL, entry, atttypid);
833-
attdim = list_length(entry->typeName->arrayBounds);
834-
if (attdim > PG_INT16_MAX)
835-
ereport(ERROR,
836-
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
837-
errmsg("too many array dimensions"));
838-
839-
if (entry->typeName->setof)
840-
ereport(ERROR,
841-
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
842-
errmsg("column \"%s\" cannot be declared SETOF",
843-
attname)));
844-
845-
TupleDescInitEntry(desc, attnum, attname,
846-
atttypid, atttypmod, attdim);
847-
att = TupleDescAttr(desc, attnum - 1);
848-
849-
/* Override TupleDescInitEntry's settings as requested */
850-
TupleDescInitEntryCollation(desc, attnum, attcollation);
851-
if (entry->storage)
852-
att->attstorage = entry->storage;
853-
854-
/* Fill in additional stuff not handled by TupleDescInitEntry */
855-
att->attnotnull = entry->is_not_null;
856-
has_not_null |= entry->is_not_null;
857-
att->attislocal = entry->is_local;
858-
att->attinhcount = entry->inhcount;
859-
att->attidentity = entry->identity;
860-
att->attgenerated = entry->generated;
861-
}
862-
863-
if (has_not_null)
864-
{
865-
TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
866-
867-
constr->has_not_null = true;
868-
constr->has_generated_stored = false;
869-
constr->defval = NULL;
870-
constr->missing = NULL;
871-
constr->num_defval = 0;
872-
constr->check = NULL;
873-
constr->num_check = 0;
874-
desc->constr = constr;
875-
}
876-
else
877-
{
878-
desc->constr = NULL;
879-
}
880-
881-
return desc;
882-
}
883-
884778
/*
885779
* BuildDescFromLists
886780
*
@@ -889,8 +783,7 @@ BuildDescForRelation(const List *columns)
889783
*
890784
* No constraints are generated.
891785
*
892-
* This is essentially a cut-down version of BuildDescForRelation for use
893-
* with functions returning RECORD.
786+
* This is for use with functions returning RECORD.
894787
*/
895788
TupleDesc
896789
BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)

src/backend/commands/tablecmds.c

+102
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,108 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
12771277
return address;
12781278
}
12791279

1280+
/*
1281+
* BuildDescForRelation
1282+
*
1283+
* Given a list of ColumnDef nodes, build a TupleDesc.
1284+
*
1285+
* Note: tdtypeid will need to be filled in later on.
1286+
*/
1287+
TupleDesc
1288+
BuildDescForRelation(const List *columns)
1289+
{
1290+
int natts;
1291+
AttrNumber attnum;
1292+
ListCell *l;
1293+
TupleDesc desc;
1294+
bool has_not_null;
1295+
char *attname;
1296+
Oid atttypid;
1297+
int32 atttypmod;
1298+
Oid attcollation;
1299+
int attdim;
1300+
1301+
/*
1302+
* allocate a new tuple descriptor
1303+
*/
1304+
natts = list_length(columns);
1305+
desc = CreateTemplateTupleDesc(natts);
1306+
has_not_null = false;
1307+
1308+
attnum = 0;
1309+
1310+
foreach(l, columns)
1311+
{
1312+
ColumnDef *entry = lfirst(l);
1313+
AclResult aclresult;
1314+
Form_pg_attribute att;
1315+
1316+
/*
1317+
* for each entry in the list, get the name and type information from
1318+
* the list and have TupleDescInitEntry fill in the attribute
1319+
* information we need.
1320+
*/
1321+
attnum++;
1322+
1323+
attname = entry->colname;
1324+
typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod);
1325+
1326+
aclresult = object_aclcheck(TypeRelationId, atttypid, GetUserId(), ACL_USAGE);
1327+
if (aclresult != ACLCHECK_OK)
1328+
aclcheck_error_type(aclresult, atttypid);
1329+
1330+
attcollation = GetColumnDefCollation(NULL, entry, atttypid);
1331+
attdim = list_length(entry->typeName->arrayBounds);
1332+
if (attdim > PG_INT16_MAX)
1333+
ereport(ERROR,
1334+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1335+
errmsg("too many array dimensions"));
1336+
1337+
if (entry->typeName->setof)
1338+
ereport(ERROR,
1339+
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
1340+
errmsg("column \"%s\" cannot be declared SETOF",
1341+
attname)));
1342+
1343+
TupleDescInitEntry(desc, attnum, attname,
1344+
atttypid, atttypmod, attdim);
1345+
att = TupleDescAttr(desc, attnum - 1);
1346+
1347+
/* Override TupleDescInitEntry's settings as requested */
1348+
TupleDescInitEntryCollation(desc, attnum, attcollation);
1349+
if (entry->storage)
1350+
att->attstorage = entry->storage;
1351+
1352+
/* Fill in additional stuff not handled by TupleDescInitEntry */
1353+
att->attnotnull = entry->is_not_null;
1354+
has_not_null |= entry->is_not_null;
1355+
att->attislocal = entry->is_local;
1356+
att->attinhcount = entry->inhcount;
1357+
att->attidentity = entry->identity;
1358+
att->attgenerated = entry->generated;
1359+
}
1360+
1361+
if (has_not_null)
1362+
{
1363+
TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
1364+
1365+
constr->has_not_null = true;
1366+
constr->has_generated_stored = false;
1367+
constr->defval = NULL;
1368+
constr->missing = NULL;
1369+
constr->num_defval = 0;
1370+
constr->check = NULL;
1371+
constr->num_check = 0;
1372+
desc->constr = constr;
1373+
}
1374+
else
1375+
{
1376+
desc->constr = NULL;
1377+
}
1378+
1379+
return desc;
1380+
}
1381+
12801382
/*
12811383
* Emit the right error or warning message for a "DROP" command issued on a
12821384
* non-existent relation

src/include/access/tupdesc.h

-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ extern void TupleDescInitEntryCollation(TupleDesc desc,
147147
AttrNumber attributeNumber,
148148
Oid collationid);
149149

150-
extern TupleDesc BuildDescForRelation(const List *columns);
151-
152150
extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
153151

154152
extern Node *TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum);

src/include/commands/tablecmds.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct AlterTableUtilityContext; /* avoid including tcop/utility.h here */
2727
extern ObjectAddress DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
2828
ObjectAddress *typaddress, const char *queryString);
2929

30+
extern TupleDesc BuildDescForRelation(const List *columns);
31+
3032
extern void RemoveRelations(DropStmt *drop);
3133

3234
extern Oid AlterTableLookupRelation(AlterTableStmt *stmt, LOCKMODE lockmode);

0 commit comments

Comments
 (0)