Fix pushing of index-expression qualifications through UNION ALL.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 29 Jan 2012 21:31:31 +0000 (16:31 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 29 Jan 2012 21:31:31 +0000 (16:31 -0500)
In commit 57664ed25e5dea117158a2e663c29e60b3546e1c, I made the planner
wrap non-simple-variable outputs of appendrel children (IOW, child SELECTs
of UNION ALL subqueries) inside PlaceHolderVars, in order to solve some
issues with EquivalenceClass processing.  However, this means that any
upper-level WHERE clauses mentioning such outputs will now contain
PlaceHolderVars after they're pushed down into the appendrel child,
and that prevents indxpath.c from recognizing that they could be matched
to index expressions.  To fix, add explicit stripping of PlaceHolderVars
from index operands, same as we have long done for RelabelType nodes.
Add a regression test covering both this and the plain-UNION case (which
is a totally different code path, but should also be able to do it).

Per bug #6416 from Matteo Beccati.  Back-patch to 9.1, same as the
previous change.

src/backend/optimizer/path/indxpath.c
src/backend/optimizer/plan/createplan.c
src/test/regress/expected/union.out
src/test/regress/sql/union.sql

index 58b8797b7383cadc1bb4da235773449570a073bb..e97c1636b1a54c4d5972b9194999425a8c39fe57 100644 (file)
@@ -2285,6 +2285,15 @@ match_index_to_operand(Node *operand,
 {
        int                     indkey;
 
+       /*
+        * Ignore any PlaceHolderVar nodes above the operand.  This is needed so
+        * that we can successfully use expression-index constraints pushed down
+        * through appendrels (UNION ALL).  It's safe because a PlaceHolderVar
+        * appearing in a relation-scan-level expression is certainly a no-op.
+        */
+       while (operand && IsA(operand, PlaceHolderVar))
+               operand = (Node *) ((PlaceHolderVar *) operand)->phexpr;
+
        /*
         * Ignore any RelabelType node above the operand.       This is needed to be
         * able to apply indexscanning in binary-compatible-operator cases. Note:
index 7a81010e4cf4ad4d930c9beed3e79dccfe832562..bf0c6208333cccaf1522942bb22c81d27796f758 100644 (file)
@@ -2623,8 +2623,11 @@ fix_indexqual_operand(Node *node, IndexOptInfo *index)
        ListCell   *indexpr_item;
 
        /*
-        * Remove any binary-compatible relabeling of the indexkey
+        * Remove any PlaceHolderVars or binary-compatible relabeling of the
+        * indexkey (this must match logic in match_index_to_operand()).
         */
+       while (IsA(node, PlaceHolderVar))
+               node = (Node *) ((PlaceHolderVar *) node)->phexpr;
        if (IsA(node, RelabelType))
                node = (Node *) ((RelabelType *) node)->arg;
 
index 2f8037f9eb04dae009f90e58bde11736d877cad2..4fff229775199fd823cf8c3c40a8795bdd432379 100644 (file)
@@ -455,3 +455,51 @@ SELECT '3.4'::numeric UNION SELECT 'foo';
 ERROR:  invalid input syntax for type numeric: "foo"
 LINE 1: SELECT '3.4'::numeric UNION SELECT 'foo';
                                            ^
+--
+-- Test that expression-index constraints can be pushed down through
+-- UNION or UNION ALL
+--
+CREATE TEMP TABLE t1 (a text, b text);
+CREATE INDEX t1_ab_idx on t1 ((a || b));
+CREATE TEMP TABLE t2 (ab text primary key);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey" for table "t2"
+INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
+INSERT INTO t2 VALUES ('ab'), ('xy');
+set enable_seqscan = off;
+set enable_indexscan = on;
+set enable_bitmapscan = off;
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+  UNION ALL
+  SELECT * FROM t2) t
+ WHERE ab = 'ab';
+                    QUERY PLAN                     
+---------------------------------------------------
+ Result
+   ->  Append
+         ->  Index Scan using t1_ab_idx on t1
+               Index Cond: ((a || b) = 'ab'::text)
+         ->  Index Scan using t2_pkey on t2
+               Index Cond: (ab = 'ab'::text)
+(6 rows)
+
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+  UNION
+  SELECT * FROM t2) t
+ WHERE ab = 'ab';
+                    QUERY PLAN                     
+---------------------------------------------------
+ HashAggregate
+   ->  Append
+         ->  Index Scan using t1_ab_idx on t1
+               Index Cond: ((a || b) = 'ab'::text)
+         ->  Index Scan using t2_pkey on t2
+               Index Cond: (ab = 'ab'::text)
+(6 rows)
+
+reset enable_seqscan;
+reset enable_indexscan;
+reset enable_bitmapscan;
index daa72c929c034de52be43e198674991ef28ca5f9..b5c2128f3f3826f49cd1a7287067dbc7ff275ed4 100644 (file)
@@ -166,3 +166,36 @@ ORDER BY 1;
 
 -- This should fail, but it should produce an error cursor
 SELECT '3.4'::numeric UNION SELECT 'foo';
+
+--
+-- Test that expression-index constraints can be pushed down through
+-- UNION or UNION ALL
+--
+
+CREATE TEMP TABLE t1 (a text, b text);
+CREATE INDEX t1_ab_idx on t1 ((a || b));
+CREATE TEMP TABLE t2 (ab text primary key);
+INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
+INSERT INTO t2 VALUES ('ab'), ('xy');
+
+set enable_seqscan = off;
+set enable_indexscan = on;
+set enable_bitmapscan = off;
+
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+  UNION ALL
+  SELECT * FROM t2) t
+ WHERE ab = 'ab';
+
+explain (costs off)
+ SELECT * FROM
+ (SELECT a || b AS ab FROM t1
+  UNION
+  SELECT * FROM t2) t
+ WHERE ab = 'ab';
+
+reset enable_seqscan;
+reset enable_indexscan;
+reset enable_bitmapscan;