From ec1d40af2d1b890970c416df7a411b4c0af5c288 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 22 Jun 2023 15:21:17 +0200 Subject: [PATCH 15/17] 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. --- src/backend/access/common/tupdesc.c | 25 +++++++++++++++++++++++++ src/backend/commands/tablecmds.c | 17 ++--------------- src/backend/parser/parse_utilcmd.c | 13 ++----------- src/backend/rewrite/rewriteHandler.c | 16 +--------------- src/include/access/tupdesc.h | 2 ++ 5 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/backend/access/common/tupdesc.c b/src/backend/access/common/tupdesc.c index 5d85831339..d119cfafb5 100644 --- a/src/backend/access/common/tupdesc.c +++ b/src/backend/access/common/tupdesc.c @@ -822,3 +822,28 @@ BuildDescFromLists(const List *names, const List *types, const List *typmods, co return desc; } + +/* + * Get default expression (or NULL if none) for the given attribute number. + */ +Node * +TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum) +{ + Node *result = NULL; + + if (tupdesc->constr) + { + AttrDefault *attrdef = tupdesc->constr->defval; + + for (int i = 0; i < tupdesc->constr->num_defval; i++) + { + if (attrdef[i].adnum == attnum) + { + result = stringToNode(attrdef[i].adbin); + break; + } + } + } + + return result; +} diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c1be6be826..bb0fb8b9af 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2785,22 +2785,9 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, */ if (attribute->atthasdef) { - Node *this_default = NULL; + Node *this_default; - /* Find default in constraint structure */ - if (constr != NULL) - { - AttrDefault *attrdef = constr->defval; - - for (int i = 0; i < constr->num_defval; i++) - { - if (attrdef[i].adnum == parent_attno) - { - this_default = stringToNode(attrdef[i].adbin); - break; - } - } - } + this_default = TupleDescGetDefault(tupleDesc, parent_attno); if (this_default == NULL) elog(ERROR, "default expression not found for attribute %d of relation \"%s\"", parent_attno, RelationGetRelationName(relation)); diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 53420306ec..9f80ac40f2 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1248,20 +1248,11 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause) (table_like_clause->options & CREATE_TABLE_LIKE_GENERATED) : (table_like_clause->options & CREATE_TABLE_LIKE_DEFAULTS))) { - Node *this_default = NULL; - AttrDefault *attrdef = constr->defval; + Node *this_default; AlterTableCmd *atsubcmd; bool found_whole_row; - /* Find default in constraint structure */ - for (int i = 0; i < constr->num_defval; i++) - { - if (attrdef[i].adnum == parent_attno) - { - this_default = stringToNode(attrdef[i].adbin); - break; - } - } + this_default = TupleDescGetDefault(tupleDesc, parent_attno); if (this_default == NULL) elog(ERROR, "default expression not found for attribute %d of relation \"%s\"", parent_attno, RelationGetRelationName(relation)); diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index b486ab559a..41a362310a 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -1246,21 +1246,7 @@ build_column_default(Relation rel, int attrno) */ if (att_tup->atthasdef) { - if (rd_att->constr && rd_att->constr->num_defval > 0) - { - AttrDefault *defval = rd_att->constr->defval; - int ndef = rd_att->constr->num_defval; - - while (--ndef >= 0) - { - if (attrno == defval[ndef].adnum) - { - /* Found it, convert string representation to node tree. */ - expr = stringToNode(defval[ndef].adbin); - break; - } - } - } + expr = TupleDescGetDefault(rd_att, attrno); if (expr == NULL) elog(ERROR, "default expression not found for attribute %d of relation \"%s\"", attrno, RelationGetRelationName(rel)); diff --git a/src/include/access/tupdesc.h b/src/include/access/tupdesc.h index f61c7cc784..d833d5f2e1 100644 --- a/src/include/access/tupdesc.h +++ b/src/include/access/tupdesc.h @@ -149,4 +149,6 @@ extern void TupleDescInitEntryCollation(TupleDesc desc, extern TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations); +extern Node *TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum); + #endif /* TUPDESC_H */ -- 2.41.0