Skip to content

Commit 8f4ee96

Browse files
committed
Fix Assert failure in WITH RECURSIVE UNION queries
If the non-recursive part of a recursive CTE ended up using TTSOpsBufferHeapTuple as the table slot type, then a duplicate value could cause an Assert failure in CheckOpSlotCompatibility() when checking the hash table for the duplicate value. The expected slot type for the deform step was TTSOpsMinimalTuple so the Assert failed when the TTSOpsBufferHeapTuple slot was used. This is a long-standing bug which we likely didn't notice because it seems much more likely that the non-recursive term would have required projection and used a TTSOpsVirtual slot, which CheckOpSlotCompatibility is ok with. There doesn't seem to be any harm done here other than the Assert failure. Both TTSOpsMinimalTuple and TTSOpsBufferHeapTuple slot types require tuple deformation, so the EEOP_*_FETCHSOME ExprState step would have properly existed in the ExprState. The solution is to pass NULL for the ExecBuildGroupingEqual's 'lops' parameter. This means the ExprState's EEOP_*_FETCHSOME step won't expect a fixed slot type. This makes CheckOpSlotCompatibility() happy as no checking is performed when the ExprEvalStep is not expecting a fixed slot type. Reported-by: Richard Guo Reviewed-by: Tom Lane Discussion: https://postgr.es/m/CAMbWs4-8U9q2LAtf8+ghV11zeUReA3AmrYkxzBEv0vKnDxwkKA@mail.gmail.com Backpatch-through: 13, all supported versions
1 parent b7493e1 commit 8f4ee96

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/backend/executor/execGrouping.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,8 @@ BuildTupleHashTableExt(PlanState *parent,
236236
hash_iv);
237237

238238
/* build comparator for all columns */
239-
/* XXX: should we support non-minimal tuples for the inputslot? */
240239
hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
241-
&TTSOpsMinimalTuple, &TTSOpsMinimalTuple,
240+
NULL, &TTSOpsMinimalTuple,
242241
numCols,
243242
keyColIdx, eqfuncoids, collations,
244243
allow_jit ? parent : NULL);

src/test/regress/expected/with.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,21 @@ SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
656656
16 | {3,7,11,16} | (16,"{3,7,11,16}")
657657
(16 rows)
658658

659+
CREATE TEMP TABLE duplicates (a INT NOT NULL);
660+
INSERT INTO duplicates VALUES(1), (1);
661+
-- Try out a recursive UNION case where the non-recursive part's table slot
662+
-- uses TTSOpsBufferHeapTuple and contains duplicate rows.
663+
WITH RECURSIVE cte (a) as (
664+
SELECT a FROM duplicates
665+
UNION
666+
SELECT a FROM cte
667+
)
668+
SELECT a FROM cte;
669+
a
670+
---
671+
1
672+
(1 row)
673+
659674
-- test that column statistics from a materialized CTE are available
660675
-- to upper planner (otherwise, we'd get a stupider plan)
661676
explain (costs off)

src/test/regress/sql/with.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,18 @@ UNION ALL
361361
SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
362362
(t1.id=t2.id);
363363

364+
CREATE TEMP TABLE duplicates (a INT NOT NULL);
365+
INSERT INTO duplicates VALUES(1), (1);
366+
367+
-- Try out a recursive UNION case where the non-recursive part's table slot
368+
-- uses TTSOpsBufferHeapTuple and contains duplicate rows.
369+
WITH RECURSIVE cte (a) as (
370+
SELECT a FROM duplicates
371+
UNION
372+
SELECT a FROM cte
373+
)
374+
SELECT a FROM cte;
375+
364376
-- test that column statistics from a materialized CTE are available
365377
-- to upper planner (otherwise, we'd get a stupider plan)
366378
explain (costs off)

0 commit comments

Comments
 (0)