@@ -2676,6 +2676,34 @@ Rules:
2676
2676
ON DELETE TO rules_src DO
2677
2677
NOTIFY rules_src_deletion
2678
2678
2679
+ --
2680
+ -- Ensure a aliased target relation for insert is correctly deparsed.
2681
+ --
2682
+ create rule r4 as on insert to rules_src do instead insert into rules_log AS trgt SELECT NEW.* RETURNING trgt.f1, trgt.f2;
2683
+ create rule r5 as on update to rules_src do instead UPDATE rules_log AS trgt SET tag = 'updated' WHERE trgt.f1 = new.f1;
2684
+ \d+ rules_src
2685
+ Table "public.rules_src"
2686
+ Column | Type | Modifiers | Storage | Stats target | Description
2687
+ --------+---------+-----------+---------+--------------+-------------
2688
+ f1 | integer | | plain | |
2689
+ f2 | integer | | plain | |
2690
+ Rules:
2691
+ r1 AS
2692
+ ON UPDATE TO rules_src DO INSERT INTO rules_log (f1, f2, tag) VALUES (old.f1,old.f2,'old'::text), (new.f1,new.f2,'new'::text)
2693
+ r2 AS
2694
+ ON UPDATE TO rules_src DO VALUES (old.f1,old.f2,'old'::text), (new.f1,new.f2,'new'::text)
2695
+ r3 AS
2696
+ ON DELETE TO rules_src DO
2697
+ NOTIFY rules_src_deletion
2698
+ r4 AS
2699
+ ON INSERT TO rules_src DO INSTEAD INSERT INTO rules_log AS trgt (f1, f2) SELECT new.f1,
2700
+ new.f2
2701
+ RETURNING trgt.f1,
2702
+ trgt.f2
2703
+ r5 AS
2704
+ ON UPDATE TO rules_src DO INSTEAD UPDATE rules_log trgt SET tag = 'updated'::text
2705
+ WHERE trgt.f1 = new.f1
2706
+
2679
2707
--
2680
2708
-- check alter rename rule
2681
2709
--
@@ -2778,16 +2806,19 @@ CREATE TABLE hats (
2778
2806
hat_color char(10) -- hat color
2779
2807
);
2780
2808
CREATE TABLE hat_data (
2781
- hat_name char(10) primary key ,
2809
+ hat_name char(10),
2782
2810
hat_color char(10) -- hat color
2783
2811
);
2812
+ create unique index hat_data_unique_idx
2813
+ on hat_data (hat_name COLLATE "C" bpchar_pattern_ops);
2784
2814
-- okay
2785
2815
CREATE RULE hat_nosert AS ON INSERT TO hats
2786
2816
DO INSTEAD
2787
2817
INSERT INTO hat_data VALUES (
2788
2818
NEW.hat_name,
2789
2819
NEW.hat_color)
2790
- ON CONFLICT (hat_name) DO NOTHING RETURNING *;
2820
+ ON CONFLICT (hat_name COLLATE "C" bpchar_pattern_ops) WHERE hat_color = 'green'
2821
+ DO NOTHING RETURNING *;
2791
2822
-- Works (projects row)
2792
2823
INSERT INTO hats VALUES ('h7', 'black') RETURNING *;
2793
2824
hat_name | hat_color
@@ -2803,12 +2834,13 @@ INSERT INTO hats VALUES ('h7', 'black') RETURNING *;
2803
2834
2804
2835
SELECT tablename, rulename, definition FROM pg_rules
2805
2836
WHERE tablename = 'hats';
2806
- tablename | rulename | definition
2807
- -----------+------------+------------------------------------------------------------------------------
2808
- hats | hat_nosert | CREATE RULE hat_nosert AS +
2809
- | | ON INSERT TO hats DO INSTEAD INSERT INTO hat_data (hat_name, hat_color)+
2810
- | | VALUES (new.hat_name, new.hat_color) ON CONFLICT DO NOTHING +
2811
- | | RETURNING hat_data.hat_name, +
2837
+ tablename | rulename | definition
2838
+ -----------+------------+---------------------------------------------------------------------------------------------
2839
+ hats | hat_nosert | CREATE RULE hat_nosert AS +
2840
+ | | ON INSERT TO hats DO INSTEAD INSERT INTO hat_data (hat_name, hat_color) +
2841
+ | | VALUES (new.hat_name, new.hat_color) ON CONFLICT(hat_name COLLATE "C" bpchar_pattern_ops)+
2842
+ | | WHERE (hat_data.hat_color = 'green'::bpchar) DO NOTHING +
2843
+ | | RETURNING hat_data.hat_name, +
2812
2844
| | hat_data.hat_color;
2813
2845
(1 row)
2814
2846
@@ -2861,13 +2893,13 @@ SELECT * FROM hat_data WHERE hat_name = 'h8';
2861
2893
2862
2894
SELECT tablename, rulename, definition FROM pg_rules
2863
2895
WHERE tablename = 'hats';
2864
- tablename | rulename | definition
2865
- -----------+------------+-------------------------------------------------------------------------------------------------------------------------------
2866
- hats | hat_upsert | CREATE RULE hat_upsert AS +
2867
- | | ON INSERT TO hats DO INSTEAD INSERT INTO hat_data (hat_name, hat_color) +
2868
- | | VALUES (new.hat_name, new.hat_color) ON CONFLICT DO UPDATE SET hat_name = hat_data.hat_name, hat_color = excluded.hat_color+
2869
- | | WHERE (excluded.hat_color <> 'forbidden'::bpchar) +
2870
- | | RETURNING hat_data.hat_name, +
2896
+ tablename | rulename | definition
2897
+ -----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------
2898
+ hats | hat_upsert | CREATE RULE hat_upsert AS +
2899
+ | | ON INSERT TO hats DO INSTEAD INSERT INTO hat_data (hat_name, hat_color) +
2900
+ | | VALUES (new.hat_name, new.hat_color) ON CONFLICT(hat_name) DO UPDATE SET hat_name = hat_data.hat_name, hat_color = excluded.hat_color+
2901
+ | | WHERE (excluded.hat_color <> 'forbidden'::bpchar) +
2902
+ | | RETURNING hat_data.hat_name, +
2871
2903
| | hat_data.hat_color;
2872
2904
(1 row)
2873
2905
@@ -2877,7 +2909,7 @@ explain (costs off) INSERT INTO hats VALUES ('h8', 'forbidden') RETURNING *;
2877
2909
----------------------------------------------------------------
2878
2910
Insert on hat_data
2879
2911
Conflict Resolution: UPDATE
2880
- Conflict Arbiter Indexes: hat_data_pkey
2912
+ Conflict Arbiter Indexes: hat_data_unique_idx
2881
2913
Conflict Filter: (excluded.hat_color <> 'forbidden'::bpchar)
2882
2914
-> Result
2883
2915
(5 rows)
@@ -2909,7 +2941,7 @@ RETURNING *;
2909
2941
----------------------------------------------------------------
2910
2942
Insert on hat_data
2911
2943
Conflict Resolution: UPDATE
2912
- Conflict Arbiter Indexes: hat_data_pkey
2944
+ Conflict Arbiter Indexes: hat_data_unique_idx
2913
2945
Conflict Filter: (excluded.hat_color <> 'forbidden'::bpchar)
2914
2946
CTE data
2915
2947
-> Values Scan on "*VALUES*"
0 commit comments