Skip to content

Commit 2c7887c

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 853cef0 commit 2c7887c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/backend/executor/execGrouping.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,8 @@ BuildTupleHashTableExt(PlanState *parent,
225225
allow_jit = metacxt != tablecxt;
226226

227227
/* build comparator for all columns */
228-
/* XXX: should we support non-minimal tuples for the inputslot? */
229228
hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
230-
&TTSOpsMinimalTuple, &TTSOpsMinimalTuple,
229+
NULL, &TTSOpsMinimalTuple,
231230
numCols,
232231
keyColIdx, eqfuncoids, collations,
233232
allow_jit ? parent : NULL);

src/test/regress/expected/with.out

+15
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,21 @@ SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
627627
16 | {3,7,11,16} | (16,"{3,7,11,16}")
628628
(16 rows)
629629

630+
CREATE TEMP TABLE duplicates (a INT NOT NULL);
631+
INSERT INTO duplicates VALUES(1), (1);
632+
-- Try out a recursive UNION case where the non-recursive part's table slot
633+
-- uses TTSOpsBufferHeapTuple and contains duplicate rows.
634+
WITH RECURSIVE cte (a) as (
635+
SELECT a FROM duplicates
636+
UNION
637+
SELECT a FROM cte
638+
)
639+
SELECT a FROM cte;
640+
a
641+
---
642+
1
643+
(1 row)
644+
630645
--
631646
-- test cycle detection
632647
--

src/test/regress/sql/with.sql

+12
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,18 @@ UNION ALL
339339
SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
340340
(t1.id=t2.id);
341341

342+
CREATE TEMP TABLE duplicates (a INT NOT NULL);
343+
INSERT INTO duplicates VALUES(1), (1);
344+
345+
-- Try out a recursive UNION case where the non-recursive part's table slot
346+
-- uses TTSOpsBufferHeapTuple and contains duplicate rows.
347+
WITH RECURSIVE cte (a) as (
348+
SELECT a FROM duplicates
349+
UNION
350+
SELECT a FROM cte
351+
)
352+
SELECT a FROM cte;
353+
342354
--
343355
-- test cycle detection
344356
--

0 commit comments

Comments
 (0)