]> granicus.if.org Git - postgresql/commitdiff
Fix some more problems with nested append relations.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Oct 2014 23:30:41 +0000 (19:30 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 1 Oct 2014 23:30:41 +0000 (19:30 -0400)
As of commit a87c72915 (which later got backpatched as far as 9.1),
we're explicitly supporting the notion that append relations can be
nested; this can occur when UNION ALL constructs are nested, or when
a UNION ALL contains a table with inheritance children.

Bug #11457 from Nelson Page, as well as an earlier report from Elvis
Pranskevichus, showed that there were still nasty bugs associated with such
cases: in particular the EquivalenceClass mechanism could try to generate
"join" clauses connecting an appendrel child to some grandparent appendrel,
which would result in assertion failures or bogus plans.

Upon investigation I concluded that all current callers of
find_childrel_appendrelinfo() need to be fixed to explicitly consider
multiple levels of parent appendrels.  The most complex fix was in
processing of "broken" EquivalenceClasses, which are ECs for which we have
been unable to generate all the derived equality clauses we would like to
because of missing cross-type equality operators in the underlying btree
operator family.  That code path is more or less entirely untested by
the regression tests to date, because no standard opfamilies have such
holes in them.  So I wrote a new regression test script to try to exercise
it a bit, which turned out to be quite a worthwhile activity as it exposed
existing bugs in all supported branches.

The present patch is essentially the same as far back as 9.2, which is
where parameterized paths were introduced.  In 9.0 and 9.1, we only need
to back-patch a small fragment of commit 5b7b5518d, which fixes failure to
propagate out the original WHERE clauses when a broken EC contains constant
members.  (The regression test case results show that these older branches
are noticeably stupider than 9.2+ in terms of the quality of the plans
generated; but we don't really care about plan quality in such cases,
only that the plan not be outright wrong.  A more invasive fix in the
older branches would not be a good idea anyway from a plan-stability
standpoint.)

src/backend/optimizer/path/equivclass.c
src/test/regress/expected/equivclass.out [new file with mode: 0644]
src/test/regress/parallel_schedule
src/test/regress/serial_schedule
src/test/regress/sql/equivclass.sql [new file with mode: 0644]

index bb209d51e880a58f61fd2215dfd4b86fe679a522..b8a4ee788cfb6f151248ab4756ca7979d2c391bd 100644 (file)
@@ -744,7 +744,12 @@ generate_base_implied_equalities_no_const(PlannerInfo *root,
  * of the EC back into the main restrictinfo datastructures.  Multi-relation
  * clauses will be regurgitated later by generate_join_implied_equalities().
  * (We do it this way to maintain continuity with the case that ec_broken
- * becomes set only after we've gone up a join level or two.)
+ * becomes set only after we've gone up a join level or two.)  However, for
+ * an EC that contains constants, we can adopt a simpler strategy and just
+ * throw back all the source RestrictInfos immediately; that works because
+ * we know that such an EC can't become broken later.  (This rule justifies
+ * ignoring ec_has_const ECs in generate_join_implied_equalities, even when
+ * they are broken.)
  */
 static void
 generate_base_implied_equalities_broken(PlannerInfo *root,
@@ -756,7 +761,8 @@ generate_base_implied_equalities_broken(PlannerInfo *root,
        {
                RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
 
-               if (bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
+               if (ec->ec_has_const ||
+                       bms_membership(restrictinfo->required_relids) != BMS_MULTIPLE)
                        distribute_restrictinfo_to_rels(root, restrictinfo);
        }
 }
diff --git a/src/test/regress/expected/equivclass.out b/src/test/regress/expected/equivclass.out
new file mode 100644 (file)
index 0000000..58363ec
--- /dev/null
@@ -0,0 +1,311 @@
+--
+-- Tests for the planner's "equivalence class" mechanism
+--
+-- One thing that's not tested well during normal querying is the logic
+-- for handling "broken" ECs.  This is because an EC can only become broken
+-- if its underlying btree operator family doesn't include a complete set
+-- of cross-type equality operators.  There are not (and should not be)
+-- any such families built into Postgres; so we have to hack things up
+-- to create one.  We do this by making two alias types that are really
+-- int8 (so we need no new C code) and adding only some operators for them
+-- into the standard integer_ops opfamily.
+create type int8alias1;
+create function int8alias1in(cstring) returns int8alias1
+  strict immutable language internal as 'int8in';
+NOTICE:  return type int8alias1 is only a shell
+create function int8alias1out(int8alias1) returns cstring
+  strict immutable language internal as 'int8out';
+NOTICE:  argument type int8alias1 is only a shell
+create type int8alias1 (
+    input = int8alias1in,
+    output = int8alias1out,
+    like = int8
+);
+create type int8alias2;
+create function int8alias2in(cstring) returns int8alias2
+  strict immutable language internal as 'int8in';
+NOTICE:  return type int8alias2 is only a shell
+create function int8alias2out(int8alias2) returns cstring
+  strict immutable language internal as 'int8out';
+NOTICE:  argument type int8alias2 is only a shell
+create type int8alias2 (
+    input = int8alias2in,
+    output = int8alias2out,
+    like = int8
+);
+create cast (int8 as int8alias1) without function;
+create cast (int8 as int8alias2) without function;
+create cast (int8alias1 as int8) without function;
+create cast (int8alias2 as int8) without function;
+create function int8alias1eq(int8alias1, int8alias1) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias1eq,
+    leftarg = int8alias1, rightarg = int8alias1,
+    commutator = =,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8alias1, int8alias1);
+create function int8alias2eq(int8alias2, int8alias2) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias2eq,
+    leftarg = int8alias2, rightarg = int8alias2,
+    commutator = =,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8alias2, int8alias2);
+create function int8alias1eq(int8, int8alias1) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias1eq,
+    leftarg = int8, rightarg = int8alias1,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8, int8alias1);
+create function int8alias1eq(int8alias1, int8alias2) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias1eq,
+    leftarg = int8alias1, rightarg = int8alias2,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8alias1, int8alias2);
+create function int8alias1lt(int8alias1, int8alias1) returns bool
+  strict immutable language internal as 'int8lt';
+create operator < (
+    procedure = int8alias1lt,
+    leftarg = int8alias1, rightarg = int8alias1
+);
+alter operator family integer_ops using btree add
+  operator 1 < (int8alias1, int8alias1);
+create function int8alias1cmp(int8, int8alias1) returns int
+  strict immutable language internal as 'btint8cmp';
+alter operator family integer_ops using btree add
+  function 1 int8alias1cmp (int8, int8alias1);
+create table ec0 (ff int8 primary key, f1 int8, f2 int8);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "ec0_pkey" for table "ec0"
+create table ec1 (ff int8 primary key, f1 int8alias1, f2 int8alias2);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "ec1_pkey" for table "ec1"
+create table ec2 (xf int8 primary key, x1 int8alias1, x2 int8alias2);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "ec2_pkey" for table "ec2"
+-- for the moment we only want to look at nestloop plans
+set enable_hashjoin = off;
+set enable_mergejoin = off;
+--
+-- Note that for cases where there's a missing operator, we don't care so
+-- much whether the plan is ideal as that we don't fail or generate an
+-- outright incorrect plan.
+--
+explain (costs off)
+  select * from ec0 where ff = f1 and f1 = '42'::int8;
+            QUERY PLAN            
+----------------------------------
+ Index Scan using ec0_pkey on ec0
+   Index Cond: (ff = 42::bigint)
+   Filter: (f1 = 42::bigint)
+(3 rows)
+
+explain (costs off)
+  select * from ec0 where ff = f1 and f1 = '42'::int8alias1;
+              QUERY PLAN               
+---------------------------------------
+ Index Scan using ec0_pkey on ec0
+   Index Cond: (ff = '42'::int8alias1)
+   Filter: (f1 = '42'::int8alias1)
+(3 rows)
+
+explain (costs off)
+  select * from ec1 where ff = f1 and f1 = '42'::int8alias1;
+              QUERY PLAN               
+---------------------------------------
+ Index Scan using ec1_pkey on ec1
+   Index Cond: (ff = '42'::int8alias1)
+   Filter: (f1 = '42'::int8alias1)
+(3 rows)
+
+explain (costs off)
+  select * from ec1 where ff = f1 and f1 = '42'::int8alias2;
+                    QUERY PLAN                     
+---------------------------------------------------
+ Seq Scan on ec1
+   Filter: ((ff = f1) AND (f1 = '42'::int8alias2))
+(2 rows)
+
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and ff = '42'::int8;
+                          QUERY PLAN                           
+---------------------------------------------------------------
+ Nested Loop
+   Join Filter: (ec1.ff = ec2.x1)
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: ((ff = 42::bigint) AND (ff = 42::bigint))
+   ->  Seq Scan on ec2
+(5 rows)
+
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and ff = '42'::int8alias1;
+                 QUERY PLAN                  
+---------------------------------------------
+ Nested Loop
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: (ff = '42'::int8alias1)
+   ->  Seq Scan on ec2
+         Filter: (ec2.x1 = '42'::int8alias1)
+(5 rows)
+
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and '42'::int8 = x1;
+               QUERY PLAN               
+----------------------------------------
+ Nested Loop
+   Join Filter: (ec1.ff = ec2.x1)
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: (ff = 42::bigint)
+   ->  Seq Scan on ec2
+         Filter: (42::bigint = ec2.x1)
+(6 rows)
+
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and x1 = '42'::int8alias1;
+                 QUERY PLAN                  
+---------------------------------------------
+ Nested Loop
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: (ff = '42'::int8alias1)
+   ->  Seq Scan on ec2
+         Filter: (ec2.x1 = '42'::int8alias1)
+(5 rows)
+
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and x1 = '42'::int8alias2;
+               QUERY PLAN                
+-----------------------------------------
+ Nested Loop
+   ->  Seq Scan on ec2
+         Filter: (x1 = '42'::int8alias2)
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: (ec1.ff = ec2.x1)
+(5 rows)
+
+create unique index ec1_expr1 on ec1((ff + 1));
+create unique index ec1_expr2 on ec1((ff + 2 + 1));
+create unique index ec1_expr3 on ec1((ff + 3 + 1));
+create unique index ec1_expr4 on ec1((ff + 4));
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1
+  where ss1.x = ec1.f1 and ec1.ff = 42::int8;
+                           QUERY PLAN                            
+-----------------------------------------------------------------
+ Nested Loop
+   Join Filter: ((((public.ec1.ff + 2) + 1)) = public.ec1.f1)
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: (ff = 42::bigint)
+   ->  Append
+         ->  Append
+               ->  Seq Scan on ec1
+               ->  Seq Scan on ec1
+         ->  Index Scan using ec1_expr4 on ec1
+               Index Cond: ((public.ec1.ff + 4) = public.ec1.f1)
+(10 rows)
+
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1
+  where ss1.x = ec1.f1 and ec1.ff = 42::int8 and ec1.ff = ec1.f1;
+                             QUERY PLAN                             
+--------------------------------------------------------------------
+ Nested Loop
+   Join Filter: ((((public.ec1.ff + 2) + 1)) = public.ec1.f1)
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: ((ff = 42::bigint) AND (ff = 42::bigint))
+         Filter: (ff = f1)
+   ->  Append
+         ->  Index Scan using ec1_expr2 on ec1
+               Index Cond: (((public.ec1.ff + 2) + 1) = 42::bigint)
+         ->  Index Scan using ec1_expr3 on ec1
+               Index Cond: (((public.ec1.ff + 3) + 1) = 42::bigint)
+         ->  Index Scan using ec1_expr4 on ec1
+               Index Cond: ((public.ec1.ff + 4) = 42::bigint)
+(12 rows)
+
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss2
+  where ss1.x = ec1.f1 and ss1.x = ss2.x and ec1.ff = 42::int8;
+                                 QUERY PLAN                                  
+-----------------------------------------------------------------------------
+ Nested Loop
+   Join Filter: ((((public.ec1.ff + 2) + 1)) = (((public.ec1.ff + 2) + 1)))
+   ->  Append
+         ->  Seq Scan on ec1
+         ->  Seq Scan on ec1
+         ->  Seq Scan on ec1
+   ->  Materialize
+         ->  Nested Loop
+               Join Filter: ((((public.ec1.ff + 2) + 1)) = public.ec1.f1)
+               ->  Index Scan using ec1_pkey on ec1
+                     Index Cond: (ff = 42::bigint)
+               ->  Append
+                     ->  Append
+                           ->  Seq Scan on ec1
+                           ->  Seq Scan on ec1
+                     ->  Index Scan using ec1_expr4 on ec1
+                           Index Cond: ((public.ec1.ff + 4) = public.ec1.f1)
+(17 rows)
+
+-- check partially indexed scan
+drop index ec1_expr3;
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1
+  where ss1.x = ec1.f1 and ec1.ff = 42::int8;
+                           QUERY PLAN                            
+-----------------------------------------------------------------
+ Nested Loop
+   Join Filter: ((((public.ec1.ff + 2) + 1)) = public.ec1.f1)
+   ->  Index Scan using ec1_pkey on ec1
+         Index Cond: (ff = 42::bigint)
+   ->  Append
+         ->  Append
+               ->  Seq Scan on ec1
+               ->  Seq Scan on ec1
+         ->  Index Scan using ec1_expr4 on ec1
+               Index Cond: ((public.ec1.ff + 4) = public.ec1.f1)
+(10 rows)
+
index 7cdf872df593a3e24a789133230d582acbf2ba8c..95aa83428f83ce4dae555afaacd196d165638439 100644 (file)
@@ -84,7 +84,7 @@ test: rules
 # ----------
 # Another group of parallel tests
 # ----------
