From b10cc5ee3904a1d7202da386362d26d79f39f8d5 Mon Sep 17 00:00:00 2001 From: Henson Choi Date: Fri, 2 Oct 2026 22:44:30 +0900 Subject: [PATCH v1 2/2] Keep real column names of relations not printed in FROM To keep the name of a merged column of an unnamed JOIN USING unique across the query, colname_is_unique() renames other columns of that name, for example to x_1, and prints a column alias list so that the new names are defined. A relation that is not printed in the FROM clause (the target of UPDATE, DELETE or MERGE, NEW and OLD of a rule) can have no column alias list, so its new names were defined nowhere. For example, this rule CREATE RULE ruletest_cu_r AS ON UPDATE TO ruletest_cu_a DO ALSO UPDATE ruletest_cu_t SET a = 1 FROM ruletest_cu_a ca FULL JOIN ruletest_cu_b cb USING (x) WHERE ruletest_cu_t.x = NEW.x AND ca.x = OLD.x; was printed as ... UPDATE ruletest_cu_t AS ruletest_cu_t SET a = 1 FROM (ruletest_cu_a ca FULL JOIN ruletest_cu_b cb USING (x)) WHERE ((ruletest_cu_t.x_1 = new.x_1) AND (ca.x = old.x_1)); and failed to read back: ERROR: column ruletest_cu_t.x_1 does not exist OLD and NEW of RETURNING read the target, so they were renamed with it, as in "RETURNING WITH (OLD AS o, NEW AS n) (o.x_1 + n.x_1)". The same happened to the target of DELETE ... USING and MERGE, as in "WHERE dt.x_1 = da.x" and "ON (mt.x_1 = ma.x)". The tests use UPDATE only, as all three take the same path. Keep the real names of the columns of such a relation, and skip the global comparison for it. Its columns are always qualified with the relation name, so they cannot be confused with the merged column. The rule above is now printed as written: ... UPDATE ruletest_cu_t SET a = 1 FROM (ruletest_cu_a ca FULL JOIN ruletest_cu_b cb USING (x)) WHERE ((ruletest_cu_t.x = new.x) AND (ca.x = old.x)); Backpatch-through: 15 --- src/backend/utils/adt/ruleutils.c | 35 +++++++++++++++++++++++------ src/test/regress/expected/rules.out | 30 ++++++++++++------------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index e9c1c9db1b1..427f3c4b446 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -207,7 +207,9 @@ typedef struct * can reference the input columns instead. That approach can fail for merged * JOIN USING columns, however, so when we have one of those in an unnamed * join, we have to make that column's alias globally unique across the whole - * query to ensure it can be referenced unambiguously. + * query to ensure it can be referenced unambiguously. The exception is the + * columns of an RTE that is not printed in the FROM clause: they keep their + * real names (see keep_real_names). * * Another problem is that a JOIN USING clause requires the columns to be * merged to have the same aliases in both input RTEs, and that no other @@ -272,6 +274,14 @@ typedef struct /* This flag tells whether we should actually print a column alias list */ bool printaliases; + /* + * This flag is set for an RTE that is not in the FROM clause (for + * example, a DML target or rule OLD and NEW). Such an RTE cannot have a + * column alias list, so its column names must stay the real ones. They + * are therefore exempt from the global uniqueness of USING column names. + */ + bool keep_real_names; + /* This list has all names used as USING names in joins above this RTE */ List *parentUsing; /* names assigned to parent merged columns */ @@ -4088,7 +4098,10 @@ set_deparse_for_query(deparse_namespace *dpns, Query *query, if (rte->rtekind == RTE_JOIN) set_join_column_names(dpns, rte, colinfo); else + { + colinfo->keep_real_names = !rte->inFromCl; set_relation_column_names(dpns, rte, colinfo); + } } } @@ -4138,7 +4151,10 @@ set_simple_column_names(deparse_namespace *dpns) * since that will prevent legal references to tables below the join.) * To ensure that every column in the query is unambiguously referenceable, * we must assign such merged columns names that are globally unique across - * the whole query, aliasing other columns out of the way as necessary. + * the whole query, aliasing other columns out of the way as necessary. (An + * RTE that is not printed in FROM, such as a DML target, is exempt: it can't + * have a column alias list, and its columns are always qualified with the + * RTE name, so it keeps its real column names.) * * Because the ensuing re-aliasing is fairly damaging to the readability of * the query, we don't do this unless we have to. So, we must pre-scan @@ -4909,14 +4925,19 @@ colname_is_unique(const char *colname, deparse_namespace *dpns, /* * Also check against USING-column names that must be globally unique. - * These are not hashed, but there should be few of them. + * These are not hashed, but there should be few of them. Skip this check + * for an RTE that is not printed in FROM: it cannot have a column alias + * list, so its column names must stay the real ones. */ - foreach(lc, dpns->using_names) + if (!colinfo->keep_real_names) { - char *oldname = (char *) lfirst(lc); + foreach(lc, dpns->using_names) + { + char *oldname = (char *) lfirst(lc); - if (strcmp(oldname, colname) == 0) - return false; + if (strcmp(oldname, colname) == 0) + return false; + } } return true; diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 3c35d8eff4b..537ce152a49 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -4165,13 +4165,13 @@ CREATE RULE ruletest_cu_r AS ON UPDATE TO ruletest_cu_a FROM ruletest_cu_a ca FULL JOIN ruletest_cu_b cb USING (x) WHERE ruletest_cu_t.x = NEW.x AND ca.x = OLD.x; SELECT pg_get_ruledef(oid) FROM pg_rewrite WHERE rulename = 'ruletest_cu_r'; - pg_get_ruledef -------------------------------------------------------------------------------------------- - CREATE RULE ruletest_cu_r AS + - ON UPDATE TO public.ruletest_cu_a DO UPDATE ruletest_cu_t AS ruletest_cu_t SET a = 1+ - FROM (ruletest_cu_a ca + - FULL JOIN ruletest_cu_b cb USING (x)) + - WHERE ((ruletest_cu_t.x_1 = new.x_1) AND (ca.x = old.x_1)); + pg_get_ruledef +-------------------------------------------------------------------------- + CREATE RULE ruletest_cu_r AS + + ON UPDATE TO public.ruletest_cu_a DO UPDATE ruletest_cu_t SET a = 1+ + FROM (ruletest_cu_a ca + + FULL JOIN ruletest_cu_b cb USING (x)) + + WHERE ((ruletest_cu_t.x = new.x) AND (ca.x = old.x)); (1 row) -- The same for OLD and NEW of RETURNING, which read the target. @@ -4181,14 +4181,14 @@ CREATE RULE ruletest_cu_ret AS ON DELETE TO ruletest_cu_a WHERE ruletest_cu_t.x = ca.x RETURNING WITH (OLD AS o, NEW AS n) o.x + n.x AS x; SELECT pg_get_ruledef(oid) FROM pg_rewrite WHERE rulename = 'ruletest_cu_ret'; - pg_get_ruledef ---------------------------------------------------------------------------------------------------- - CREATE RULE ruletest_cu_ret AS + - ON DELETE TO public.ruletest_cu_a DO INSTEAD UPDATE ruletest_cu_t AS ruletest_cu_t SET a = 1+ - FROM (ruletest_cu_a ca + - FULL JOIN ruletest_cu_b cb USING (x)) + - WHERE (ruletest_cu_t.x_1 = ca.x) + - RETURNING WITH (OLD AS o, NEW AS n) (o.x_1 + n.x_1) AS x; + pg_get_ruledef +---------------------------------------------------------------------------------- + CREATE RULE ruletest_cu_ret AS + + ON DELETE TO public.ruletest_cu_a DO INSTEAD UPDATE ruletest_cu_t SET a = 1+ + FROM (ruletest_cu_a ca + + FULL JOIN ruletest_cu_b cb USING (x)) + + WHERE (ruletest_cu_t.x = ca.x) + + RETURNING WITH (OLD AS o, NEW AS n) (o.x + n.x) AS x; (1 row) DROP TABLE ruletest_cu_a, ruletest_cu_b, ruletest_cu_t; -- 2.54.0 (Apple Git-157)