Skip to content

Commit d6a3f32

Browse files
committed
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 this 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
1 parent ddfcfb7 commit d6a3f32

File tree

3 files changed

+188
-3
lines changed

3 files changed

+188
-3
lines changed

src/backend/executor/nodeModifyTable.c

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include "nodes/nodeFuncs.h"
6565
#include "optimizer/optimizer.h"
6666
#include "rewrite/rewriteHandler.h"
67+
#include "rewrite/rewriteManip.h"
6768
#include "storage/bufmgr.h"
6869
#include "storage/lmgr.h"
6970
#include "utils/builtins.h"
@@ -3375,6 +3376,7 @@ ExecInitMerge(ModifyTableState *mtstate, EState *estate)
33753376
switch (action->commandType)
33763377
{
33773378
case CMD_INSERT:
3379+
/* INSERT actions always use rootRelInfo */
33783380
ExecCheckPlanOutput(rootRelInfo->ri_RelationDesc,
33793381
action->targetList);
33803382

@@ -3414,9 +3416,23 @@ ExecInitMerge(ModifyTableState *mtstate, EState *estate)
34143416
}
34153417
else
34163418
{
3417-
/* not partitioned? use the stock relation and slot */
3418-
tgtslot = resultRelInfo->ri_newTupleSlot;
3419-
tgtdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
3419+
/*
3420+
* If the MERGE targets an inherited table, we insert
3421+
* into the root table, so we must initialize its
3422+
* "new" tuple slot, if not already done, and use its
3423+
* relation descriptor for the projection.
3424+
*
3425+
* For non-inherited tables, rootRelInfo and
3426+
* resultRelInfo are the same, and the "new" tuple
3427+
* slot will already have been initialized.
3428+
*/
3429+
if (rootRelInfo->ri_newTupleSlot == NULL)
3430+
rootRelInfo->ri_newTupleSlot =
3431+
table_slot_create(rootRelInfo->ri_RelationDesc,
3432+
&estate->es_tupleTable);
3433+
3434+
tgtslot = rootRelInfo->ri_newTupleSlot;
3435+
tgtdesc = RelationGetDescr(rootRelInfo->ri_RelationDesc);
34203436
}
34213437

34223438
action_state->mas_proj =
@@ -3449,6 +3465,76 @@ ExecInitMerge(ModifyTableState *mtstate, EState *estate)
34493465
}
34503466
}
34513467
}
3468+
3469+
/*
3470+
* If the MERGE targets an inherited table, any INSERT actions will use
3471+
* rootRelInfo, and rootRelInfo will not be in the resultRelInfo array.
3472+
* Therefore we must initialize its WITH CHECK OPTION constraints, as
3473+
* ExecInitModifyTable did for the resultRelInfo entries, but there should
3474+
* be nothing to do for RETURNING, since MERGE does not support RETURNING.
3475+
*
3476+
* Note that the planner does not build a withCheckOptionList for the root
3477+
* relation, but as in ExecInitPartitionInfo, we can use the first
3478+
* resultRelInfo entry as a reference to calculate the attno's for the
3479+
* root table.
3480+
*/
3481+
if (rootRelInfo != mtstate->resultRelInfo &&
3482+
rootRelInfo->ri_RelationDesc->rd_rel->relkind != RELKIND_PARTITIONED_TABLE &&
3483+
(mtstate->mt_merge_subcommands & MERGE_INSERT) != 0)
3484+
{
3485+
Relation rootRelation = rootRelInfo->ri_RelationDesc;
3486+
Relation firstResultRel = mtstate->resultRelInfo[0].ri_RelationDesc;
3487+
3488+
if (node->withCheckOptionLists != NIL)
3489+
{
3490+
List *wcoList;
3491+
List *wcoExprs = NIL;
3492+
3493+
/* There should be as many WCO lists as result rels */
3494+
Assert(list_length(node->withCheckOptionLists) ==
3495+
list_length(node->resultRelations));
3496+
3497+
/*
3498+
* Use the first WCO list as a reference. In the most common case,
3499+
* this will be for the same relation as rootRelInfo, and so there
3500+
* will be no need to adjust its attno's.
3501+
*/
3502+
wcoList = linitial(node->withCheckOptionLists);
3503+
if (rootRelation != firstResultRel)
3504+
{
3505+
int firstVarno = mtstate->resultRelInfo[0].ri_RangeTableIndex;
3506+
AttrMap *part_attmap;
3507+
bool found_whole_row;
3508+
3509+
/* Convert any Vars in it to contain the root's attno's */
3510+
part_attmap =
3511+
build_attrmap_by_name(RelationGetDescr(rootRelation),
3512+
RelationGetDescr(firstResultRel));
3513+
3514+
wcoList = (List *)
3515+
map_variable_attnos((Node *) wcoList,
3516+
firstVarno, 0,
3517+
part_attmap,
3518+
RelationGetForm(rootRelation)->reltype,
3519+
&found_whole_row);
3520+
}
3521+
3522+
foreach(lc, wcoList)
3523+
{
3524+
WithCheckOption *wco = lfirst_node(WithCheckOption, lc);
3525+
ExprState *wcoExpr = ExecInitQual(castNode(List, wco->qual),
3526+
&mtstate->ps);
3527+
3528+
wcoExprs = lappend(wcoExprs, wcoExpr);
3529+
}
3530+
3531+
rootRelInfo->ri_WithCheckOptions = wcoList;
3532+
rootRelInfo->ri_WithCheckOptionExprs = wcoExprs;
3533+
}
3534+
3535+
/* MERGE does not support RETURNING */
3536+
Assert(node->returningLists == NIL);
3537+
}
34523538
}
34533539