-test: select_views portals_p2 foreign_key cluster dependency guc bitmapops combocid tsearch tsdicts foreign_data window xmlmap
+test: select_views portals_p2 foreign_key cluster dependency guc bitmapops combocid tsearch tsdicts foreign_data window xmlmap equivclass
 
 # ----------
 # Another group of parallel tests
index c404d54206ba8b6c6a12e1a28605ecfe0b78973e..d65de25d27c885900b60fd4c0b91d3f4efa49d1e 100644 (file)
@@ -103,6 +103,7 @@ test: tsdicts
 test: foreign_data
 test: window
 test: xmlmap
+test: equivclass
 test: plancache
 test: limit
 test: plpgsql
diff --git a/src/test/regress/sql/equivclass.sql b/src/test/regress/sql/equivclass.sql
new file mode 100644 (file)
index 0000000..46cfa01
--- /dev/null
@@ -0,0 +1,187 @@
+--
+-- Tests for the planner's "equivalence class" mechanism
+--
+
+-- One thing that's not tested well during normal querying is the logic
+-- for handling "broken" ECs.  This is because an EC can only become broken
+-- if its underlying btree operator family doesn't include a complete set
+-- of cross-type equality operators.  There are not (and should not be)
+-- any such families built into Postgres; so we have to hack things up
+-- to create one.  We do this by making two alias types that are really
+-- int8 (so we need no new C code) and adding only some operators for them
+-- into the standard integer_ops opfamily.
+
+create type int8alias1;
+create function int8alias1in(cstring) returns int8alias1
+  strict immutable language internal as 'int8in';
+create function int8alias1out(int8alias1) returns cstring
+  strict immutable language internal as 'int8out';
+create type int8alias1 (
+    input = int8alias1in,
+    output = int8alias1out,
+    like = int8
+);
+
+create type int8alias2;
+create function int8alias2in(cstring) returns int8alias2
+  strict immutable language internal as 'int8in';
+create function int8alias2out(int8alias2) returns cstring
+  strict immutable language internal as 'int8out';
+create type int8alias2 (
+    input = int8alias2in,
+    output = int8alias2out,
+    like = int8
+);
+
+create cast (int8 as int8alias1) without function;
+create cast (int8 as int8alias2) without function;
+create cast (int8alias1 as int8) without function;
+create cast (int8alias2 as int8) without function;
+
+create function int8alias1eq(int8alias1, int8alias1) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias1eq,
+    leftarg = int8alias1, rightarg = int8alias1,
+    commutator = =,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8alias1, int8alias1);
+
+create function int8alias2eq(int8alias2, int8alias2) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias2eq,
+    leftarg = int8alias2, rightarg = int8alias2,
+    commutator = =,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8alias2, int8alias2);
+
+create function int8alias1eq(int8, int8alias1) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias1eq,
+    leftarg = int8, rightarg = int8alias1,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8, int8alias1);
+
+create function int8alias1eq(int8alias1, int8alias2) returns bool
+  strict immutable language internal as 'int8eq';
+create operator = (
+    procedure = int8alias1eq,
+    leftarg = int8alias1, rightarg = int8alias2,
+    restrict = eqsel, join = eqjoinsel,
+    merges
+);
+alter operator family integer_ops using btree add
+  operator 3 = (int8alias1, int8alias2);
+
+create function int8alias1lt(int8alias1, int8alias1) returns bool
+  strict immutable language internal as 'int8lt';
+create operator < (
+    procedure = int8alias1lt,
+    leftarg = int8alias1, rightarg = int8alias1
+);
+alter operator family integer_ops using btree add
+  operator 1 < (int8alias1, int8alias1);
+
+create function int8alias1cmp(int8, int8alias1) returns int
+  strict immutable language internal as 'btint8cmp';
+alter operator family integer_ops using btree add
+  function 1 int8alias1cmp (int8, int8alias1);
+
+create table ec0 (ff int8 primary key, f1 int8, f2 int8);
+create table ec1 (ff int8 primary key, f1 int8alias1, f2 int8alias2);
+create table ec2 (xf int8 primary key, x1 int8alias1, x2 int8alias2);
+
+-- for the moment we only want to look at nestloop plans
+set enable_hashjoin = off;
+set enable_mergejoin = off;
+
+--
+-- Note that for cases where there's a missing operator, we don't care so
+-- much whether the plan is ideal as that we don't fail or generate an
+-- outright incorrect plan.
+--
+
+explain (costs off)
+  select * from ec0 where ff = f1 and f1 = '42'::int8;
+explain (costs off)
+  select * from ec0 where ff = f1 and f1 = '42'::int8alias1;
+explain (costs off)
+  select * from ec1 where ff = f1 and f1 = '42'::int8alias1;
+explain (costs off)
+  select * from ec1 where ff = f1 and f1 = '42'::int8alias2;
+
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and ff = '42'::int8;
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and ff = '42'::int8alias1;
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and '42'::int8 = x1;
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and x1 = '42'::int8alias1;
+explain (costs off)
+  select * from ec1, ec2 where ff = x1 and x1 = '42'::int8alias2;
+
+create unique index ec1_expr1 on ec1((ff + 1));
+create unique index ec1_expr2 on ec1((ff + 2 + 1));
+create unique index ec1_expr3 on ec1((ff + 3 + 1));
+create unique index ec1_expr4 on ec1((ff + 4));
+
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1
+  where ss1.x = ec1.f1 and ec1.ff = 42::int8;
+
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1
+  where ss1.x = ec1.f1 and ec1.ff = 42::int8 and ec1.ff = ec1.f1;
+
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss2
+  where ss1.x = ec1.f1 and ss1.x = ss2.x and ec1.ff = 42::int8;
+
+-- check partially indexed scan
+drop index ec1_expr3;
+
+explain (costs off)
+  select * from ec1,
+    (select ff + 1 as x from
+       (select ff + 2 as ff from ec1
+        union all
+        select ff + 3 as ff from ec1) ss0
+     union all
+     select ff + 4 as x from ec1) as ss1
+  where ss1.x = ec1.f1 and ec1.ff = 42::int8;