Skip to content

Commit 99cf29c

Browse files
peterepull[bot]
authored andcommitted
Add TupleDescGetDefault()
This unifies some repetitive code. Note: I didn't push the "not found" error message into the new function, even though all existing callers would be able to make use of it. Using the existing error handling as-is would probably require exposing the Relation type via tupdesc.h, which doesn't seem desirable. (Or even if we changed it to just report the OID, it would inject the concept of a relation containing the tuple descriptor into tupdesc.h, which might be a layering violation. Perhaps some further improvements could be considered here separately.) Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da%40eisentraut.org
1 parent 966866f commit 99cf29c

File tree

5 files changed

+32
-41
lines changed

5 files changed

+32
-41
lines changed

src/backend/access/common/tupdesc.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,28 @@ BuildDescFromLists(const List *names, const List *types, const List *typmods, co
927927

928928
return desc;
929929
}
930+
931+
/*
932+
* Get default expression (or NULL if none) for the given attribute number.
933+
*/
934+
Node *
935+
TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
936+
{
937+
Node *result = NULL;
938+
939+
if (tupdesc->constr)
940+
{
941+
AttrDefault *attrdef = tupdesc->constr->defval;
942+
943+
for (int i = 0; i < tupdesc->constr->num_defval; i++)
944+
{
945+
if (attrdef[i].adnum == attnum)
946+
{
947+
result = stringToNode(attrdef[i].adbin);
948+
break;
949+
}
950+
}
951+
}
952+
953+
return result;
954+
}

src/backend/commands/tablecmds.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,22 +2795,9 @@ MergeAttributes(List *columns, const List *supers, char relpersistence,
27952795
*/
27962796
if (attribute->atthasdef)
27972797
{
2798-
Node *this_default = NULL;
2798+
Node *this_default;
27992799

2800-
/* Find default in constraint structure */
2801-
if (constr != NULL)
2802-
{
2803-
AttrDefault *attrdef = constr->defval;
2804-
2805-
for (int i = 0; i < constr->num_defval; i++)
2806-
{
2807-
if (attrdef[i].adnum == parent_attno)
2808-
{
2809-
this_default = stringToNode(attrdef[i].adbin);
2810-
break;
2811-
}
2812-
}
2813-
}
2800+
this_default = TupleDescGetDefault(tupleDesc, parent_attno);
28142801
if (this_default == NULL)
28152802
elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
28162803
parent_attno, RelationGetRelationName(relation));

src/backend/parser/parse_utilcmd.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,20 +1358,11 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
13581358
(table_like_clause->options & CREATE_TABLE_LIKE_GENERATED) :
13591359
(table_like_clause->options & CREATE_TABLE_LIKE_DEFAULTS)))
13601360
{
1361-
Node *this_default = NULL;
1362-
AttrDefault *attrdef = constr->defval;
1361+
Node *this_default;
13631362
AlterTableCmd *atsubcmd;
13641363
bool found_whole_row;
13651364

1366-
/* Find default in constraint structure */
1367-
for (int i = 0; i < constr->num_defval; i++)
1368-
{
1369-
if (attrdef[i].adnum == parent_attno)
1370-
{
1371-
this_default = stringToNode(attrdef[i].adbin);
1372-
break;
1373-
}
1374-
}
1365+
this_default = TupleDescGetDefault(tupleDesc, parent_attno);
13751366
if (this_default == NULL)
13761367
elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
13771368
parent_attno, RelationGetRelationName(relation));

src/backend/rewrite/rewriteHandler.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,21 +1246,7 @@ build_column_default(Relation rel, int attrno)
12461246
*/
12471247
if (att_tup->atthasdef)
12481248
{
1249-
if (rd_att->constr && rd_att->constr->num_defval > 0)
1250-
{
1251-
AttrDefault *defval = rd_att->constr->defval;
1252-
int ndef = rd_att->constr->num_defval;
1253-
1254-
while (--ndef >= 0)
1255-
{
1256-
if (attrno == defval[ndef].adnum)
1257-
{
1258-
/* Found it, convert string representation to node tree. */
1259-
expr = stringToNode(defval[ndef].adbin);
1260-
break;
1261-
}
1262-
}
1263-
}
1249+
expr = TupleDescGetDefault(rd_att, attrno);
12641250
if (expr == NULL)
12651251
elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
12661252
attrno, RelationGetRelationName(rel));

src/include/access/tupdesc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,6 @@ extern TupleDesc BuildDescForRelation(const List *columns);
151151

152152
extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations);
153153

154+
extern Node *TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum);
155+
154156
#endif /* TUPDESC_H */

0 commit comments

Comments
 (0)