Skip to content

Commit c8a5f16

Browse files
committed
Print the correct aliases for DML target tables in ruleutils.
ruleutils.c blindly printed the user-given alias (or nothing if there hadn't been one) for the target table of INSERT/UPDATE/DELETE queries. That works a large percentage of the time, but not always: for queries appearing in WITH, it's possible that we chose a different alias to avoid conflict with outer-scope names. Since the chosen alias would be used in any Var references to the target table, this'd lead to an inconsistent printout with consequences such as dump/restore failures. The correct logic for printing (or not) a relation alias was embedded in get_from_clause_item. Factor it out to a separate function so that we don't need a jointree node to use it. (Only a limited part of that function can be reached from these new call sites, but this seems like the cleanest non-duplicative factorization.) In passing, I got rid of a redundant "\d+ rules_src" step in rules.sql. Initial report from Jonathan Katz; thanks to Vignesh C for analysis. This has been broken for a long time, so back-patch to all supported branches. Discussion: https://postgr.es/m/e947fa21-24b2-f922-375a-d4f763ef3e4b@postgresql.org Discussion: https://postgr.es/m/CALDaNm1MMntjmT_NJGp-Z=xbF02qHGAyuSHfYHias3TqQbPF2w@mail.gmail.com
1 parent 5d8ec1b commit c8a5f16

File tree

3 files changed

+126
-80
lines changed

3 files changed

+126
-80
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 84 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ static void get_from_clause(Query *query, const char *prefix,
475475
deparse_context *context);
476476
static void get_from_clause_item(Node *jtnode, Query *query,
477477
deparse_context *context);
478+
static void get_rte_alias(RangeTblEntry *rte, int varno, bool use_as,
479+
deparse_context *context);
478480
static void get_column_alias_list(deparse_columns *colinfo,
479481
deparse_context *context);
480482
static void get_from_clause_coldeflist(RangeTblFunction *rtfunc,
@@ -6658,12 +6660,14 @@ get_insert_query_def(Query *query, deparse_context *context,
66586660
context->indentLevel += PRETTYINDENT_STD;
66596661
appendStringInfoChar(buf, ' ');
66606662
}
6661-
appendStringInfo(buf, "INSERT INTO %s ",
6663+
appendStringInfo(buf, "INSERT INTO %s",
66626664
generate_relation_name(rte->relid, NIL));
6663-
/* INSERT requires AS keyword for target alias */
6664-
if (rte->alias != NULL)
6665-
appendStringInfo(buf, "AS %s ",
6666-
quote_identifier(rte->alias->aliasname));
6665+
6666+
/* Print the relation alias, if needed; INSERT requires explicit AS */
6667+
get_rte_alias(rte, query->resultRelation, true, context);
6668+
6669+
/* always want a space here */
6670+
appendStringInfoChar(buf, ' ');
66676671

66686672
/*
66696673
* Add the insert-column-names list. Any indirection decoration needed on
@@ -6845,9 +6849,10 @@ get_update_query_def(Query *query, deparse_context *context,
68456849
appendStringInfo(buf, "UPDATE %s%s",
68466850
only_marker(rte),
68476851
generate_relation_name(rte->relid, NIL));
6848-
if (rte->alias != NULL)
6849-
appendStringInfo(buf, " %s",
6850-
quote_identifier(rte->alias->aliasname));
6852+
6853+
/* Print the relation alias, if needed */
6854+
get_rte_alias(rte, query->resultRelation, false, context);
6855+
68516856
appendStringInfoString(buf, " SET ");
68526857

68536858
/* Deparse targetlist */
@@ -7053,9 +7058,9 @@ get_delete_query_def(Query *query, deparse_context *context,
70537058
appendStringInfo(buf, "DELETE FROM %s%s",
70547059
only_marker(rte),
70557060
generate_relation_name(rte->relid, NIL));
7056-
if (rte->alias != NULL)
7057-
appendStringInfo(buf, " %s",
7058-
quote_identifier(rte->alias->aliasname));
7061+
7062+
/* Print the relation alias, if needed */
7063+
get_rte_alias(rte, query->resultRelation, false, context);
70597064

70607065
/* Add the USING clause if given */
70617066
get_from_clause(query, " USING ", context);
@@ -10916,10 +10921,8 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1091610921
{
1091710922
int varno = ((RangeTblRef *) jtnode)->rtindex;
1091810923
RangeTblEntry *rte = rt_fetch(varno, query->rtable);
10919-
char *refname = get_rtable_name(varno, context);
1092010924
deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
1092110925
RangeTblFunction *rtfunc1 = NULL;
10922-
bool printalias;
1092310926

1092410927
if (rte->lateral)
1092510928
appendStringInfoString(buf, "LATERAL ");
@@ -11056,55 +11059,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1105611059
}
1105711060

1105811061
/* Print the relation alias, if needed */
11059-
printalias = false;
11060-
if (rte->alias != NULL)
11061-
{
11062-
/* Always print alias if user provided one */
11063-
printalias = true;
11064-
}
11065-
else if (colinfo->printaliases)
11066-
{
11067-
/* Always print alias if we need to print column aliases */
11068-
printalias = true;
11069-
}
11070-
else if (rte->rtekind == RTE_RELATION)
11071-
{
11072-
/*
11073-
* No need to print alias if it's same as relation name (this
11074-
* would normally be the case, but not if set_rtable_names had to
11075-
* resolve a conflict).
11076-
*/
11077-
if (strcmp(refname, get_relation_name(rte->relid)) != 0)
11078-
printalias = true;
11079-
}
11080-
else if (rte->rtekind == RTE_FUNCTION)
11081-
{
11082-
/*
11083-
* For a function RTE, always print alias. This covers possible
11084-
* renaming of the function and/or instability of the
11085-
* FigureColname rules for things that aren't simple functions.
11086-
* Note we'd need to force it anyway for the columndef list case.
11087-
*/
11088-
printalias = true;
11089-
}
11090-
else if (rte->rtekind == RTE_SUBQUERY ||
11091-
rte->rtekind == RTE_VALUES)
11092-
{
11093-
/* Alias is syntactically required for SUBQUERY and VALUES */
11094-
printalias = true;
11095-
}
11096-
else if (rte->rtekind == RTE_CTE)
11097-
{
11098-
/*
11099-
* No need to print alias if it's same as CTE name (this would
11100-
* normally be the case, but not if set_rtable_names had to
11101-
* resolve a conflict).
11102-
*/
11103-
if (strcmp(refname, rte->ctename) != 0)
11104-
printalias = true;
11105-
}
11106-
if (printalias)
11107-
appendStringInfo(buf, " %s", quote_identifier(refname));
11062+
get_rte_alias(rte, varno, false, context);
1110811063

1110911064
/* Print the column definitions or aliases, if needed */
1111011065
if (rtfunc1 && rtfunc1->funccolnames != NIL)
@@ -11242,6 +11197,73 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1124211197
(int) nodeTag(jtnode));
1124311198
}
1124411199

11200+
/*
11201+
* get_rte_alias - print the relation's alias, if needed
11202+
*
11203+
* If printed, the alias is preceded by a space, or by " AS " if use_as is true.
11204+
*/
11205+
static void
11206+
get_rte_alias(RangeTblEntry *rte, int varno, bool use_as,
11207+
deparse_context *context)
11208+
{
11209+
deparse_namespace *dpns = (deparse_namespace *) linitial(context->namespaces);
11210+
char *refname = get_rtable_name(varno, context);
11211+
deparse_columns *colinfo = deparse_columns_fetch(varno, dpns);
11212+
bool printalias = false;
11213+
11214+
if (rte->alias != NULL)
11215+
{
11216+
/* Always print alias if user provided one */
11217+
printalias = true;
11218+
}
11219+
else if (colinfo->printaliases)
11220+
{
11221+
/* Always print alias if we need to print column aliases */
11222+
printalias = true;
11223+
}
11224+
else if (rte->rtekind == RTE_RELATION)
11225+
{
11226+
/*
11227+
* No need to print alias if it's same as relation name (this would
11228+
* normally be the case, but not if set_rtable_names had to resolve a
11229+
* conflict).
11230+
*/
11231+
if (strcmp(refname, get_relation_name(rte->relid)) != 0)
11232+
printalias = true;
11233+
}
11234+
else if (rte->rtekind == RTE_FUNCTION)
11235+
{
11236+
/*
11237+
* For a function RTE, always print alias. This covers possible
11238+
* renaming of the function and/or instability of the FigureColname
11239+
* rules for things that aren't simple functions. Note we'd need to
11240+
* force it anyway for the columndef list case.
11241+
*/
11242+
printalias = true;
11243+
}
11244+
else if (rte->rtekind == RTE_SUBQUERY ||
11245+
rte->rtekind == RTE_VALUES)
11246+
{
11247+
/* Alias is syntactically required for SUBQUERY and VALUES */
11248+
printalias = true;
11249+
}
11250+
else if (rte->rtekind == RTE_CTE)
11251+
{
11252+
/*
11253+
* No need to print alias if it's same as CTE name (this would
11254+
* normally be the case, but not if set_rtable_names had to resolve a
11255+
* conflict).
11256+
*/
11257+
if (strcmp(refname, rte->ctename) != 0)
11258+
printalias = true;
11259+
}
11260+
11261+
if (printalias)
11262+
appendStringInfo(context->buf, "%s%s",
11263+
use_as ? " AS " : " ",
11264+
quote_identifier(refname));
11265+
}
11266+
1124511267
/*
1124611268
* get_column_alias_list - print column alias list for an RTE
1124711269
*

src/test/regress/expected/rules.out

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,28 +2998,21 @@ select * from rules_log;
29982998
(16 rows)
29992999

30003000
create rule r4 as on delete to rules_src do notify rules_src_deletion;
3001-
\d+ rules_src
3002-
Table "public.rules_src"
3003-
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
3004-
--------+---------+-----------+----------+---------+---------+--------------+-------------
3005-
f1 | integer | | | | plain | |
3006-
f2 | integer | | | 0 | plain | |
3007-
Rules:
3008-
r1 AS
3009-
ON UPDATE TO rules_src DO INSERT INTO rules_log (f1, f2, tag, id) VALUES (old.f1,old.f2,'old'::text,DEFAULT), (new.f1,new.f2,'new'::text,DEFAULT)
3010-
r2 AS
3011-
ON UPDATE TO rules_src DO VALUES (old.f1,old.f2,'old'::text), (new.f1,new.f2,'new'::text)
3012-
r3 AS
3013-
ON INSERT TO rules_src DO INSERT INTO rules_log (f1, f2, tag, id) VALUES (NULL::integer,NULL::integer,'-'::text,DEFAULT), (new.f1,new.f2,'new'::text,DEFAULT)
3014-
r4 AS
3015-
ON DELETE TO rules_src DO
3016-
NOTIFY rules_src_deletion
3017-
30183001
--
30193002
-- Ensure an aliased target relation for insert is correctly deparsed.
30203003
--
30213004
create rule r5 as on insert to rules_src do instead insert into rules_log AS trgt SELECT NEW.* RETURNING trgt.f1, trgt.f2;
30223005
create rule r6 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
3006+
--
3007+
-- Check deparse disambiguation of INSERT/UPDATE/DELETE targets.
3008+
--
3009+
create rule r7 as on delete to rules_src do instead
3010+
with wins as (insert into int4_tbl as trgt values (0) returning *),
3011+
wupd as (update int4_tbl trgt set f1 = f1+1 returning *),
3012+
wdel as (delete from int4_tbl trgt where f1 = 0 returning *)
3013+
insert into rules_log AS trgt select old.* from wins, wupd, wdel
3014+
returning trgt.f1, trgt.f2;
3015+
-- check display of all rules added above
30233016
\d+ rules_src
30243017
Table "public.rules_src"
30253018
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
@@ -3044,6 +3037,26 @@ Rules:
30443037
r6 AS
30453038
ON UPDATE TO rules_src DO INSTEAD UPDATE rules_log trgt SET tag = 'updated'::text
30463039
WHERE trgt.f1 = new.f1
3040+
r7 AS
3041+
ON DELETE TO rules_src DO INSTEAD WITH wins AS (
3042+
INSERT INTO int4_tbl AS trgt_1 (f1)
3043+
VALUES (0)
3044+
RETURNING trgt_1.f1
3045+
), wupd AS (
3046+
UPDATE int4_tbl trgt_1 SET f1 = trgt_1.f1 + 1
3047+
RETURNING trgt_1.f1
3048+
), wdel AS (
3049+
DELETE FROM int4_tbl trgt_1
3050+
WHERE trgt_1.f1 = 0
3051+
RETURNING trgt_1.f1
3052+
)
3053+
INSERT INTO rules_log AS trgt (f1, f2) SELECT old.f1,
3054+
old.f2
3055+
FROM wins,
3056+
wupd,
3057+
wdel
3058+
RETURNING trgt.f1,
3059+
trgt.f2
30473060

30483061
--
30493062
-- Also check multiassignment deparsing.

src/test/regress/sql/rules.sql

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,13 +1034,24 @@ insert into rules_src values(22,23), (33,default);
10341034
select * from rules_src;
10351035
select * from rules_log;
10361036
create rule r4 as on delete to rules_src do notify rules_src_deletion;
1037-
\d+ rules_src
10381037

10391038
--
10401039
-- Ensure an aliased target relation for insert is correctly deparsed.
10411040
--
10421041
create rule r5 as on insert to rules_src do instead insert into rules_log AS trgt SELECT NEW.* RETURNING trgt.f1, trgt.f2;
10431042
create rule r6 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
1043+
1044+
--
1045+
-- Check deparse disambiguation of INSERT/UPDATE/DELETE targets.
1046+
--
1047+
create rule r7 as on delete to rules_src do instead
1048+
with wins as (insert into int4_tbl as trgt values (0) returning *),
1049+
wupd as (update int4_tbl trgt set f1 = f1+1 returning *),
1050+
wdel as (delete from int4_tbl trgt where f1 = 0 returning *)
1051+
insert into rules_log AS trgt select old.* from wins, wupd, wdel
1052+
returning trgt.f1, trgt.f2;
1053+
1054+
-- check display of all rules added above
10441055
\d+ rules_src
10451056

10461057
--

0 commit comments

Comments
 (0)