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

Commit b0ae295

Browse files
committed
MergeAttributes() and related variable renaming
Mainly, rename "schema" to "columns" and related changes. The previous naming has long been confusing. Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da%40eisentraut.org
1 parent 369202b commit b0ae295

File tree

3 files changed

+59
-64
lines changed

3 files changed

+59
-64
lines changed

src/backend/access/common/tupdesc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -782,12 +782,12 @@ TupleDescInitEntryCollation(TupleDesc desc,
782782
/*
783783
* BuildDescForRelation
784784
*
785-
* Given a relation schema (list of ColumnDef nodes), build a TupleDesc.
785+
* Given a list of ColumnDef nodes, build a TupleDesc.
786786
*
787787
* Note: tdtypeid will need to be filled in later on.
788788
*/
789789
TupleDesc
790-
BuildDescForRelation(List *schema)
790+
BuildDescForRelation(const List *columns)
791791
{
792792
int natts;
793793
AttrNumber attnum;
@@ -803,13 +803,13 @@ BuildDescForRelation(List *schema)
803803
/*
804804
* allocate a new tuple descriptor
805805
*/
806-
natts = list_length(schema);
806+
natts = list_length(columns);
807807
desc = CreateTemplateTupleDesc(natts);
808808
has_not_null = false;
809809

810810
attnum = 0;
811811

812-
foreach(l, schema)
812+
foreach(l, columns)
813813
{
814814
ColumnDef *entry = lfirst(l);
815815
AclResult aclresult;
@@ -891,7 +891,7 @@ BuildDescForRelation(List *schema)
891891
* with functions returning RECORD.
892892
*/
893893
TupleDesc
894-
BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
894+
BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
895895
{
896896
int natts;
897897
AttrNumber attnum;

src/backend/commands/tablecmds.c

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ static void truncate_check_perms(Oid relid, Form_pg_class reltuple);
350350
static void truncate_check_activity(Relation rel);
351351
static void RangeVarCallbackForTruncate(const RangeVar *relation,
352352
Oid relId, Oid oldRelId, void *arg);
353-
static List *MergeAttributes(List *schema, List *supers, char relpersistence,
353+
static List *MergeAttributes(List *columns, const List *supers, char relpersistence,
354354
bool is_partition, List **supconstr,
355355
List **supnotnulls);
356356
static List *MergeCheckConstraint(List *constraints, const char *name, Node *expr);
@@ -361,7 +361,7 @@ static void StoreCatalogInheritance(Oid relationId, List *supers,
361361
static void StoreCatalogInheritance1(Oid relationId, Oid parentOid,
362362
int32 seqNumber, Relation inhRelation,
363363
bool child_is_partition);
364-
static int findAttrByName(const char *attributeName, List *schema);
364+
static int findAttrByName(const char *attributeName, const List *columns);
365365
static void AlterIndexNamespaces(Relation classRel, Relation rel,
366366
Oid oldNspOid, Oid newNspOid, ObjectAddresses *objsMoved);
367367
static void AlterSeqNamespaces(Relation classRel, Relation rel,
@@ -2307,7 +2307,7 @@ storage_name(char c)
23072307
* Returns new schema given initial schema and superclasses.
23082308
*
23092309
* Input arguments:
2310-
* 'schema' is the column/attribute definition for the table. (It's a list
2310+
* 'columns' is the column/attribute definition for the table. (It's a list
23112311
* of ColumnDef's.) It is destructively changed.
23122312
* 'supers' is a list of OIDs of parent relations, already locked by caller.
23132313
* 'relpersistence' is the persistence type of the table.
@@ -2369,17 +2369,17 @@ storage_name(char c)
23692369
*----------
23702370
*/
23712371
static List *
2372-
MergeAttributes(List *schema, List *supers, char relpersistence,
2372+
MergeAttributes(List *columns, const List *supers, char relpersistence,
23732373
bool is_partition, List **supconstr, List **supnotnulls)
23742374
{
2375-
List *inhSchema = NIL;
2375+
List *inh_columns = NIL;
23762376
List *constraints = NIL;
23772377
List *nnconstraints = NIL;
23782378
bool have_bogus_defaults = false;
23792379
int child_attno;
23802380
static Node bogus_marker = {0}; /* marks conflicting defaults */
2381-
List *saved_schema = NIL;
2382-
ListCell *entry;
2381+
List *saved_columns = NIL;
2382+
ListCell *lc;
23832383

23842384
/*
23852385
* Check for and reject tables with too many columns. We perform this
@@ -2392,7 +2392,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
23922392
* Note that we also need to check that we do not exceed this figure after
23932393
* including columns from inherited relations.
23942394
*/
2395-
if (list_length(schema) > MaxHeapAttributeNumber)
2395+
if (list_length(columns) > MaxHeapAttributeNumber)
23962396
ereport(ERROR,
23972397
(errcode(ERRCODE_TOO_MANY_COLUMNS),
23982398
errmsg("tables can have at most %d columns",
@@ -2406,15 +2406,15 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
24062406
* sense to assume such conflicts are errors.
24072407
*
24082408
* We don't use foreach() here because we have two nested loops over the
2409-
* schema list, with possible element deletions in the inner one. If we
2409+
* columns list, with possible element deletions in the inner one. If we
24102410
* used foreach_delete_current() it could only fix up the state of one of
24112411
* the loops, so it seems cleaner to use looping over list indexes for
24122412
* both loops. Note that any deletion will happen beyond where the outer
24132413
* loop is, so its index never needs adjustment.
24142414
*/
2415-
for (int coldefpos = 0; coldefpos < list_length(schema); coldefpos++)
2415+
for (int coldefpos = 0; coldefpos < list_length(columns); coldefpos++)
24162416
{
2417-
ColumnDef *coldef = list_nth_node(ColumnDef, schema, coldefpos);
2417+
ColumnDef *coldef = list_nth_node(ColumnDef, columns, coldefpos);
24182418

24192419
if (!is_partition && coldef->typeName == NULL)
24202420
{
@@ -2431,9 +2431,9 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
24312431
}
24322432

24332433
/* restpos scans all entries beyond coldef; incr is in loop body */
2434-
for (int restpos = coldefpos + 1; restpos < list_length(schema);)
2434+
for (int restpos = coldefpos + 1; restpos < list_length(columns);)
24352435
{
2436-
ColumnDef *restdef = list_nth_node(ColumnDef, schema, restpos);
2436+
ColumnDef *restdef = list_nth_node(ColumnDef, columns, restpos);
24372437

24382438
if (strcmp(coldef->colname, restdef->colname) == 0)
24392439
{
@@ -2447,7 +2447,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
24472447
coldef->cooked_default = restdef->cooked_default;
24482448
coldef->constraints = restdef->constraints;
24492449
coldef->is_from_type = false;
2450-
schema = list_delete_nth_cell(schema, restpos);
2450+
columns = list_delete_nth_cell(columns, restpos);
24512451
}
24522452
else
24532453
ereport(ERROR,
@@ -2467,26 +2467,25 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
24672467
*/
24682468
if (is_partition)
24692469
{
2470-
saved_schema = schema;
2471-
schema = NIL;
2470+
saved_columns = columns;
2471+
columns = NIL;
24722472
}
24732473

24742474
/*
24752475
* Scan the parents left-to-right, and merge their attributes to form a
2476-
* list of inherited attributes (inhSchema).
2476+
* list of inherited columns (inh_columns).
24772477
*/
24782478
child_attno = 0;
2479-
foreach(entry, supers)
2479+
foreach(lc, supers)
24802480
{
2481-
Oid parent = lfirst_oid(entry);
2481+
Oid parent = lfirst_oid(lc);
24822482
Relation relation;
24832483
TupleDesc tupleDesc;
24842484
TupleConstr *constr;
24852485
AttrMap *newattmap;
24862486
List *inherited_defaults;
24872487
List *cols_with_defaults;
24882488
List *nnconstrs;
2489-
AttrNumber parent_attno;
24902489
ListCell *lc1;
24912490
ListCell *lc2;
24922491
Bitmapset *pkattrs;
@@ -2507,8 +2506,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
25072506
* We do not allow partitioned tables and partitions to participate in
25082507
* regular inheritance.
25092508
*/
2510-
if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
2511-
!is_partition)
2509+
if (relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE && !is_partition)
25122510
ereport(ERROR,
25132511
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
25142512
errmsg("cannot inherit from partitioned table \"%s\"",
@@ -2593,7 +2591,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
25932591
nncols = bms_add_member(nncols,
25942592
((CookedConstraint *) lfirst(lc1))->attnum);
25952593

2596-
for (parent_attno = 1; parent_attno <= tupleDesc->natts;
2594+
for (AttrNumber parent_attno = 1; parent_attno <= tupleDesc->natts;
25972595
parent_attno++)
25982596
{
25992597
Form_pg_attribute attribute = TupleDescAttr(tupleDesc,
@@ -2611,7 +2609,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
26112609
/*
26122610
* Does it conflict with some previously inherited column?
26132611
*/
2614-
exist_attno = findAttrByName(attributeName, inhSchema);
2612+
exist_attno = findAttrByName(attributeName, inh_columns);
26152613
if (exist_attno > 0)
26162614
{
26172615
Oid defTypeId;
@@ -2624,7 +2622,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
26242622
ereport(NOTICE,
26252623
(errmsg("merging multiple inherited definitions of column \"%s\"",
26262624
attributeName)));
2627-
def = (ColumnDef *) list_nth(inhSchema, exist_attno - 1);
2625+
def = (ColumnDef *) list_nth(inh_columns, exist_attno - 1);
26282626

26292627
/*
26302628
* Must have the same type and typmod
@@ -2761,7 +2759,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
27612759
if (CompressionMethodIsValid(attribute->attcompression))
27622760
def->compression =
27632761
pstrdup(GetCompressionMethodName(attribute->attcompression));
2764-
inhSchema = lappend(inhSchema, def);
2762+
inh_columns = lappend(inh_columns, def);
27652763
newattmap->attnums[parent_attno - 1] = ++child_attno;
27662764

27672765
/*
@@ -2862,7 +2860,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
28622860
* If we already had a default from some prior parent, check to
28632861
* see if they are the same. If so, no problem; if not, mark the
28642862
* column as having a bogus default. Below, we will complain if
2865-
* the bogus default isn't overridden by the child schema.
2863+
* the bogus default isn't overridden by the child columns.
28662864
*/
28672865
Assert(def->raw_default == NULL);
28682866
if (def->cooked_default == NULL)
@@ -2882,9 +2880,8 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
28822880
if (constr && constr->num_check > 0)
28832881
{
28842882
ConstrCheck *check = constr->check;
2885-
int i;
28862883

2887-
for (i = 0; i < constr->num_check; i++)
2884+
for (int i = 0; i < constr->num_check; i++)
28882885
{
28892886
char *name = check[i].ccname;
28902887
Node *expr;
@@ -2945,27 +2942,27 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
29452942
}
29462943

29472944
/*
2948-
* If we had no inherited attributes, the result schema is just the
2945+
* If we had no inherited attributes, the result columns are just the
29492946
* explicitly declared columns. Otherwise, we need to merge the declared
2950-
* columns into the inherited schema list. Although, we never have any
2947+
* columns into the inherited column list. Although, we never have any
29512948
* explicitly declared columns if the table is a partition.
29522949
*/
2953-
if (inhSchema != NIL)
2950+
if (inh_columns != NIL)
29542951
{
2955-
int schema_attno = 0;
2952+
int newcol_attno = 0;
29562953

2957-
foreach(entry, schema)
2954+
foreach(lc, columns)
29582955
{
2959-
ColumnDef *newdef = lfirst(entry);
2956+
ColumnDef *newdef = lfirst(lc);
29602957
char *attributeName = newdef->colname;
29612958
int exist_attno;
29622959

2963-
schema_attno++;
2960+
newcol_attno++;
29642961

29652962
/*
29662963
* Does it conflict with some previously inherited column?
29672964
*/
2968-
exist_attno = findAttrByName(attributeName, inhSchema);
2965+
exist_attno = findAttrByName(attributeName, inh_columns);
29692966
if (exist_attno > 0)
29702967
{
29712968
ColumnDef *def;
@@ -2985,15 +2982,15 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
29852982
/*
29862983
* Yes, try to merge the two column definitions.
29872984
*/
2988-
if (exist_attno == schema_attno)
2985+
if (exist_attno == newcol_attno)
29892986
ereport(NOTICE,
29902987
(errmsg("merging column \"%s\" with inherited definition",
29912988
attributeName)));
29922989
else
29932990
ereport(NOTICE,
29942991
(errmsg("moving and merging column \"%s\" with inherited definition", attributeName),
29952992
errdetail("User-specified column moved to the position of the inherited column.")));
2996-
def = (ColumnDef *) list_nth(inhSchema, exist_attno - 1);
2993+
def = (ColumnDef *) list_nth(inh_columns, exist_attno - 1);
29972994

29982995
/*
29992996
* Must have the same type and typmod
@@ -3118,19 +3115,19 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
31183115
else
31193116
{
31203117
/*
3121-
* No, attach new column to result schema
3118+
* No, attach new column to result columns
31223119
*/
3123-
inhSchema = lappend(inhSchema, newdef);
3120+
inh_columns = lappend(inh_columns, newdef);
31243121
}
31253122
}
31263123

3127-
schema = inhSchema;
3124+
columns = inh_columns;
31283125

31293126
/*
31303127
* Check that we haven't exceeded the legal # of columns after merging
31313128
* in inherited columns.
31323129
*/
3133-
if (list_length(schema) > MaxHeapAttributeNumber)
3130+
if (list_length(columns) > MaxHeapAttributeNumber)
31343131
ereport(ERROR,
31353132
(errcode(ERRCODE_TOO_MANY_COLUMNS),
31363133
errmsg("tables can have at most %d columns",
@@ -3144,13 +3141,13 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
31443141
*/
31453142
if (is_partition)
31463143
{
3147-
foreach(entry, saved_schema)
3144+
foreach(lc, saved_columns)
31483145
{
3149-
ColumnDef *restdef = lfirst(entry);
3146+
ColumnDef *restdef = lfirst(lc);
31503147
bool found = false;
31513148
ListCell *l;
31523149

3153-
foreach(l, schema)
3150+
foreach(l, columns)
31543151
{
31553152
ColumnDef *coldef = lfirst(l);
31563153

@@ -3222,9 +3219,9 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
32223219
*/
32233220
if (have_bogus_defaults)
32243221
{
3225-
foreach(entry, schema)
3222+
foreach(lc, columns)
32263223
{
3227-
ColumnDef *def = lfirst(entry);
3224+
ColumnDef *def = lfirst(lc);
32283225

32293226
if (def->cooked_default == &bogus_marker)
32303227
{
@@ -3247,7 +3244,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
32473244
*supconstr = constraints;
32483245
*supnotnulls = nnconstraints;
32493246

3250-
return schema;
3247+
return columns;
32513248
}
32523249

32533250

@@ -3402,22 +3399,20 @@ StoreCatalogInheritance1(Oid relationId, Oid parentOid,
34023399
}
34033400

34043401
/*
3405-
* Look for an existing schema entry with the given name.
3402+
* Look for an existing column entry with the given name.
34063403
*
3407-
* Returns the index (starting with 1) if attribute already exists in schema,
3404+
* Returns the index (starting with 1) if attribute already exists in columns,
34083405
* 0 if it doesn't.
34093406
*/
34103407
static int
3411-
findAttrByName(const char *attributeName, List *schema)
3408+
findAttrByName(const char *attributeName, const List *columns)
34123409
{
3413-
ListCell *s;
3410+
ListCell *lc;
34143411
int i = 1;
34153412

3416-
foreach(s, schema)
3413+
foreach(lc, columns)
34173414
{
3418-
ColumnDef *def = lfirst(s);
3419-
3420-
if (strcmp(attributeName, def->colname) == 0)
3415+
if (strcmp(attributeName, lfirst_node(ColumnDef, lc)->colname) == 0)
34213416
return i;
34223417

34233418
i++;

src/include/access/tupdesc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ extern void TupleDescInitEntryCollation(TupleDesc desc,
147147
AttrNumber attributeNumber,
148148
Oid collationid);
149149

150-
extern TupleDesc BuildDescForRelation(List *schema);
150+
extern TupleDesc BuildDescForRelation(const List *columns);
151151

152-
extern TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations);
152+
extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
153153

154154
#endif /* TUPDESC_H */

0 commit comments

Comments
 (0)