Skip to content

Commit 58447e3

Browse files
committed
Add hint about not qualifying UPDATE...SET target with relation name.
Target columns in UPDATE ... SET must not be qualified with the target table; we disallow this because it'd create ambiguity about which name is the column name in case of field-qualified names. However, newbies have been seen to expect that they could qualify a target name just like other names. The error message when they do is confusing: "column "foo" of relation "foo" does not exist". To improve matters, issue a HINT if the invalid name is qualified and matches the relation's alias. James Coleman (editorialized a bit by me) Discussion: https://postgr.es/m/CAAaqYe8S2Qa060UV-YF5GoSd5PkEhLV94x-fEi3=TOtpaXCV+w@mail.gmail.com
1 parent 075df6b commit 58447e3

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

src/backend/parser/analyze.c

+3
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,9 @@ transformUpdateTargetList(ParseState *pstate, List *origTlist)
25182518
errmsg("column \"%s\" of relation \"%s\" does not exist",
25192519
origTarget->name,
25202520
RelationGetRelationName(pstate->p_target_relation)),
2521+
(origTarget->indirection != NIL &&
2522+
strcmp(origTarget->name, pstate->p_target_nsitem->p_names->aliasname) == 0) ?
2523+
errhint("SET target columns cannot be qualified with the relation name.") : 0,
25212524
parser_errposition(pstate, origTarget->location)));
25222525

25232526
updateTargetListEntry(pstate, tle, origTarget->name,

src/test/regress/expected/insert_conflict.out

+6
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ ERROR: invalid reference to FROM-clause entry for table "insertconflicttest"
272272
LINE 1: ...onfruit') on conflict (key) do update set fruit = insertconf...
273273
^
274274
HINT: Perhaps you meant to reference the table alias "ict".
275+
-- Check helpful hint when qualifying set column with target table
276+
insert into insertconflicttest values (3, 'Kiwi') on conflict (key, fruit) do update set insertconflicttest.fruit = 'Mango';
277+
ERROR: column "insertconflicttest" of relation "insertconflicttest" does not exist
278+
LINE 1: ...3, 'Kiwi') on conflict (key, fruit) do update set insertconf...
279+
^
280+
HINT: SET target columns cannot be qualified with the relation name.
275281
drop index key_index;
276282
--
277283
-- Composite key tests

src/test/regress/expected/update.out

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ SELECT * FROM update_test;
4444
10 | 20 |
4545
(2 rows)
4646

47+
-- error, you're not supposed to qualify the target column
48+
UPDATE update_test t SET t.b = t.b + 10 WHERE t.a = 10;
49+
ERROR: column "t" of relation "update_test" does not exist
50+
LINE 1: UPDATE update_test t SET t.b = t.b + 10 WHERE t.a = 10;
51+
^
52+
HINT: SET target columns cannot be qualified with the relation name.
4753
--
4854
-- Test VALUES in FROM
4955
--

src/test/regress/sql/insert_conflict.sql

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ insert into insertconflicttest AS ict values (6, 'Passionfruit') on conflict (ke
118118
insert into insertconflicttest AS ict values (6, 'Passionfruit') on conflict (key) do update set fruit = ict.fruit; -- ok, alias
119119
insert into insertconflicttest AS ict values (6, 'Passionfruit') on conflict (key) do update set fruit = insertconflicttest.fruit; -- error, references aliased away name
120120

121+
-- Check helpful hint when qualifying set column with target table
122+
insert into insertconflicttest values (3, 'Kiwi') on conflict (key, fruit) do update set insertconflicttest.fruit = 'Mango';
123+
121124
drop index key_index;
122125

123126
--

src/test/regress/sql/update.sql

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ UPDATE update_test t SET b = t.b + 10 WHERE t.a = 10;
3131

3232
SELECT * FROM update_test;
3333

34+
-- error, you're not supposed to qualify the target column
35+
UPDATE update_test t SET t.b = t.b + 10 WHERE t.a = 10;
36+
3437
--
3538
-- Test VALUES in FROM
3639
--

0 commit comments

Comments
 (0)