1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-31 22:04:40 +03:00

Fix parsing of qualified relation names in RETURNING.

Given a qualified refname, refnameNamespaceItem() will search for a
matching namespace item by relation OID, rather than by name. Commit
80feb727c8 broke this by adding additional namespace items for OLD and
NEW in the RETURNING list, which have the same relation OID, causing
ambiguity. Fix this by ignoring these in the search, which is correct
since they don't match the qualified relation name, and so there is no
real ambiguity.

Reported by Richard Guo.

Discussion: https://postgr.es/m/CAMbWs49MBjWYWDROJ8MZ%3DY%2B4UgRQa10wzik1tWrD5yto9eoGXg%40mail.gmail.com
This commit is contained in:
Dean Rasheed
2025-01-17 10:35:07 +00:00
parent e24d77080b
commit 43830ecb8a
3 changed files with 23 additions and 1 deletions

View File

@ -125,7 +125,10 @@ static bool isQueryUsingTempRelation_walker(Node *node, void *context);
* that (a) has no alias and (b) is for the same relation identified by * that (a) has no alias and (b) is for the same relation identified by
* schemaname.refname. In this case we convert schemaname.refname to a * schemaname.refname. In this case we convert schemaname.refname to a
* relation OID and search by relid, rather than by alias name. This is * relation OID and search by relid, rather than by alias name. This is
* peculiar, but it's what SQL says to do. * peculiar, but it's what SQL says to do. While processing a query's
* RETURNING list, there may be additional namespace items for OLD and NEW,
* with the same relation OID as the target namespace item. These are
* ignored in the search, since they don't match by schemaname.refname.
*/ */
ParseNamespaceItem * ParseNamespaceItem *
refnameNamespaceItem(ParseState *pstate, refnameNamespaceItem(ParseState *pstate,
@ -255,6 +258,9 @@ scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location)
/* If not inside LATERAL, ignore lateral-only items */ /* If not inside LATERAL, ignore lateral-only items */
if (nsitem->p_lateral_only && !pstate->p_lateral_active) if (nsitem->p_lateral_only && !pstate->p_lateral_active)
continue; continue;
/* Ignore OLD/NEW namespace items that can appear in RETURNING */
if (nsitem->p_returning_type != VAR_RETURNING_DEFAULT)
continue;
/* yes, the test for alias == NULL should be there... */ /* yes, the test for alias == NULL should be there... */
if (rte->rtekind == RTE_RELATION && if (rte->rtekind == RTE_RELATION &&

View File

@ -766,6 +766,16 @@ DELETE FROM zerocol
(1 row) (1 row)
DROP TABLE zerocol; DROP TABLE zerocol;
-- Test schema-qualified table name in RETURNING list
CREATE TABLE public.tt(a int, b int);
INSERT INTO public.tt VALUES (1, 10);
UPDATE public.tt SET b = b * 2 RETURNING a, b, old.b, new.b, tt.b, public.tt.b;
a | b | b | b | b | b
---+----+----+----+----+----
1 | 20 | 10 | 20 | 20 | 20
(1 row)
DROP TABLE public.tt;
-- Test cross-partition updates and attribute mapping -- Test cross-partition updates and attribute mapping
CREATE TABLE foo_parted (a int, b float8, c text) PARTITION BY LIST (a); CREATE TABLE foo_parted (a int, b float8, c text) PARTITION BY LIST (a);
CREATE TABLE foo_part_s1 PARTITION OF foo_parted FOR VALUES IN (1); CREATE TABLE foo_part_s1 PARTITION OF foo_parted FOR VALUES IN (1);

View File

@ -312,6 +312,12 @@ DELETE FROM zerocol
new.tableoid::regclass, new.ctid, ctid, *; new.tableoid::regclass, new.ctid, ctid, *;
DROP TABLE zerocol; DROP TABLE zerocol;
-- Test schema-qualified table name in RETURNING list
CREATE TABLE public.tt(a int, b int);
INSERT INTO public.tt VALUES (1, 10);
UPDATE public.tt SET b = b * 2 RETURNING a, b, old.b, new.b, tt.b, public.tt.b;
DROP TABLE public.tt;
-- Test cross-partition updates and attribute mapping -- Test cross-partition updates and attribute mapping
CREATE TABLE foo_parted (a int, b float8, c text) PARTITION BY LIST (a); CREATE TABLE foo_parted (a int, b float8, c text) PARTITION BY LIST (a);
CREATE TABLE foo_part_s1 PARTITION OF foo_parted FOR VALUES IN (1); CREATE TABLE foo_part_s1 PARTITION OF foo_parted FOR VALUES IN (1);