34543540
/*

src/test/regress/expected/merge.out

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,64 @@ SELECT * FROM new_measurement ORDER BY city_id, logdate;
23072307
1 | 01-17-2007 | |
23082308
(2 rows)
23092309

2310+
-- MERGE into inheritance root table
2311+
DROP TRIGGER insert_measurement_trigger ON measurement;
2312+
ALTER TABLE measurement ADD CONSTRAINT mcheck CHECK (city_id = 0) NO INHERIT;
2313+
EXPLAIN (COSTS OFF)
2314+
MERGE INTO measurement m
2315+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
2316+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
2317+
WHEN NOT MATCHED THEN INSERT
2318+
(city_id, logdate, peaktemp, unitsales)
2319+
VALUES (city_id - 1, logdate, 25, 100);
2320+
QUERY PLAN
2321+
--------------------------------------------------------------------------
2322+
Merge on measurement m
2323+
Merge on measurement_y2007m01 m_1
2324+
-> Nested Loop Left Join
2325+
-> Result
2326+
-> Seq Scan on measurement_y2007m01 m_1
2327+
Filter: ((city_id = 1) AND (logdate = '01-17-2007'::date))
2328+
(6 rows)
2329+
2330+
BEGIN;
2331+
MERGE INTO measurement m
2332+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
2333+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
2334+
WHEN NOT MATCHED THEN INSERT
2335+
(city_id, logdate, peaktemp, unitsales)
2336+
VALUES (city_id - 1, logdate, 25, 100);
2337+
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
2338+
city_id | logdate | peaktemp | unitsales
2339+
---------+------------+----------+-----------
2340+
0 | 07-21-2005 | 25 | 35
2341+
0 | 01-17-2007 | 25 | 100
2342+
(2 rows)
2343+
2344+
ROLLBACK;
2345+
ALTER TABLE measurement ENABLE ROW LEVEL SECURITY;
2346+
ALTER TABLE measurement FORCE ROW LEVEL SECURITY;
2347+
CREATE POLICY measurement_p ON measurement USING (peaktemp IS NOT NULL);
2348+
MERGE INTO measurement m
2349+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
2350+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
2351+
WHEN NOT MATCHED THEN INSERT
2352+
(city_id, logdate, peaktemp, unitsales)
2353+
VALUES (city_id - 1, logdate, NULL, 100); -- should fail
2354+
ERROR: new row violates row-level security policy for table "measurement"
2355+
MERGE INTO measurement m
2356+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
2357+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
2358+
WHEN NOT MATCHED THEN INSERT
2359+
(city_id, logdate, peaktemp, unitsales)
2360+
VALUES (city_id - 1, logdate, 25, 100); -- ok
2361+
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
2362+
city_id | logdate | peaktemp | unitsales
2363+
---------+------------+----------+-----------
2364+
0 | 07-21-2005 | 25 | 35
2365+
0 | 01-17-2007 | 25 | 100
2366+
(2 rows)
2367+
23102368
DROP TABLE measurement, new_measurement CASCADE;
23112369
NOTICE: drop cascades to 3 other objects
23122370
DETAIL: drop cascades to table measurement_y2006m02

src/test/regress/sql/merge.sql

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,47 @@ WHEN MATCHED THEN DELETE;
15001500

15011501
SELECT * FROM new_measurement ORDER BY city_id, logdate;
15021502

1503+
-- MERGE into inheritance root table
1504+
DROP TRIGGER insert_measurement_trigger ON measurement;
1505+
ALTER TABLE measurement ADD CONSTRAINT mcheck CHECK (city_id = 0) NO INHERIT;
1506+
1507+
EXPLAIN (COSTS OFF)
1508+
MERGE INTO measurement m
1509+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
1510+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
1511+
WHEN NOT MATCHED THEN INSERT
1512+
(city_id, logdate, peaktemp, unitsales)
1513+
VALUES (city_id - 1, logdate, 25, 100);
1514+
1515+
BEGIN;
1516+
MERGE INTO measurement m
1517+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
1518+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
1519+
WHEN NOT MATCHED THEN INSERT
1520+
(city_id, logdate, peaktemp, unitsales)
1521+
VALUES (city_id - 1, logdate, 25, 100);
1522+
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
1523+
ROLLBACK;
1524+
1525+
ALTER TABLE measurement ENABLE ROW LEVEL SECURITY;
1526+
ALTER TABLE measurement FORCE ROW LEVEL SECURITY;
1527+
CREATE POLICY measurement_p ON measurement USING (peaktemp IS NOT NULL);
1528+
1529+
MERGE INTO measurement m
1530+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
1531+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
1532+
WHEN NOT MATCHED THEN INSERT
1533+
(city_id, logdate, peaktemp, unitsales)
1534+
VALUES (city_id - 1, logdate, NULL, 100); -- should fail
1535+
1536+
MERGE INTO measurement m
1537+
USING (VALUES (1, '01-17-2007'::date)) nm(city_id, logdate) ON
1538+
(m.city_id = nm.city_id and m.logdate=nm.logdate)
1539+
WHEN NOT MATCHED THEN INSERT
1540+
(city_id, logdate, peaktemp, unitsales)
1541+
VALUES (city_id - 1, logdate, 25, 100); -- ok
1542+
SELECT * FROM ONLY measurement ORDER BY city_id, logdate;
1543+
15031544
DROP TABLE measurement, new_measurement CASCADE;
15041545
DROP FUNCTION measurement_insert_trigger();
15051546

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy