Hi hackers, When a function in FROM returns a named composite type that gains columns after a view is created, ruleutils.c prints a column alias list that reads back onto the wrong columns, if the range table entry has several functions (ROWS FROM) or WITH ORDINALITY, or is under a join with an alias list.
The problem
-----------
create table t1 (a int);
insert into t1 values (1);
create table t2 (b int);
insert into t2 values (2);
create function f1() returns setof t1
language sql as 'select * from t1';
create function f2() returns setof t2
language sql as 'select * from t2';
create view v as
select * from rows from (f1(), f2()) as r(m, n);
alter table t1 add column c int default 99;
select * from v; -- m = 1, n = 2
before: SELECT m, n FROM ROWS FROM(f1(), f2()) r(m, n);
created again, n is 99
after: SELECT m, n FROM ROWS FROM(f1(), f2()) r(m, c, n);
The alias list leaves out the columns added since parse time, but
when the text is read back they show up right after the other columns
of their function, so the aliases after them shift. Above, n names
the new column c.
With WITH ORDINALITY and no alias list written by the user, the new
column takes the name ordinality instead:
create view w as
select * from rows from (f1(), f2()) with ordinality;
alter table t2 add column d int;
before: ... WITH ORDINALITY f1(a, c, b, ordinality);
ERROR: column reference "ordinality" is ambiguous
after: ... WITH ORDINALITY f1(a, c, b, d, ordinality);
Tables are not affected, since their alias lists include the new
columns. Nor is a single function without WITH ORDINALITY, where
they come at the end, unless a join with an alias list is above it:
that list runs over the columns of all its inputs, so the inputs
after the function shift as well.
The fix
-------
0002 builds the alias list in the column order the text is read back
with, added columns included. Dropped columns are skipped, and the
new names are made unique like any other. Nothing changes when no
function has gained columns, and EXPLAIN is not affected. An
existing column whose name a new column took first is printed as, for
example, "ordinality_1 AS ordinality".
Tests
-----
Each case in rangefuncs creates a view, adds columns to the types,
creates the view again from pg_get_viewdef() and queries it:
- one function with ordinality
- the same, returning a domain over a composite type
- two functions without ordinality, the first example above
- two functions with ordinality, one of which had no columns, with a
new column taking the name of a later one and a dropped one among
them
- new columns after a dropped last column, with a name clashing
with an earlier one
The views are kept for the dump and restore comparison in the
pg_upgrade test. If that is more than wanted, the fourth case alone
covers most of the fix and could stay on its own.
Back branches
-------------
I think this should be back-patched to 15. The .patch files are for
master and also go to 18 and 19, with a trivial conflict on the two
allocations. The REL_17 .txt files are for 15-17, which have no
column name hash; on 17 the loop can use foreach_ptr as on master.
On 15 the expected output needs regenerating, as views there print
qualified column names.
This is one of the three ruleutils.c fixes I said in the row pattern
recognition thread [1] I would send on their own, and I have posted
a patch there that takes it out of the series [2]. The other two
follow in separate threads.
[1]
https://postgr.es/m/CAAAe_zCb9847vcoaH72wvk61O-xOH_OSzYHt40TH37xjz=k...@mail.gmail.com
[2]
https://postgr.es/m/CAAAe_zA7eFMiRRJAgHoaQ1S6YZWmMz7P=83kxnavg1jvq%[email protected]
Best regards,
Henson
From 4e4115a70247e055f82cbf68eb6300ee5d0218d6 Mon Sep 17 00:00:00 2001 From: Henson Choi <[email protected]> Date: Thu, 22 Oct 2026 13:39:03 +0900 Subject: [PATCH v1 1/2] Add a test for columns added to function result types after parse The column alias list of a function RTE is printed from the columns that the RTE had when the query was parsed. Columns added to the composite type that a function returns show up when the text is read back, in the middle of the columns when the RTE has an ordinality column or more than one function, so the alias list is applied to the wrong columns: FROM ROWS FROM(rngfunc_f0(), rngfunc_f1()) WITH ORDINALITY rngfunc_f0(a, ordinality) Read back, this gives the new column x the name a, and a the name ordinality ("column reference "ordinality" is ambiguous"). The expected output shows the behavior before the fix. The next commit fixes it and updates the output. --- src/test/regress/expected/rangefuncs.out | 159 +++++++++++++++++++++++ src/test/regress/sql/rangefuncs.sql | 93 +++++++++++++ 2 files changed, 252 insertions(+) diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.out index 397a8b35d6d..fd804189599 100644 --- a/src/test/regress/expected/rangefuncs.out +++ b/src/test/regress/expected/rangefuncs.out @@ -2501,3 +2501,162 @@ DETAIL: Returned type integer at ordinal position 3, but query expects double p select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail ERROR: function return row and query-specified return row do not match DETAIL: Returned row contains 1 attribute, but query expects 2. +-- Columns added to the composite types that functions return show up in the +-- middle of the columns of the RTE, so the column alias list has to make room +-- for them +create type rngfunc_t0 as (a int); +create function rngfunc_f0() returns rngfunc_t0 + language sql as 'select null::rngfunc_t0'; +create view rngfunc_one_v as + select * from rngfunc_f0() with ordinality; +alter type rngfunc_t0 add attribute x int; +select format('create view rngfunc_one_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_one_v'::regclass), ';')) +\gexec +create view rngfunc_one_v2 as SELECT a, + ordinality + FROM rngfunc_f0() WITH ORDINALITY rngfunc_f0(a, ordinality) +ERROR: column reference "ordinality" is ambiguous +LINE 2: ordinality + ^ +select * from rngfunc_one_v2; +ERROR: relation "rngfunc_one_v2" does not exist +LINE 1: select * from rngfunc_one_v2; + ^ +drop view rngfunc_one_v; +-- The same for a function that returns a domain over a composite type +create type rngfunc_t1 as (a int); +create domain rngfunc_d1 as rngfunc_t1; +create function rngfunc_f1() returns rngfunc_d1 + language sql as 'select null::rngfunc_d1'; +create view rngfunc_dom_v as + select * from rngfunc_f1() with ordinality; +alter type rngfunc_t1 add attribute x int; +select format('create view rngfunc_dom_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_dom_v'::regclass), ';')) +\gexec +create view rngfunc_dom_v2 as SELECT a, + ordinality + FROM rngfunc_f1() WITH ORDINALITY rngfunc_f1(a, ordinality) +ERROR: column reference "ordinality" is ambiguous +LINE 2: ordinality + ^ +select * from rngfunc_dom_v2; +ERROR: relation "rngfunc_dom_v2" does not exist +LINE 1: select * from rngfunc_dom_v2; + ^ +drop view rngfunc_dom_v; +-- Without ordinality, the aliases after the new columns of a function shift +create type rngfunc_t2 as (a int); +create type rngfunc_t3 as (b int); +create function rngfunc_f2() returns rngfunc_t2 + language sql as 'select 1'; +create function rngfunc_f3() returns rngfunc_t3 + language sql as 'select 2'; +create view rngfunc_alias_v as + select * from rows from (rngfunc_f2(), rngfunc_f3()) as r(m, n); +alter type rngfunc_t2 add attribute c int; +create or replace function rngfunc_f2() returns rngfunc_t2 + language sql as 'select 1, 99'; +select * from rngfunc_alias_v; + m | n +---+--- + 1 | 2 +(1 row) + +select format('create view rngfunc_alias_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_alias_v'::regclass), ';')) +\gexec +create view rngfunc_alias_v2 as SELECT m, + n + FROM ROWS FROM(rngfunc_f2(), rngfunc_f3()) r(m, n) +select * from rngfunc_alias_v2; + m | n +---+---- + 1 | 99 +(1 row) + +drop view rngfunc_alias_v; +-- With both, for a function that had no columns and a column named ordinality +create type rngfunc_t4 as (); +create type rngfunc_t5 as (a int); +create function rngfunc_f4() returns rngfunc_t4 + language sql as 'select row()::rngfunc_t4'; +create function rngfunc_f5() returns rngfunc_t5 + language sql as 'select 1'; +create view rngfunc_grown_v as + select * from rows from (rngfunc_f4(), rngfunc_f5()) with ordinality; +select * from rngfunc_grown_v; + a | ordinality +---+------------ + 1 | 1 +(1 row) + +alter type rngfunc_t4 add attribute d int; +alter type rngfunc_t4 add attribute x int; +alter type rngfunc_t4 drop attribute d; +alter type rngfunc_t5 add attribute ordinality int; +create or replace function rngfunc_f4() returns rngfunc_t4 + language sql as 'select 10'; +create or replace function rngfunc_f5() returns rngfunc_t5 + language sql as 'select 1, 2'; +select * from rngfunc_grown_v; + a | ordinality +---+------------ + 1 | 1 +(1 row) + +select * from rows from (rngfunc_f4(), rngfunc_f5()) with ordinality; + x | a | ordinality | ordinality +----+---+------------+------------ + 10 | 1 | 2 | 1 +(1 row) + +select pg_get_viewdef('rngfunc_grown_v'::regclass); + pg_get_viewdef +------------------------------------------------------------------------------------------ + SELECT a, + + ordinality + + FROM ROWS FROM(rngfunc_f4(), rngfunc_f5()) WITH ORDINALITY rngfunc_f4(a, ordinality); +(1 row) + +select format('create view rngfunc_grown_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_grown_v'::regclass), ';')) +\gexec +create view rngfunc_grown_v2 as SELECT a, + ordinality + FROM ROWS FROM(rngfunc_f4(), rngfunc_f5()) WITH ORDINALITY rngfunc_f4(a, ordinality) +ERROR: column reference "ordinality" is ambiguous +LINE 2: ordinality + ^ +select * from rngfunc_grown_v2; +ERROR: relation "rngfunc_grown_v2" does not exist +LINE 1: select * from rngfunc_grown_v2; + ^ +drop view rngfunc_grown_v; +-- New columns after a dropped last column, renamed away from an earlier one +create type rngfunc_t6 as (a int, b int); +create type rngfunc_t7 as (); +create function rngfunc_f6() returns rngfunc_t6 + language sql as 'select null::rngfunc_t6'; +create function rngfunc_f7() returns rngfunc_t7 + language sql as 'select null::rngfunc_t7'; +create view rngfunc_drop_v as + select a, ordinality + from rows from (rngfunc_f6(), rngfunc_f7()) with ordinality; +alter type rngfunc_t6 drop attribute b; +alter type rngfunc_t7 add attribute a int; +select format('create view rngfunc_drop_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_drop_v'::regclass), ';')) +\gexec +create view rngfunc_drop_v2 as SELECT a, + ordinality + FROM ROWS FROM(rngfunc_f6(), rngfunc_f7()) WITH ORDINALITY rngfunc_f6(a, ordinality) +ERROR: column reference "ordinality" is ambiguous +LINE 2: ordinality + ^ +select * from rngfunc_drop_v2; +ERROR: relation "rngfunc_drop_v2" does not exist +LINE 1: select * from rngfunc_drop_v2; + ^ +drop view rngfunc_drop_v; diff --git a/src/test/regress/sql/rangefuncs.sql b/src/test/regress/sql/rangefuncs.sql index 3c47c98e113..3a64e135013 100644 --- a/src/test/regress/sql/rangefuncs.sql +++ b/src/test/regress/sql/rangefuncs.sql @@ -825,3 +825,96 @@ select * from a, coalesce(b) as c(d int, e int, f int, g int); -- fail with a(b) as (values (row(1,2,3))) select * from a, coalesce(b) as c(d int, e int, f float); -- fail select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail + +-- Columns added to the composite types that functions return show up in the +-- middle of the columns of the RTE, so the column alias list has to make room +-- for them +create type rngfunc_t0 as (a int); +create function rngfunc_f0() returns rngfunc_t0 + language sql as 'select null::rngfunc_t0'; +create view rngfunc_one_v as + select * from rngfunc_f0() with ordinality; +alter type rngfunc_t0 add attribute x int; +select format('create view rngfunc_one_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_one_v'::regclass), ';')) +\gexec +select * from rngfunc_one_v2; +drop view rngfunc_one_v; + +-- The same for a function that returns a domain over a composite type +create type rngfunc_t1 as (a int); +create domain rngfunc_d1 as rngfunc_t1; +create function rngfunc_f1() returns rngfunc_d1 + language sql as 'select null::rngfunc_d1'; +create view rngfunc_dom_v as + select * from rngfunc_f1() with ordinality; +alter type rngfunc_t1 add attribute x int; +select format('create view rngfunc_dom_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_dom_v'::regclass), ';')) +\gexec +select * from rngfunc_dom_v2; +drop view rngfunc_dom_v; + +-- Without ordinality, the aliases after the new columns of a function shift +create type rngfunc_t2 as (a int); +create type rngfunc_t3 as (b int); +create function rngfunc_f2() returns rngfunc_t2 + language sql as 'select 1'; +create function rngfunc_f3() returns rngfunc_t3 + language sql as 'select 2'; +create view rngfunc_alias_v as + select * from rows from (rngfunc_f2(), rngfunc_f3()) as r(m, n); +alter type rngfunc_t2 add attribute c int; +create or replace function rngfunc_f2() returns rngfunc_t2 + language sql as 'select 1, 99'; +select * from rngfunc_alias_v; +select format('create view rngfunc_alias_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_alias_v'::regclass), ';')) +\gexec +select * from rngfunc_alias_v2; +drop view rngfunc_alias_v; + +-- With both, for a function that had no columns and a column named ordinality +create type rngfunc_t4 as (); +create type rngfunc_t5 as (a int); +create function rngfunc_f4() returns rngfunc_t4 + language sql as 'select row()::rngfunc_t4'; +create function rngfunc_f5() returns rngfunc_t5 + language sql as 'select 1'; +create view rngfunc_grown_v as + select * from rows from (rngfunc_f4(), rngfunc_f5()) with ordinality; +select * from rngfunc_grown_v; +alter type rngfunc_t4 add attribute d int; +alter type rngfunc_t4 add attribute x int; +alter type rngfunc_t4 drop attribute d; +alter type rngfunc_t5 add attribute ordinality int; +create or replace function rngfunc_f4() returns rngfunc_t4 + language sql as 'select 10'; +create or replace function rngfunc_f5() returns rngfunc_t5 + language sql as 'select 1, 2'; +select * from rngfunc_grown_v; +select * from rows from (rngfunc_f4(), rngfunc_f5()) with ordinality; +select pg_get_viewdef('rngfunc_grown_v'::regclass); +select format('create view rngfunc_grown_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_grown_v'::regclass), ';')) +\gexec +select * from rngfunc_grown_v2; +drop view rngfunc_grown_v; + +-- New columns after a dropped last column, renamed away from an earlier one +create type rngfunc_t6 as (a int, b int); +create type rngfunc_t7 as (); +create function rngfunc_f6() returns rngfunc_t6 + language sql as 'select null::rngfunc_t6'; +create function rngfunc_f7() returns rngfunc_t7 + language sql as 'select null::rngfunc_t7'; +create view rngfunc_drop_v as + select a, ordinality + from rows from (rngfunc_f6(), rngfunc_f7()) with ordinality; +alter type rngfunc_t6 drop attribute b; +alter type rngfunc_t7 add attribute a int; +select format('create view rngfunc_drop_v2 as %s', + rtrim(pg_get_viewdef('rngfunc_drop_v'::regclass), ';')) +\gexec +select * from rngfunc_drop_v2; +drop view rngfunc_drop_v; -- 2.54.0 (Apple Git-157)
v1-0001-Add-a-test-for-columns-added-to-function-result-t.patch
Description: Binary data
v1-0002-Fix-alias-lists-of-function-RTEs-whose-composite-.patch
Description: Binary data
From 4f791796328e6e5c9d958a8666353559f097edf2 Mon Sep 17 00:00:00 2001 From: Henson Choi <[email protected]> Date: Fri, 2 Oct 2026 22:48:31 +0900 Subject: [PATCH v1 2/2] Fix alias lists of function RTEs whose composite type gained columns expandRTE() reports as many columns of a function that returns a composite type as the type had when the query was parsed, but a reparsed query sees the columns added since, after the other columns of their function, so in the middle of the columns of the RTE when it has several functions or an ordinality column. The column alias list printed from the parse-time columns was therefore applied to the wrong columns, or was too short. Find out how many columns each function has now, and put the names of the new ones in new_colnames[] right after the last parse-time column of their function, or first for leading functions that had no columns. Backpatch-through: 15 --- src/backend/utils/adt/ruleutils.c | 168 +++++++++++++++++++---- src/test/regress/expected/rangefuncs.out | 79 +++++------ src/test/regress/sql/rangefuncs.sql | 7 +- 3 files changed, 174 insertions(+), 80 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 2d3625c9b70..3052d430960 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -366,6 +366,10 @@ static void set_simple_column_names(deparse_namespace *dpns); static bool has_dangerous_join_using(deparse_namespace *dpns, Node *jtnode); static void set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing); +static void add_function_new_colnames(List *funcnewcols, List *funcendcols, + int endcol, int *next_new_col, + deparse_namespace *dpns, + deparse_columns *colinfo); static void set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, deparse_columns *colinfo); @@ -4325,6 +4329,47 @@ set_using_names(deparse_namespace *dpns, Node *jtnode, List *parentUsing) (int) nodeTag(jtnode)); } +/* + * add_function_new_colnames: append to new_colnames[] the columns that + * functions have gained since parse time, for the functions whose parse-time + * columns end at column number endcol of the RTE (0 for leading functions + * that had no columns). Such columns have no varattno, so they appear only + * in new_colnames[]. *next_new_col is the next free slot there; it is + * advanced. + */ +static void +add_function_new_colnames(List *funcnewcols, List *funcendcols, int endcol, + int *next_new_col, deparse_namespace *dpns, + deparse_columns *colinfo) +{ + ListCell *lcn; + ListCell *lce; + ListCell *lc; + + forboth(lcn, funcnewcols, lce, funcendcols) + { + if (lfirst_int(lce) != endcol) + continue; + foreach(lc, (List *) lfirst(lcn)) + { + char *newname = (char *) lfirst(lc); + + newname = make_colname_unique(newname, dpns, colinfo); + colinfo->new_colnames[*next_new_col] = newname; + colinfo->is_new_col[*next_new_col] = true; + (*next_new_col)++; + + /* + * Without a names hash, colname_is_unique finds a name that is + * not in colnames[], such as this one, only by scanning + * new_colnames[] up to num_new_cols. The entries before + * *next_new_col are all filled in. + */ + colinfo->num_new_cols = *next_new_col; + } + } +} + /* * set_relation_column_names: select column aliases for a non-join RTE * @@ -4342,6 +4387,9 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, int noldcolumns; int i; int j; + List *funcnewcols = NIL; /* per function: new column names */ + List *funcendcols = NIL; /* per function: parse-time end column */ + int nfuncnew = 0; /* * Construct an array of the current "real" column names of the RTE. @@ -4394,9 +4442,49 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, */ if (rte->rtekind == RTE_FUNCTION && rte->functions != NIL) { + int endcol = 0; + /* Since we're not creating Vars, rtindex etc. don't matter */ expandRTE(rte, 1, 0, -1, true /* include dropped */ , &colnames, NULL); + + /* + * expandRTE() reports only as many columns of a function that + * returns a composite type as there were at parse time, but a + * reparsed query would see the columns added since, in the middle + * of the RTE's columns if there are several functions or an + * ordinality column. Find out which functions got new columns, + * and where they go. + */ + foreach(lc, rte->functions) + { + RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc); + TupleDesc tupdesc; + TypeFuncClass functypclass; + List *newcols = NIL; + + functypclass = get_expr_result_type(rtfunc->funcexpr, NULL, + &tupdesc); + if (functypclass == TYPEFUNC_COMPOSITE || + functypclass == TYPEFUNC_COMPOSITE_DOMAIN) + { + Assert(tupdesc); + for (i = rtfunc->funccolcount; i < tupdesc->natts; i++) + { + Form_pg_attribute attr = TupleDescAttr(tupdesc, i); + + if (!attr->attisdropped) + { + newcols = lappend(newcols, + pstrdup(NameStr(attr->attname))); + nfuncnew++; + } + } + } + endcol += rtfunc->funccolcount; + funcnewcols = lappend(funcnewcols, newcols); + funcendcols = lappend_int(funcendcols, endcol); + } } else colnames = rte->eref->colnames; @@ -4434,23 +4522,33 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, /* * Make sufficiently large new_colnames and is_new_col arrays, too. * - * Note: because we leave colinfo->num_new_cols zero until after the loop, - * colname_is_unique will not consult that array, which is fine because it - * would only be duplicate effort. + * Note: we leave colinfo->num_new_cols zero until after the loop, except + * that add_function_new_colnames() advances it, because the names that + * function adds are only in new_colnames[]. For the other columns, + * colname_is_unique need not consult that array: their names are in + * colnames[] as well. */ - colinfo->new_colnames = (char **) palloc(ncolumns * sizeof(char *)); - colinfo->is_new_col = (bool *) palloc(ncolumns * sizeof(bool)); + colinfo->new_colnames = (char **) palloc((ncolumns + nfuncnew) * + sizeof(char *)); + colinfo->is_new_col = (bool *) palloc((ncolumns + nfuncnew) * + sizeof(bool)); /* * Scan the columns, select a unique alias for each one, and store it in * colinfo->colnames and colinfo->new_colnames. The former array has NULL * entries for dropped columns, the latter omits them. Also mark * new_colnames entries as to whether they are new since parse time; this - * is the case for entries beyond the length of rte->eref->colnames. + * is the case for entries beyond the length of rte->eref->colnames, and + * for the columns that a function has got since parse time (which + * add_function_new_colnames() adds, possibly in the middle). */ noldcolumns = list_length(rte->eref->colnames); changed_any = false; j = 0; + + /* New columns of leading functions that had no columns come first */ + add_function_new_colnames(funcnewcols, funcendcols, 0, &j, dpns, colinfo); + for (i = 0; i < ncolumns; i++) { char *real_colname = real_colnames[i]; @@ -4460,40 +4558,50 @@ set_relation_column_names(deparse_namespace *dpns, RangeTblEntry *rte, if (real_colname == NULL) { Assert(colname == NULL); /* colnames[i] is already NULL */ - continue; } - - /* If alias already assigned, that's what to use */ - if (colname == NULL) + else { - /* If user wrote an alias, prefer that over real column name */ - if (rte->alias && i < list_length(rte->alias->colnames)) - colname = strVal(list_nth(rte->alias->colnames, i)); - else - colname = real_colname; + /* If alias already assigned, that's what to use */ + if (colname == NULL) + { + /* If user wrote an alias, prefer that over real column name */ + if (rte->alias && i < list_length(rte->alias->colnames)) + colname = strVal(list_nth(rte->alias->colnames, i)); + else + colname = real_colname; - /* Unique-ify and insert into colinfo */ - colname = make_colname_unique(colname, dpns, colinfo); + /* Unique-ify and insert into colinfo */ + colname = make_colname_unique(colname, dpns, colinfo); - colinfo->colnames[i] = colname; - } + colinfo->colnames[i] = colname; + } - /* Put names of non-dropped columns in new_colnames[] too */ - colinfo->new_colnames[j] = colname; - /* And mark them as new or not */ - colinfo->is_new_col[j] = (i >= noldcolumns); - j++; + /* Put names of non-dropped columns in new_colnames[] too */ + colinfo->new_colnames[j] = colname; + /* And mark them as new or not */ + colinfo->is_new_col[j] = (i >= noldcolumns); + j++; - /* Remember if any assigned aliases differ from "real" name */ - if (!changed_any && strcmp(colname, real_colname) != 0) - changed_any = true; + /* Remember if any assigned aliases differ from "real" name */ + if (!changed_any && strcmp(colname, real_colname) != 0) + changed_any = true; + } + + /* + * If this is the last parse-time column of a function that has got + * new columns, they come right after it. + */ + add_function_new_colnames(funcnewcols, funcendcols, i + 1, &j, + dpns, colinfo); } /* * Set correct length for new_colnames[] array. (Note: if columns have - * been added, colinfo->num_cols includes them, which is not really quite - * right but is harmless, since any new columns must be at the end where - * they won't affect varattnos of pre-existing columns.) + * been added to a relation, colinfo->num_cols includes them, which is not + * really quite right but is harmless, since any new columns must be at + * the end where they won't affect varattnos of pre-existing columns. The + * columns that a function has got since parse time are not counted in + * num_cols; they have no varattno.) */ colinfo->num_new_cols = j; diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.out index fd804189599..cc3cef6d8d6 100644 --- a/src/test/regress/expected/rangefuncs.out +++ b/src/test/regress/expected/rangefuncs.out @@ -2503,7 +2503,7 @@ ERROR: function return row and query-specified return row do not match DETAIL: Returned row contains 1 attribute, but query expects 2. -- Columns added to the composite types that functions return show up in the -- middle of the columns of the RTE, so the column alias list has to make room --- for them +-- for them; leave the objects around for the pg_upgrade test create type rngfunc_t0 as (a int); create function rngfunc_f0() returns rngfunc_t0 language sql as 'select null::rngfunc_t0'; @@ -2515,15 +2515,13 @@ select format('create view rngfunc_one_v2 as %s', \gexec create view rngfunc_one_v2 as SELECT a, ordinality - FROM rngfunc_f0() WITH ORDINALITY rngfunc_f0(a, ordinality) -ERROR: column reference "ordinality" is ambiguous -LINE 2: ordinality - ^ + FROM rngfunc_f0() WITH ORDINALITY rngfunc_f0(a, x, ordinality) select * from rngfunc_one_v2; -ERROR: relation "rngfunc_one_v2" does not exist -LINE 1: select * from rngfunc_one_v2; - ^ -drop view rngfunc_one_v; + a | ordinality +---+------------ + | 1 +(1 row) + -- The same for a function that returns a domain over a composite type create type rngfunc_t1 as (a int); create domain rngfunc_d1 as rngfunc_t1; @@ -2537,15 +2535,13 @@ select format('create view rngfunc_dom_v2 as %s', \gexec create view rngfunc_dom_v2 as SELECT a, ordinality - FROM rngfunc_f1() WITH ORDINALITY rngfunc_f1(a, ordinality) -ERROR: column reference "ordinality" is ambiguous -LINE 2: ordinality - ^ + FROM rngfunc_f1() WITH ORDINALITY rngfunc_f1(a, x, ordinality) select * from rngfunc_dom_v2; -ERROR: relation "rngfunc_dom_v2" does not exist -LINE 1: select * from rngfunc_dom_v2; - ^ -drop view rngfunc_dom_v; + a | ordinality +---+------------ + | 1 +(1 row) + -- Without ordinality, the aliases after the new columns of a function shift create type rngfunc_t2 as (a int); create type rngfunc_t3 as (b int); @@ -2569,14 +2565,13 @@ select format('create view rngfunc_alias_v2 as %s', \gexec create view rngfunc_alias_v2 as SELECT m, n - FROM ROWS FROM(rngfunc_f2(), rngfunc_f3()) r(m, n) + FROM ROWS FROM(rngfunc_f2(), rngfunc_f3()) r(m, c, n) select * from rngfunc_alias_v2; - m | n ----+---- - 1 | 99 + m | n +---+--- + 1 | 2 (1 row) -drop view rngfunc_alias_v; -- With both, for a function that had no columns and a column named ordinality create type rngfunc_t4 as (); create type rngfunc_t5 as (a int); @@ -2613,27 +2608,25 @@ select * from rows from (rngfunc_f4(), rngfunc_f5()) with ordinality; (1 row) select pg_get_viewdef('rngfunc_grown_v'::regclass); - pg_get_viewdef ------------------------------------------------------------------------------------------- - SELECT a, + - ordinality + - FROM ROWS FROM(rngfunc_f4(), rngfunc_f5()) WITH ORDINALITY rngfunc_f4(a, ordinality); + pg_get_viewdef +----------------------------------------------------------------------------------------------------------- + SELECT a, + + ordinality_1 AS ordinality + + FROM ROWS FROM(rngfunc_f4(), rngfunc_f5()) WITH ORDINALITY rngfunc_f4(x, a, ordinality, ordinality_1); (1 row) select format('create view rngfunc_grown_v2 as %s', rtrim(pg_get_viewdef('rngfunc_grown_v'::regclass), ';')) \gexec create view rngfunc_grown_v2 as SELECT a, - ordinality - FROM ROWS FROM(rngfunc_f4(), rngfunc_f5()) WITH ORDINALITY rngfunc_f4(a, ordinality) -ERROR: column reference "ordinality" is ambiguous -LINE 2: ordinality - ^ + ordinality_1 AS ordinality + FROM ROWS FROM(rngfunc_f4(), rngfunc_f5()) WITH ORDINALITY rngfunc_f4(x, a, ordinality, ordinality_1) select * from rngfunc_grown_v2; -ERROR: relation "rngfunc_grown_v2" does not exist -LINE 1: select * from rngfunc_grown_v2; - ^ -drop view rngfunc_grown_v; + a | ordinality +---+------------ + 1 | 1 +(1 row) + -- New columns after a dropped last column, renamed away from an earlier one create type rngfunc_t6 as (a int, b int); create type rngfunc_t7 as (); @@ -2651,12 +2644,10 @@ select format('create view rngfunc_drop_v2 as %s', \gexec create view rngfunc_drop_v2 as SELECT a, ordinality - FROM ROWS FROM(rngfunc_f6(), rngfunc_f7()) WITH ORDINALITY rngfunc_f6(a, ordinality) -ERROR: column reference "ordinality" is ambiguous -LINE 2: ordinality - ^ + FROM ROWS FROM(rngfunc_f6(), rngfunc_f7()) WITH ORDINALITY rngfunc_f6(a, a_1, ordinality) select * from rngfunc_drop_v2; -ERROR: relation "rngfunc_drop_v2" does not exist -LINE 1: select * from rngfunc_drop_v2; - ^ -drop view rngfunc_drop_v; + a | ordinality +---+------------ + | 1 +(1 row) + diff --git a/src/test/regress/sql/rangefuncs.sql b/src/test/regress/sql/rangefuncs.sql index 3a64e135013..920c061e519 100644 --- a/src/test/regress/sql/rangefuncs.sql +++ b/src/test/regress/sql/rangefuncs.sql @@ -828,7 +828,7 @@ select * from int8_tbl, coalesce(row(1)) as (a int, b int); -- fail -- Columns added to the composite types that functions return show up in the -- middle of the columns of the RTE, so the column alias list has to make room --- for them +-- for them; leave the objects around for the pg_upgrade test create type rngfunc_t0 as (a int); create function rngfunc_f0() returns rngfunc_t0 language sql as 'select null::rngfunc_t0'; @@ -839,7 +839,6 @@ select format('create view rngfunc_one_v2 as %s', rtrim(pg_get_viewdef('rngfunc_one_v'::regclass), ';')) \gexec select * from rngfunc_one_v2; -drop view rngfunc_one_v; -- The same for a function that returns a domain over a composite type create type rngfunc_t1 as (a int); @@ -853,7 +852,6 @@ select format('create view rngfunc_dom_v2 as %s', rtrim(pg_get_viewdef('rngfunc_dom_v'::regclass), ';')) \gexec select * from rngfunc_dom_v2; -drop view rngfunc_dom_v; -- Without ordinality, the aliases after the new columns of a function shift create type rngfunc_t2 as (a int); @@ -872,7 +870,6 @@ select format('create view rngfunc_alias_v2 as %s', rtrim(pg_get_viewdef('rngfunc_alias_v'::regclass), ';')) \gexec select * from rngfunc_alias_v2; -drop view rngfunc_alias_v; -- With both, for a function that had no columns and a column named ordinality create type rngfunc_t4 as (); @@ -899,7 +896,6 @@ select format('create view rngfunc_grown_v2 as %s', rtrim(pg_get_viewdef('rngfunc_grown_v'::regclass), ';')) \gexec select * from rngfunc_grown_v2; -drop view rngfunc_grown_v; -- New columns after a dropped last column, renamed away from an earlier one create type rngfunc_t6 as (a int, b int); @@ -917,4 +913,3 @@ select format('create view rngfunc_drop_v2 as %s', rtrim(pg_get_viewdef('rngfunc_drop_v'::regclass), ';')) \gexec select * from rngfunc_drop_v2; -drop view rngfunc_drop_v; -- 2.54.0 (Apple Git-157)
