From ebf76f2753a91615d45f113f1535a8443fa8d076 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 27 Sep 2023 18:52:40 +0100 Subject: 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 --- src/backend/commands/tablecmds.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'src/backend/commands/tablecmds.c') diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index ad398e837d5..73b8dea81c8 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2795,22 +2795,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)); -- cgit v1.2.3