mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-21 22:07:01 +08:00
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
This commit is contained in:
@ -1034,13 +1034,24 @@ insert into rules_src values(22,23), (33,default);
|
||||
select * from rules_src;
|
||||
select * from rules_log;
|
||||
create rule r4 as on delete to rules_src do notify rules_src_deletion;
|
||||
\d+ rules_src
|
||||
|
||||
--
|
||||
-- Ensure an aliased target relation for insert is correctly deparsed.
|
||||
--
|
||||
create rule r5 as on insert to rules_src do instead insert into rules_log AS trgt SELECT NEW.* RETURNING trgt.f1, trgt.f2;
|
||||
create rule r6 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
|
||||
|
||||
--
|
||||
-- Check deparse disambiguation of INSERT/UPDATE/DELETE targets.
|
||||
--
|
||||
create rule r7 as on delete to rules_src do instead
|
||||
with wins as (insert into int4_tbl as trgt values (0) returning *),
|
||||
wupd as (update int4_tbl trgt set f1 = f1+1 returning *),
|
||||
wdel as (delete from int4_tbl trgt where f1 = 0 returning *)
|
||||
insert into rules_log AS trgt select old.* from wins, wupd, wdel
|
||||
returning trgt.f1, trgt.f2;
|
||||
|
||||
-- check display of all rules added above
|
||||
\d+ rules_src
|
||||
|
||||
--
|
||||
|
||||
Reference in New Issue
Block a user