Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4efb4f0

Browse files
committed
Print the correct aliases for DML target tables in ruleutils.
ruleutils.c blindly printed the user-given alias (or nothing if therehadn't been one) for the target table of INSERT/UPDATE/DELETE queries.That works a large percentage of the time, but not always: for queriesappearing in WITH, it's possible that we chose a different alias toavoid conflict with outer-scope names. Since the chosen alias wouldbe used in any Var references to the target table, this'd lead to aninconsistent printout with consequences such as dump/restore failures.The correct logic for printing (or not) a relation alias was embeddedin get_from_clause_item. Factor it out to a separate function so thatwe don't need a jointree node to use it. (Only a limited part of thatfunction can be reached from these new call sites, but this seems likethe 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 supportedbranches.Discussion:https://postgr.es/m/e947fa21-24b2-f922-375a-d4f763ef3e4b@postgresql.orgDiscussion:https://postgr.es/m/CALDaNm1MMntjmT_NJGp-Z=xbF02qHGAyuSHfYHias3TqQbPF2w@mail.gmail.com
1 parent2eb8e54 commit4efb4f0

File tree

3 files changed

+126
-79
lines changed

3 files changed

+126
-79
lines changed

‎src/backend/utils/adt/ruleutils.c

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ static void get_from_clause(Query *query, const char *prefix,
463463
deparse_context*context);
464464
staticvoidget_from_clause_item(Node*jtnode,Query*query,
465465
deparse_context*context);
466+
staticvoidget_rte_alias(RangeTblEntry*rte,intvarno,booluse_as,
467+
deparse_context*context);
466468
staticvoidget_column_alias_list(deparse_columns*colinfo,
467469
deparse_context*context);
468470
staticvoidget_from_clause_coldeflist(RangeTblFunction*rtfunc,
@@ -6206,12 +6208,14 @@ get_insert_query_def(Query *query, deparse_context *context,
62066208
context->indentLevel+=PRETTYINDENT_STD;
62076209
appendStringInfoChar(buf,' ');
62086210
}
6209-
appendStringInfo(buf,"INSERT INTO %s",
6211+
appendStringInfo(buf,"INSERT INTO %s",
62106212
generate_relation_name(rte->relid,NIL));
6211-
/* INSERT requires AS keyword for target alias */
6212-
if (rte->alias!=NULL)
6213-
appendStringInfo(buf,"AS %s ",
6214-
quote_identifier(rte->alias->aliasname));
6213+
6214+
/* Print the relation alias, if needed; INSERT requires explicit AS */
6215+
get_rte_alias(rte,query->resultRelation, true,context);
6216+
6217+
/* always want a space here */
6218+
appendStringInfoChar(buf,' ');
62156219

62166220
/*
62176221
* Add the insert-column-names list. Any indirection decoration needed on
@@ -6393,9 +6397,10 @@ get_update_query_def(Query *query, deparse_context *context,
63936397
appendStringInfo(buf,"UPDATE %s%s",
63946398
only_marker(rte),
63956399
generate_relation_name(rte->relid,NIL));
6396-
if (rte->alias!=NULL)
6397-
appendStringInfo(buf," %s",
6398-
quote_identifier(rte->alias->aliasname));
6400+
6401+
/* Print the relation alias, if needed */
6402+
get_rte_alias(rte,query->resultRelation, false,context);
6403+
63996404
appendStringInfoString(buf," SET ");
64006405

64016406
/* Deparse targetlist */
@@ -6601,9 +6606,9 @@ get_delete_query_def(Query *query, deparse_context *context,
66016606
appendStringInfo(buf,"DELETE FROM %s%s",
66026607
only_marker(rte),
66036608
generate_relation_name(rte->relid,NIL));
6604-
if (rte->alias!=NULL)
6605-
appendStringInfo(buf," %s",
6606-
quote_identifier(rte->alias->aliasname));
6609+
6610+
/* Print the relation alias, if needed */
6611+
get_rte_alias(rte,query->resultRelation, false,context);
66076612

66086613
/* Add the USING clause if given */
66096614
get_from_clause(query," USING ",context);
@@ -10179,10 +10184,8 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1017910184
{
1018010185
intvarno= ((RangeTblRef*)jtnode)->rtindex;
1018110186
RangeTblEntry*rte=rt_fetch(varno,query->rtable);
10182-
char*refname=get_rtable_name(varno,context);
1018310187
deparse_columns*colinfo=deparse_columns_fetch(varno,dpns);
1018410188
RangeTblFunction*rtfunc1=NULL;
10185-
boolprintalias;
1018610189

1018710190
if (rte->lateral)
1018810191
appendStringInfoString(buf,"LATERAL ");
@@ -10319,54 +10322,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1031910322
}
1032010323

1032110324
/* Print the relation alias, if needed */
10322-
printalias= false;
10323-
if (rte->alias!=NULL)
10324-
{
10325-
/* Always print alias if user provided one */
10326-
printalias= true;
10327-
}
10328-
elseif (colinfo->printaliases)
10329-
{
10330-
/* Always print alias if we need to print column aliases */
10331-
printalias= true;
10332-
}
10333-
elseif (rte->rtekind==RTE_RELATION)
10334-
{
10335-
/*
10336-
* No need to print alias if it's same as relation name (this
10337-
* would normally be the case, but not if set_rtable_names had to
10338-
* resolve a conflict).
10339-
*/
10340-
if (strcmp(refname,get_relation_name(rte->relid))!=0)
10341-
printalias= true;
10342-
}
10343-
elseif (rte->rtekind==RTE_FUNCTION)
10344-
{
10345-
/*
10346-
* For a function RTE, always print alias. This covers possible
10347-
* renaming of the function and/or instability of the
10348-
* FigureColname rules for things that aren't simple functions.
10349-
* Note we'd need to force it anyway for the columndef list case.
10350-
*/
10351-
printalias= true;
10352-
}
10353-
elseif (rte->rtekind==RTE_VALUES)
10354-
{
10355-
/* Alias is syntactically required for VALUES */
10356-
printalias= true;
10357-
}
10358-
elseif (rte->rtekind==RTE_CTE)
10359-
{
10360-
/*
10361-
* No need to print alias if it's same as CTE name (this would
10362-
* normally be the case, but not if set_rtable_names had to
10363-
* resolve a conflict).
10364-
*/
10365-
if (strcmp(refname,rte->ctename)!=0)
10366-
printalias= true;
10367-
}
10368-
if (printalias)
10369-
appendStringInfo(buf," %s",quote_identifier(refname));
10325+
get_rte_alias(rte,varno, false,context);
1037010326

1037110327
/* Print the column definitions or aliases, if needed */
1037210328
if (rtfunc1&&rtfunc1->funccolnames!=NIL)
@@ -10500,6 +10456,73 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
1050010456
(int)nodeTag(jtnode));
1050110457
}
1050210458

10459+
/*
10460+
* get_rte_alias - print the relation's alias, if needed
10461+
*
10462+
* If printed, the alias is preceded by a space, or by " AS " if use_as is true.
10463+
*/
10464+
staticvoid
10465+
get_rte_alias(RangeTblEntry*rte,intvarno,booluse_as,
10466+
deparse_context*context)
10467+
{
10468+
deparse_namespace*dpns= (deparse_namespace*)linitial(context->namespaces);
10469+
char*refname=get_rtable_name(varno,context);
10470+
deparse_columns*colinfo=deparse_columns_fetch(varno,dpns);
10471+
boolprintalias= false;
10472+
10473+
if (rte->alias!=NULL)
10474+
{
10475+
/* Always print alias if user provided one */
10476+
printalias= true;
10477+
}
10478+
elseif (colinfo->printaliases)
10479+
{
10480+
/* Always print alias if we need to print column aliases */
10481+
printalias= true;
10482+
}
10483+
elseif (rte->rtekind==RTE_RELATION)
10484+
{
10485+
/*
10486+
* No need to print alias if it's same as relation name (this would
10487+
* normally be the case, but not if set_rtable_names had to resolve a
10488+
* conflict).
10489+
*/
10490+
if (strcmp(refname,get_relation_name(rte->relid))!=0)
10491+
printalias= true;
10492+
}
10493+
elseif (rte->rtekind==RTE_FUNCTION)
10494+
{
10495+
/*
10496+
* For a function RTE, always print alias. This covers possible
10497+
* renaming of the function and/or instability of the FigureColname
10498+
* rules for things that aren't simple functions. Note we'd need to
10499+
* force it anyway for the columndef list case.
10500+
*/
10501+
printalias= true;
10502+
}
10503+
elseif (rte->rtekind==RTE_SUBQUERY||
10504+
rte->rtekind==RTE_VALUES)
10505+
{
10506+
/* Alias is syntactically required for SUBQUERY and VALUES */
10507+
printalias= true;
10508+
}
10509+
elseif (rte->rtekind==RTE_CTE)
10510+
{
10511+
/*
10512+
* No need to print alias if it's same as CTE name (this would
10513+
* normally be the case, but not if set_rtable_names had to resolve a
10514+
* conflict).
10515+
*/
10516+
if (strcmp(refname,rte->ctename)!=0)
10517+
printalias= true;
10518+
}
10519+
10520+
if (printalias)
10521+
appendStringInfo(context->buf,"%s%s",
10522+
use_as ?" AS " :" ",
10523+
quote_identifier(refname));
10524+
}
10525+
1050310526
/*
1050410527
* get_column_alias_list - print column alias list for an RTE
1050510528
*

‎src/test/regress/expected/rules.out

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

30383038
create rule r4 as on delete to rules_src do notify rules_src_deletion;
3039-
\d+ rules_src
3040-
Table "public.rules_src"
3041-
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
3042-
--------+---------+-----------+----------+---------+---------+--------------+-------------
3043-
f1 | integer | | | | plain | |
3044-
f2 | integer | | | 0 | plain | |
3045-
Rules:
3046-
r1 AS
3047-
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)
3048-
r2 AS
3049-
ON UPDATE TO rules_src DO VALUES (old.f1,old.f2,'old'::text), (new.f1,new.f2,'new'::text)
3050-
r3 AS
3051-
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)
3052-
r4 AS
3053-
ON DELETE TO rules_src DO
3054-
NOTIFY rules_src_deletion
3055-
30563039
--
30573040
-- Ensure an aliased target relation for insert is correctly deparsed.
30583041
--
30593042
create rule r5 as on insert to rules_src do instead insert into rules_log AS trgt SELECT NEW.* RETURNING trgt.f1, trgt.f2;
30603043
create rule r6 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
3044+
--
3045+
-- Check deparse disambiguation of INSERT/UPDATE/DELETE targets.
3046+
--
3047+
create rule r7 as on delete to rules_src do instead
3048+
with wins as (insert into int4_tbl as trgt values (0) returning *),
3049+
wupd as (update int4_tbl trgt set f1 = f1+1 returning *),
3050+
wdel as (delete from int4_tbl trgt where f1 = 0 returning *)
3051+
insert into rules_log AS trgt select old.* from wins, wupd, wdel
3052+
returning trgt.f1, trgt.f2;
3053+
-- check display of all rules added above
30613054
\d+ rules_src
30623055
Table "public.rules_src"
30633056
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
@@ -3082,6 +3075,26 @@ Rules:
30823075
r6 AS
30833076
ON UPDATE TO rules_src DO INSTEAD UPDATE rules_log trgt SET tag = 'updated'::text
30843077
WHERE trgt.f1 = new.f1
3078+
r7 AS
3079+
ON DELETE TO rules_src DO INSTEAD WITH wins AS (
3080+
INSERT INTO int4_tbl AS trgt_1 (f1)
3081+
VALUES (0)
3082+
RETURNING trgt_1.f1
3083+
), wupd AS (
3084+
UPDATE int4_tbl trgt_1 SET f1 = trgt_1.f1 + 1
3085+
RETURNING trgt_1.f1
3086+
), wdel AS (
3087+
DELETE FROM int4_tbl trgt_1
3088+
WHERE trgt_1.f1 = 0
3089+
RETURNING trgt_1.f1
3090+
)
3091+
INSERT INTO rules_log AS trgt (f1, f2) SELECT old.f1,
3092+
old.f2
3093+
FROM wins,
3094+
wupd,
3095+
wdel
3096+
RETURNING trgt.f1,
3097+
trgt.f2
30853098

30863099
--
30873100
-- 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
createruler4ason 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
createruler5ason insert to rules_src do insteadinsert into rules_logAS trgtSELECT NEW.* RETURNINGtrgt.f1,trgt.f2;
10431042
createruler6asonupdate to rules_src do insteadUPDATE rules_logAS trgtSET tag='updated'WHEREtrgt.f1=new.f1;
1043+
1044+
--
1045+
-- Check deparse disambiguation of INSERT/UPDATE/DELETE targets.
1046+
--
1047+
createruler7ason delete to rules_src do instead
1048+
with winsas (insert into int4_tblas trgtvalues (0) returning*),
1049+
wupdas (update int4_tbl trgtset f1= f1+1 returning*),
1050+
wdelas (deletefrom int4_tbl trgtwhere f1=0 returning*)
1051+
insert into rules_logAS trgtselect old.*from wins, wupd, wdel
1052+
returningtrgt.f1,trgt.f2;
1053+
1054+
-- check display of all rules added above
10441055
\d+ rules_src
10451056

10461057
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp