Skip to content

Commit 326a33a

Browse files
committed
Fix corner-case planner failure for MERGE.
MERGE planning could fail with "variable not found in subplan target list" if the target table is partitioned and all its partitions are excluded at plan time, or in the case where it has no partitions but used to have some. This happened because distribute_row_identity_vars thought it didn't need to make the target table's reltarget list fully valid; but if we generate a join plan then that is required because the dummy Result node's tlist will be made from the reltarget. The same logic appears in distribute_row_identity_vars in v14, but AFAICS the problem is unreachable in that branch for lack of MERGE. In other updating statements, the target table is always inner-joined to any other tables, so if the target is known dummy then the whole plan reduces to dummy, so no join nodes are created. So I'll refrain from back-patching this code change to v14 for now. Per report from Alvaro Herrera. Discussion: https://postgr.es/m/20230328112248.6as34mlx5sr4kltg@alvherre.pgsql
1 parent c1f1c1f commit 326a33a

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/backend/optimizer/util/appendinfo.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "nodes/nodeFuncs.h"
2222
#include "optimizer/appendinfo.h"
2323
#include "optimizer/pathnode.h"
24+
#include "optimizer/planmain.h"
2425
#include "parser/parsetree.h"
2526
#include "utils/lsyscache.h"
2627
#include "utils/rel.h"
@@ -994,9 +995,10 @@ distribute_row_identity_vars(PlannerInfo *root)
994995
* certainly process no rows. Handle this edge case by re-opening the top
995996
* result relation and adding the row identity columns it would have used,
996997
* as preprocess_targetlist() would have done if it weren't marked "inh".
997-
* (This is a bit ugly, but it seems better to confine the ugliness and
998-
* extra cycles to this unusual corner case.) We needn't worry about
999-
* fixing the rel's reltarget, as that won't affect the finished plan.
998+
* Then re-run build_base_rel_tlists() to ensure that the added columns
999+
* get propagated to the relation's reltarget. (This is a bit ugly, but
1000+
* it seems better to confine the ugliness and extra cycles to this
1001+
* unusual corner case.)
10001002
*/
10011003
if (root->row_identity_vars == NIL)
10021004
{
@@ -1006,6 +1008,8 @@ distribute_row_identity_vars(PlannerInfo *root)
10061008
add_row_identity_columns(root, result_relation,
10071009
target_rte, target_relation);
10081010
table_close(target_relation, NoLock);
1011+
build_base_rel_tlists(root, root->processed_tlist);
1012+
/* There are no ROWID_VAR Vars in this case, so we're done. */
10091013
return;
10101014
}
10111015

src/test/regress/expected/merge.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,32 @@ TABLE pa_target;
18491849
2
18501850
(2 rows)
18511851

1852+
-- Partition-less partitioned table
1853+
-- (the bug we are checking for appeared only if table had partitions before)
1854+
DROP TABLE pa_targetp;
1855+
EXPLAIN (VERBOSE, COSTS OFF)
1856+
MERGE INTO pa_target t USING pa_source s ON t.tid = s.sid
1857+
WHEN NOT MATCHED THEN INSERT VALUES (s.sid);
1858+
QUERY PLAN
1859+
--------------------------------------------
1860+
Merge on public.pa_target t
1861+
-> Hash Left Join
1862+
Output: s.sid, t.ctid
1863+
Inner Unique: true
1864+
Hash Cond: (s.sid = t.tid)
1865+
-> Seq Scan on public.pa_source s
1866+
Output: s.sid
1867+
-> Hash
1868+
Output: t.tid, t.ctid
1869+
-> Result
1870+
Output: t.tid, t.ctid
1871+
One-Time Filter: false
1872+
(12 rows)
1873+
1874+
MERGE INTO pa_target t USING pa_source s ON t.tid = s.sid
1875+
WHEN NOT MATCHED THEN INSERT VALUES (s.sid);
1876+
ERROR: no partition of relation "pa_target" found for row
1877+
DETAIL: Partition key of the failing row contains (tid) = (1).
18521878
DROP TABLE pa_source;
18531879
DROP TABLE pa_target CASCADE;
18541880
-- some complex joins on the source side

src/test/regress/sql/merge.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,18 @@ MERGE INTO pa_target t USING pa_source s ON t.tid = s.sid
11691169

11701170
TABLE pa_target;
11711171

1172+
-- Partition-less partitioned table
1173+
-- (the bug we are checking for appeared only if table had partitions before)
1174+
1175+
DROP TABLE pa_targetp;
1176+
1177+
EXPLAIN (VERBOSE, COSTS OFF)
1178+
MERGE INTO pa_target t USING pa_source s ON t.tid = s.sid
1179+
WHEN NOT MATCHED THEN INSERT VALUES (s.sid);
1180+
1181+
MERGE INTO pa_target t USING pa_source s ON t.tid = s.sid
1182+
WHEN NOT MATCHED THEN INSERT VALUES (s.sid);
1183+
11721184
DROP TABLE pa_source;
11731185
DROP TABLE pa_target CASCADE;
11741186

0 commit comments

Comments
 (0)