mirror of
https://github.com/postgres/postgres.git
synced 2025-12-19 17:02:53 +03:00
Fix MERGE into a plain inheritance parent table.
When a MERGE's target table is the parent of an inheritance tree, any INSERT actions insert into the parent table using ModifyTableState's rootResultRelInfo. However, there are two bugs in the way is initialized: 1. ExecInitMerge() incorrectly uses a different ResultRelInfo entry from ModifyTableState's resultRelInfo array to build the insert projection, which may not be compatible with rootResultRelInfo. 2. ExecInitModifyTable() does not fully initialize rootResultRelInfo. Specifically, ri_WithCheckOptions, ri_WithCheckOptionExprs, ri_returningList, and ri_projectReturning are not initialized. This can lead to crashes, or incorrect query results due to failing to check WCO's or process the RETURNING list for INSERT actions. Fix both these bugs in ExecInitMerge(), noting that it is only necessary to fully initialize rootResultRelInfo if the MERGE has INSERT actions and the target table is a plain inheritance parent. Backpatch to v15, where MERGE was introduced. Reported-by: Andres Freund <andres@anarazel.de> Author: Dean Rasheed <dean.a.rasheed@gmail.com> Reviewed-by: Jian He <jian.universality@gmail.com> Reviewed-by: Tender Wang <tndrwang@gmail.com> Discussion: https://postgr.es/m/4rlmjfniiyffp6b3kv4pfy4jw3pciy6mq72rdgnedsnbsx7qe5@j5hlpiwdguvc Backpatch-through: 15
This commit is contained in:
@@ -2702,6 +2702,76 @@ SELECT * FROM new_measurement ORDER BY city_id, logdate;
|
||||
1 | 01-17-2007 | |
|
||||
(2 rows)
|
||||
|
||||
-- MERGE into inheritance root table
|
||||
DROP TRIGGER insert_measurement_trigger ON measurement;
|
||||
ALTER TABLE measurement ADD CONSTRAINT mcheck CHECK (city_id = 0) NO INHERIT;
|
||||
EXPLAIN (COSTS OFF)
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 100);
|
||||
QUERY PLAN
|
||||
--------------------------------------------------------------------------
|
||||
Merge on measurement m
|
||||
Merge on measurement_y2007m01 m_1
|
||||
-> Nested Loop Left Join
|
||||
-> Result
|
||||
-> Seq Scan on measurement_y2007m01 m_1
|
||||
Filter: ((city_id = 1) AND (logdate = '01-17-2007'::date))
|
||||
(6 rows)
|
||||
|
||||
BEGIN;
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 100);
|
||||
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
|
||||
city_id | logdate | peaktemp | unitsales
|
||||
---------+------------+----------+-----------
|
||||
0 | 07-21-2005 | 25 | 35
|
||||
0 | 01-17-2007 | 25 | 100
|
||||
(2 rows)
|
||||
|
||||
ROLLBACK;
|
||||
ALTER TABLE measurement ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE measurement FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY measurement_p ON measurement USING (peaktemp IS NOT NULL);
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, NULL, 100); -- should fail
|
||||
ERROR: new row violates row-level security policy for table "measurement"
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 100); -- ok
|
||||
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
|
||||
city_id | logdate | peaktemp | unitsales
|
||||
---------+------------+----------+-----------
|
||||
0 | 07-21-2005 | 25 | 35
|
||||
0 | 01-17-2007 | 25 | 100
|
||||
(2 rows)
|
||||
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-18-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 200)
|
||||
RETURNING merge_action(), m.*;
|
||||
merge_action | city_id | logdate | peaktemp | unitsales
|
||||
--------------+---------+------------+----------+-----------
|
||||
INSERT | 0 | 01-18-2007 | 25 | 200
|
||||
(1 row)
|
||||
|
||||
DROP TABLE measurement, new_measurement CASCADE;
|
||||
NOTICE: drop cascades to 3 other objects
|
||||
DETAIL: drop cascades to table measurement_y2006m02
|
||||
|
||||
@@ -1722,6 +1722,55 @@ WHEN MATCHED THEN DELETE;
|
||||
|
||||
SELECT * FROM new_measurement ORDER BY city_id, logdate;
|
||||
|
||||
-- MERGE into inheritance root table
|
||||
DROP TRIGGER insert_measurement_trigger ON measurement;
|
||||
ALTER TABLE measurement ADD CONSTRAINT mcheck CHECK (city_id = 0) NO INHERIT;
|
||||
|
||||
EXPLAIN (COSTS OFF)
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 100);
|
||||
|
||||
BEGIN;
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 100);
|
||||
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
|
||||
ROLLBACK;
|
||||
|
||||
ALTER TABLE measurement ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE measurement FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY measurement_p ON measurement USING (peaktemp IS NOT NULL);
|
||||
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, NULL, 100); -- should fail
|
||||
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 100); -- ok
|
||||
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
|
||||
|
||||
MERGE INTO measurement m
|
||||
USING (VALUES (1, '01-18-2007'::date)) nm(city_id, logdate) ON
|
||||
(m.city_id = nm.city_id and m.logdate=nm.logdate)
|
||||
WHEN NOT MATCHED THEN INSERT
|
||||
(city_id, logdate, peaktemp, unitsales)
|
||||
VALUES (city_id - 1, logdate, 25, 200)
|
||||
RETURNING merge_action(), m.*;
|
||||
|
||||
DROP TABLE measurement, new_measurement CASCADE;
|
||||
DROP FUNCTION measurement_insert_trigger();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user