Fix Assert failure in WITH RECURSIVE UNION queries
authorDavid Rowley <[email protected]>
Thu, 19 Dec 2024 00:13:51 +0000 (13:13 +1300)
committerDavid Rowley <[email protected]>
Thu, 19 Dec 2024 00:13:51 +0000 (13:13 +1300)
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
Back-through: 13, all supported versions

src/backend/executor/execGrouping.c
src/test/regress/expected/with.out
src/test/regress/sql/with.sql

index ccc088f3f54a9ea37f9e13db2fb4dc7caba0186c..2f10b38d44bc32749c133ba968a457f738eec7c6 100644 (file)
@@ -225,9 +225,8 @@ BuildTupleHashTableExt(PlanState *parent,
    allow_jit = metacxt != tablecxt;
 
    /* build comparator for all columns */
-   /* XXX: should we support non-minimal tuples for the inputslot? */
    hashtable->tab_eq_func = ExecBuildGroupingEqual(inputDesc, inputDesc,
-                                                   &TTSOpsMinimalTuple, &TTSOpsMinimalTuple,
+                                                   NULL, &TTSOpsMinimalTuple,
                                                    numCols,
                                                    keyColIdx, eqfuncoids, collations,
                                                    allow_jit ? parent : NULL);
index 2c3e36f5fe1d37e2b429a26551c688afe507445d..1226fbc9f4a67db20f8884eff40c63db62b5ef7e 100644 (file)
@@ -627,6 +627,21 @@ SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
  16 | {3,7,11,16} | (16,"{3,7,11,16}")
 (16 rows)
 
+CREATE TEMP TABLE duplicates (a INT NOT NULL);
+INSERT INTO duplicates VALUES(1), (1);
+-- Try out a recursive UNION case where the non-recursive part's table slot
+-- uses TTSOpsBufferHeapTuple and contains duplicate rows.
+WITH RECURSIVE cte (a) as (
+   SELECT a FROM duplicates
+   UNION
+   SELECT a FROM cte
+)
+SELECT a FROM cte;
+ a 
+---
+ 1
+(1 row)
+
 --
 -- test cycle detection
 --
index c2d1555fb082b55d4f53c88613b80a5cd0a31125..bae20e0d841370172de71fd02f94e0e325094b84 100644 (file)
@@ -339,6 +339,18 @@ UNION ALL
 SELECT t1.id, t2.path, t2 FROM t AS t1 JOIN t AS t2 ON
 (t1.id=t2.id);
 
+CREATE TEMP TABLE duplicates (a INT NOT NULL);
+INSERT INTO duplicates VALUES(1), (1);
+
+-- Try out a recursive UNION case where the non-recursive part's table slot
+-- uses TTSOpsBufferHeapTuple and contains duplicate rows.
+WITH RECURSIVE cte (a) as (
+   SELECT a FROM duplicates
+   UNION
+   SELECT a FROM cte
+)
+SELECT a FROM cte;
+
 --
 -- test cycle detection
 --