From cc95f1c1faf6e02d24243503c098a980ebe32f70 Mon Sep 17 00:00:00 2001
From: Vlada Pogozhelskaya <vlada@Vladas-MacBook-Pro.local>
Date: Sat, 10 Aug 2024 20:33:50 +0300
Subject: [PATCH 1/5] [PGPRO-10100] Fixed regression test in pathman_upd_del

Tags: joinsel
---
 expected/pathman_upd_del_3.out | 55 +++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 28 deletions(-)

diff --git a/expected/pathman_upd_del_3.out b/expected/pathman_upd_del_3.out
index 70b41e7d..53124187 100644
--- a/expected/pathman_upd_del_3.out
+++ b/expected/pathman_upd_del_3.out
@@ -303,35 +303,34 @@ UPDATE test.tmp t SET value = 2
 WHERE t.id IN (SELECT id
 			   FROM test.tmp2 t2
 			   WHERE id = t.id);
-                 QUERY PLAN                 
---------------------------------------------
+                QUERY PLAN                
+------------------------------------------
  Update on tmp t
-   ->  Seq Scan on tmp t
-         Filter: (SubPlan 1)
-         SubPlan 1
-           ->  Custom Scan (RuntimeAppend)
-                 Prune by: (t2.id = t.id)
-                 ->  Seq Scan on tmp2_1 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_2 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_3 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_4 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_5 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_6 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_7 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_8 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_9 t2
-                       Filter: (id = t.id)
-                 ->  Seq Scan on tmp2_10 t2
-                       Filter: (id = t.id)
-(26 rows)
+   ->  Nested Loop Semi Join
+         ->  Seq Scan on tmp t
+         ->  Custom Scan (RuntimeAppend)
+               Prune by: (t.id = t2.id)
+               ->  Seq Scan on tmp2_1 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_2 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_3 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_4 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_5 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_6 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_7 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_8 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_9 t2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_10 t2
+                     Filter: (t.id = id)
+(25 rows)
 
 /* Test special rule for CTE; SELECT (PostgreSQL 9.5) */
 EXPLAIN (COSTS OFF)

From b11d80afee458ba23001f5ad25e34fc2d23a580f Mon Sep 17 00:00:00 2001
From: Vlada Pogozhelskaya <v.pogozhelskaya@postgrespro.ru>
Date: Thu, 17 Oct 2024 10:10:17 +0300
Subject: [PATCH 2/5] [PGPRO-10100] Fixed regression test in pathman_upd_del

---
 expected/pathman_upd_del_2.out | 46 +++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/expected/pathman_upd_del_2.out b/expected/pathman_upd_del_2.out
index 80325d7e..47d47a0e 100644
--- a/expected/pathman_upd_del_2.out
+++ b/expected/pathman_upd_del_2.out
@@ -23,7 +23,7 @@ INSERT INTO test.tmp VALUES (1, 1), (2, 2);
 CREATE TABLE test.tmp2 (id INTEGER NOT NULL, value INTEGER NOT NULL);
 INSERT INTO test.tmp2 SELECT i % 10 + 1, i FROM generate_series(1, 100) i;
 SELECT pathman.create_range_partitions('test.tmp2', 'id', 1, 1, 10);
- create_range_partitions 
+ create_range_partitions
 -------------------------
                       10
 (1 row)
@@ -38,7 +38,7 @@ FROM generate_series('2010-01-01'::date, '2010-12-31'::date, '1 day') AS g;
 SELECT pathman.create_range_partitions('test.range_rel', 'dt',
 									   '2010-01-01'::date, '1 month'::interval,
 									   12);
- create_range_partitions 
+ create_range_partitions
 -------------------------
                       12
 (1 row)
@@ -49,7 +49,7 @@ VACUUM ANALYZE;
  */
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Update on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -59,7 +59,7 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id  |            dt            | value 
+ id  |            dt            | value
 -----+--------------------------+-------
  166 | Tue Jun 15 00:00:00 2010 |   111
 (1 row)
@@ -67,7 +67,7 @@ SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
 ROLLBACK;
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Delete on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -77,14 +77,14 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Update on range_rel
    ->  Seq Scan on range_rel
@@ -94,14 +94,14 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt = '1990-01-01';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Delete on range_rel
    ->  Seq Scan on range_rel
@@ -111,7 +111,7 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt < '1990-01-01';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
@@ -120,7 +120,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.range_rel r SET value = t.value
 FROM test.tmp t WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Update on range_rel_1 r
    ->  Nested Loop
@@ -138,7 +138,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.tmp t SET value = r.value
 FROM test.range_rel r WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Update on tmp t
    ->  Nested Loop
@@ -156,7 +156,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.range_rel r USING test.tmp t
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Delete on range_rel_1 r
    ->  Nested Loop
@@ -174,7 +174,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.tmp t USING test.range_rel r
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop
@@ -224,7 +224,7 @@ USING (SELECT *
 	   JOIN test.tmp2 a2
 	   USING(id)) t
 WHERE t.id = r.id;
-                   QUERY PLAN                   
+                   QUERY PLAN
 ------------------------------------------------
  Delete on tmp r
    ->  Nested Loop
@@ -303,7 +303,7 @@ UPDATE test.tmp t SET value = 2
 WHERE t.id IN (SELECT id
 			   FROM test.tmp2 t2
 			   WHERE id = t.id);
-                 QUERY PLAN                 
+                 QUERY PLAN
 --------------------------------------------
  Update on tmp t
    ->  Seq Scan on tmp t
@@ -338,7 +338,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT * FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02')
 DELETE FROM test.tmp USING q;
-                                         QUERY PLAN                                         
+                                         QUERY PLAN
 --------------------------------------------------------------------------------------------
  Delete on tmp
    ->  Nested Loop
@@ -359,7 +359,7 @@ WITH q AS (DELETE FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02'
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                       QUERY PLAN                                       
+                                       QUERY PLAN
 ----------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -384,7 +384,7 @@ WITH q AS (DELETE FROM test.tmp t
 		   WHERE r.dt = '2010-01-02' AND r.id = t.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                          QUERY PLAN                                          
+                                          QUERY PLAN
 ----------------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -414,15 +414,15 @@ WITH q AS (WITH n AS (SELECT id FROM test.tmp2 WHERE id = 2)
 		   WHERE t.id = n.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-               QUERY PLAN               
-----------------------------------------
+                 QUERY PLAN
+---------------------------------------------
  Delete on tmp
    CTE q
      ->  Delete on tmp t
            ->  Nested Loop
                  ->  Seq Scan on tmp t
                        Filter: (id = 2)
-                 ->  Seq Scan on tmp2_2
+                 ->  Seq Scan on tmp2_2 tmp2
                        Filter: (id = 2)
    ->  Nested Loop
          ->  Seq Scan on tmp
@@ -434,7 +434,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT id FROM test.tmp2
 		   WHERE id < 3)
 DELETE FROM test.tmp t WHERE t.id in (SELECT id FROM q);
-                          QUERY PLAN                          
+                          QUERY PLAN
 --------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop Semi Join

From a6ee529346376ccdfc1a88adcf6b70d3bd34182f Mon Sep 17 00:00:00 2001
From: Vlada Pogozhelskaya <v.pogozhelskaya@postgrespro.ru>
Date: Mon, 21 Oct 2024 16:34:35 +0300
Subject: [PATCH 3/5] Revert "[PGPRO-10100] Fixed regression test in
 pathman_upd_del"

This reverts commit b11d80afee458ba23001f5ad25e34fc2d23a580f.
---
 expected/pathman_upd_del_2.out | 46 +++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/expected/pathman_upd_del_2.out b/expected/pathman_upd_del_2.out
index 47d47a0e..80325d7e 100644
--- a/expected/pathman_upd_del_2.out
+++ b/expected/pathman_upd_del_2.out
@@ -23,7 +23,7 @@ INSERT INTO test.tmp VALUES (1, 1), (2, 2);
 CREATE TABLE test.tmp2 (id INTEGER NOT NULL, value INTEGER NOT NULL);
 INSERT INTO test.tmp2 SELECT i % 10 + 1, i FROM generate_series(1, 100) i;
 SELECT pathman.create_range_partitions('test.tmp2', 'id', 1, 1, 10);
- create_range_partitions
+ create_range_partitions 
 -------------------------
                       10
 (1 row)
@@ -38,7 +38,7 @@ FROM generate_series('2010-01-01'::date, '2010-12-31'::date, '1 day') AS g;
 SELECT pathman.create_range_partitions('test.range_rel', 'dt',
 									   '2010-01-01'::date, '1 month'::interval,
 									   12);
- create_range_partitions
+ create_range_partitions 
 -------------------------
                       12
 (1 row)
@@ -49,7 +49,7 @@ VACUUM ANALYZE;
  */
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
-                                   QUERY PLAN
+                                   QUERY PLAN                                   
 --------------------------------------------------------------------------------
  Update on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -59,7 +59,7 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id  |            dt            | value
+ id  |            dt            | value 
 -----+--------------------------+-------
  166 | Tue Jun 15 00:00:00 2010 |   111
 (1 row)
@@ -67,7 +67,7 @@ SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
 ROLLBACK;
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
-                                   QUERY PLAN
+                                   QUERY PLAN                                   
 --------------------------------------------------------------------------------
  Delete on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -77,14 +77,14 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id | dt | value
+ id | dt | value 
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01';
-                                   QUERY PLAN
+                                   QUERY PLAN                                   
 --------------------------------------------------------------------------------
  Update on range_rel
    ->  Seq Scan on range_rel
@@ -94,14 +94,14 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt = '1990-01-01';
- id | dt | value
+ id | dt | value 
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
-                                   QUERY PLAN
+                                   QUERY PLAN                                   
 --------------------------------------------------------------------------------
  Delete on range_rel
    ->  Seq Scan on range_rel
@@ -111,7 +111,7 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt < '1990-01-01';
- id | dt | value
+ id | dt | value 
 ----+----+-------
 (0 rows)
 
@@ -120,7 +120,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.range_rel r SET value = t.value
 FROM test.tmp t WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN
+                                      QUERY PLAN                                      
 --------------------------------------------------------------------------------------
  Update on range_rel_1 r
    ->  Nested Loop
@@ -138,7 +138,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.tmp t SET value = r.value
 FROM test.range_rel r WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN
+                                      QUERY PLAN                                      
 --------------------------------------------------------------------------------------
  Update on tmp t
    ->  Nested Loop
@@ -156,7 +156,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.range_rel r USING test.tmp t
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN
+                                      QUERY PLAN                                      
 --------------------------------------------------------------------------------------
  Delete on range_rel_1 r
    ->  Nested Loop
@@ -174,7 +174,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.tmp t USING test.range_rel r
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN
+                                      QUERY PLAN                                      
 --------------------------------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop
@@ -224,7 +224,7 @@ USING (SELECT *
 	   JOIN test.tmp2 a2
 	   USING(id)) t
 WHERE t.id = r.id;
-                   QUERY PLAN
+                   QUERY PLAN                   
 ------------------------------------------------
  Delete on tmp r
    ->  Nested Loop
@@ -303,7 +303,7 @@ UPDATE test.tmp t SET value = 2
 WHERE t.id IN (SELECT id
 			   FROM test.tmp2 t2
 			   WHERE id = t.id);
-                 QUERY PLAN
+                 QUERY PLAN                 
 --------------------------------------------
  Update on tmp t
    ->  Seq Scan on tmp t
@@ -338,7 +338,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT * FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02')
 DELETE FROM test.tmp USING q;
-                                         QUERY PLAN
+                                         QUERY PLAN                                         
 --------------------------------------------------------------------------------------------
  Delete on tmp
    ->  Nested Loop
@@ -359,7 +359,7 @@ WITH q AS (DELETE FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02'
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                       QUERY PLAN
+                                       QUERY PLAN                                       
 ----------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -384,7 +384,7 @@ WITH q AS (DELETE FROM test.tmp t
 		   WHERE r.dt = '2010-01-02' AND r.id = t.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                          QUERY PLAN
+                                          QUERY PLAN                                          
 ----------------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -414,15 +414,15 @@ WITH q AS (WITH n AS (SELECT id FROM test.tmp2 WHERE id = 2)
 		   WHERE t.id = n.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                 QUERY PLAN
----------------------------------------------
+               QUERY PLAN               
+----------------------------------------
  Delete on tmp
    CTE q
      ->  Delete on tmp t
            ->  Nested Loop
                  ->  Seq Scan on tmp t
                        Filter: (id = 2)
-                 ->  Seq Scan on tmp2_2 tmp2
+                 ->  Seq Scan on tmp2_2
                        Filter: (id = 2)
    ->  Nested Loop
          ->  Seq Scan on tmp
@@ -434,7 +434,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT id FROM test.tmp2
 		   WHERE id < 3)
 DELETE FROM test.tmp t WHERE t.id in (SELECT id FROM q);
-                          QUERY PLAN
+                          QUERY PLAN                          
 --------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop Semi Join

From 37ef13b81362b1d7809293f4d298bed3711cd6de Mon Sep 17 00:00:00 2001
From: Vlada Pogozhelskaya <v.pogozhelskaya@postgrespro.ru>
Date: Mon, 21 Oct 2024 16:41:20 +0300
Subject: [PATCH 4/5] [PGPRO-10100] Fixed regression test in pathman_upd_del

---
 expected/pathman_upd_del_2.out |  42 +--
 expected/pathman_upd_del_4.out | 462 +++++++++++++++++++++++++++++++++
 2 files changed, 483 insertions(+), 21 deletions(-)
 create mode 100644 expected/pathman_upd_del_4.out

diff --git a/expected/pathman_upd_del_2.out b/expected/pathman_upd_del_2.out
index 80325d7e..8932a23e 100644
--- a/expected/pathman_upd_del_2.out
+++ b/expected/pathman_upd_del_2.out
@@ -23,7 +23,7 @@ INSERT INTO test.tmp VALUES (1, 1), (2, 2);
 CREATE TABLE test.tmp2 (id INTEGER NOT NULL, value INTEGER NOT NULL);
 INSERT INTO test.tmp2 SELECT i % 10 + 1, i FROM generate_series(1, 100) i;
 SELECT pathman.create_range_partitions('test.tmp2', 'id', 1, 1, 10);
- create_range_partitions 
+ create_range_partitions
 -------------------------
                       10
 (1 row)
@@ -38,7 +38,7 @@ FROM generate_series('2010-01-01'::date, '2010-12-31'::date, '1 day') AS g;
 SELECT pathman.create_range_partitions('test.range_rel', 'dt',
 									   '2010-01-01'::date, '1 month'::interval,
 									   12);
- create_range_partitions 
+ create_range_partitions
 -------------------------
                       12
 (1 row)
@@ -49,7 +49,7 @@ VACUUM ANALYZE;
  */
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Update on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -59,7 +59,7 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id  |            dt            | value 
+ id  |            dt            | value
 -----+--------------------------+-------
  166 | Tue Jun 15 00:00:00 2010 |   111
 (1 row)
@@ -67,7 +67,7 @@ SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
 ROLLBACK;
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Delete on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -77,14 +77,14 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Update on range_rel
    ->  Seq Scan on range_rel
@@ -94,14 +94,14 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt = '1990-01-01';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Delete on range_rel
    ->  Seq Scan on range_rel
@@ -111,7 +111,7 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt < '1990-01-01';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
@@ -120,7 +120,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.range_rel r SET value = t.value
 FROM test.tmp t WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Update on range_rel_1 r
    ->  Nested Loop
@@ -138,7 +138,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.tmp t SET value = r.value
 FROM test.range_rel r WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Update on tmp t
    ->  Nested Loop
@@ -156,7 +156,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.range_rel r USING test.tmp t
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Delete on range_rel_1 r
    ->  Nested Loop
@@ -174,7 +174,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.tmp t USING test.range_rel r
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop
@@ -224,7 +224,7 @@ USING (SELECT *
 	   JOIN test.tmp2 a2
 	   USING(id)) t
 WHERE t.id = r.id;
-                   QUERY PLAN                   
+                   QUERY PLAN
 ------------------------------------------------
  Delete on tmp r
    ->  Nested Loop
@@ -303,7 +303,7 @@ UPDATE test.tmp t SET value = 2
 WHERE t.id IN (SELECT id
 			   FROM test.tmp2 t2
 			   WHERE id = t.id);
-                 QUERY PLAN                 
+                 QUERY PLAN
 --------------------------------------------
  Update on tmp t
    ->  Seq Scan on tmp t
@@ -338,7 +338,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT * FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02')
 DELETE FROM test.tmp USING q;
-                                         QUERY PLAN                                         
+                                         QUERY PLAN
 --------------------------------------------------------------------------------------------
  Delete on tmp
    ->  Nested Loop
@@ -359,7 +359,7 @@ WITH q AS (DELETE FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02'
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                       QUERY PLAN                                       
+                                       QUERY PLAN
 ----------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -384,7 +384,7 @@ WITH q AS (DELETE FROM test.tmp t
 		   WHERE r.dt = '2010-01-02' AND r.id = t.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                          QUERY PLAN                                          
+                                          QUERY PLAN
 ----------------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -414,7 +414,7 @@ WITH q AS (WITH n AS (SELECT id FROM test.tmp2 WHERE id = 2)
 		   WHERE t.id = n.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-               QUERY PLAN               
+               QUERY PLAN
 ----------------------------------------
  Delete on tmp
    CTE q
@@ -434,7 +434,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT id FROM test.tmp2
 		   WHERE id < 3)
 DELETE FROM test.tmp t WHERE t.id in (SELECT id FROM q);
-                          QUERY PLAN                          
+                          QUERY PLAN
 --------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop Semi Join
diff --git a/expected/pathman_upd_del_4.out b/expected/pathman_upd_del_4.out
new file mode 100644
index 00000000..70b41e7d
--- /dev/null
+++ b/expected/pathman_upd_del_4.out
@@ -0,0 +1,462 @@
+/*
+ * -------------------------------------------
+ *  NOTE: This test behaves differenly on 9.5
+ * -------------------------------------------
+ *
+ * Also since 8edd0e794 (>= 12) Append nodes with single subplan are eliminated,
+ * causing different output. Moreover, again since 12 (608b167f9f), CTEs which are
+ * scanned once are no longer an optimization fence, changing a good deal of
+ * plans here. There is an option to forcibly make them MATERIALIZED, but we
+ * also need to run tests on older versions, so put updated plans in
+ * pathman_upd_del_2.out instead.
+ */
+\set VERBOSITY terse
+SET search_path = 'public';
+CREATE SCHEMA pathman;
+CREATE EXTENSION pg_pathman SCHEMA pathman;
+CREATE SCHEMA test;
+SET enable_indexscan = ON;
+SET enable_seqscan = OFF;
+/* Temporary tables for JOINs */
+CREATE TABLE test.tmp (id INTEGER NOT NULL, value INTEGER NOT NULL);
+INSERT INTO test.tmp VALUES (1, 1), (2, 2);
+CREATE TABLE test.tmp2 (id INTEGER NOT NULL, value INTEGER NOT NULL);
+INSERT INTO test.tmp2 SELECT i % 10 + 1, i FROM generate_series(1, 100) i;
+SELECT pathman.create_range_partitions('test.tmp2', 'id', 1, 1, 10);
+ create_range_partitions 
+-------------------------
+                      10
+(1 row)
+
+/* Partition table by RANGE */
+CREATE TABLE test.range_rel (
+	id		SERIAL PRIMARY KEY,
+	dt		TIMESTAMP NOT NULL,
+	value	INTEGER);
+INSERT INTO test.range_rel (dt, value) SELECT g, extract(day from g)
+FROM generate_series('2010-01-01'::date, '2010-12-31'::date, '1 day') AS g;
+SELECT pathman.create_range_partitions('test.range_rel', 'dt',
+									   '2010-01-01'::date, '1 month'::interval,
+									   12);
+ create_range_partitions 
+-------------------------
+                      12
+(1 row)
+
+VACUUM ANALYZE;
+/*
+ * Test UPDATE and DELETE
+ */
+/* have partitions for this 'dt' */
+EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
+                                   QUERY PLAN                                   
+--------------------------------------------------------------------------------
+ Update on range_rel_6
+   ->  Seq Scan on range_rel_6
+         Filter: (dt = 'Tue Jun 15 00:00:00 2010'::timestamp without time zone)
+(3 rows)
+
+BEGIN;
+UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
+SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
+ id  |            dt            | value 
+-----+--------------------------+-------
+ 166 | Tue Jun 15 00:00:00 2010 |   111
+(1 row)
+
+ROLLBACK;
+/* have partitions for this 'dt' */
+EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
+                                   QUERY PLAN                                   
+--------------------------------------------------------------------------------
+ Delete on range_rel_6
+   ->  Seq Scan on range_rel_6
+         Filter: (dt = 'Tue Jun 15 00:00:00 2010'::timestamp without time zone)
+(3 rows)
+
+BEGIN;
+DELETE FROM test.range_rel WHERE dt = '2010-06-15';
+SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
+ id | dt | value 
+----+----+-------
+(0 rows)
+
+ROLLBACK;
+/* no partitions for this 'dt' */
+EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01';
+                                   QUERY PLAN                                   
+--------------------------------------------------------------------------------
+ Update on range_rel
+   ->  Seq Scan on range_rel
+         Filter: (dt = 'Mon Jan 01 00:00:00 1990'::timestamp without time zone)
+(3 rows)
+
+BEGIN;
+UPDATE test.range_rel SET value = 111 WHERE dt = '1990-01-01';
+SELECT * FROM test.range_rel WHERE dt = '1990-01-01';
+ id | dt | value 
+----+----+-------
+(0 rows)
+
+ROLLBACK;
+/* no partitions for this 'dt' */
+EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
+                                   QUERY PLAN                                   
+--------------------------------------------------------------------------------
+ Delete on range_rel
+   ->  Seq Scan on range_rel
+         Filter: (dt < 'Mon Jan 01 00:00:00 1990'::timestamp without time zone)
+(3 rows)
+
+BEGIN;
+DELETE FROM test.range_rel WHERE dt < '1990-01-01';
+SELECT * FROM test.range_rel WHERE dt < '1990-01-01';
+ id | dt | value 
+----+----+-------
+(0 rows)
+
+ROLLBACK;
+/* UPDATE + FROM, partitioned table */
+EXPLAIN (COSTS OFF)
+UPDATE test.range_rel r SET value = t.value
+FROM test.tmp t WHERE r.dt = '2010-01-01' AND r.id = t.id;
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Update on range_rel_1 r
+   ->  Nested Loop
+         Join Filter: (r.id = t.id)
+         ->  Index Scan using range_rel_1_pkey on range_rel_1 r
+               Filter: (dt = 'Fri Jan 01 00:00:00 2010'::timestamp without time zone)
+         ->  Seq Scan on tmp t
+(6 rows)
+
+BEGIN;
+UPDATE test.range_rel r SET value = t.value
+FROM test.tmp t WHERE r.dt = '2010-01-01' AND r.id = t.id;
+ROLLBACK;
+/* UPDATE + FROM, single table */
+EXPLAIN (COSTS OFF)
+UPDATE test.tmp t SET value = r.value
+FROM test.range_rel r WHERE r.dt = '2010-01-01' AND r.id = t.id;
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Update on tmp t
+   ->  Nested Loop
+         ->  Seq Scan on tmp t
+         ->  Index Scan using range_rel_1_pkey on range_rel_1 r
+               Index Cond: (id = t.id)
+               Filter: (dt = 'Fri Jan 01 00:00:00 2010'::timestamp without time zone)
+(6 rows)
+
+BEGIN;
+UPDATE test.tmp t SET value = r.value
+FROM test.range_rel r WHERE r.dt = '2010-01-01' AND r.id = t.id;
+ROLLBACK;
+/* DELETE + USING, partitioned table */
+EXPLAIN (COSTS OFF)
+DELETE FROM test.range_rel r USING test.tmp t
+WHERE r.dt = '2010-01-02' AND r.id = t.id;
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Delete on range_rel_1 r
+   ->  Nested Loop
+         Join Filter: (r.id = t.id)
+         ->  Index Scan using range_rel_1_pkey on range_rel_1 r
+               Filter: (dt = 'Sat Jan 02 00:00:00 2010'::timestamp without time zone)
+         ->  Seq Scan on tmp t
+(6 rows)
+
+BEGIN;
+DELETE FROM test.range_rel r USING test.tmp t
+WHERE r.dt = '2010-01-02' AND r.id = t.id;
+ROLLBACK;
+/* DELETE + USING, single table */
+EXPLAIN (COSTS OFF)
+DELETE FROM test.tmp t USING test.range_rel r
+WHERE r.dt = '2010-01-02' AND r.id = t.id;
+                                      QUERY PLAN                                      
+--------------------------------------------------------------------------------------
+ Delete on tmp t
+   ->  Nested Loop
+         ->  Seq Scan on tmp t
+         ->  Index Scan using range_rel_1_pkey on range_rel_1 r
+               Index Cond: (id = t.id)
+               Filter: (dt = 'Sat Jan 02 00:00:00 2010'::timestamp without time zone)
+(6 rows)
+
+BEGIN;
+DELETE FROM test.tmp t USING test.range_rel r
+WHERE r.dt = '2010-01-02' AND r.id = t.id;
+ROLLBACK;
+/* DELETE + USING, two partitioned tables */
+EXPLAIN (COSTS OFF)
+DELETE FROM test.range_rel r USING test.tmp2 t
+WHERE t.id = r.id;
+ERROR:  DELETE and UPDATE queries with a join of partitioned tables are not supported
+BEGIN;
+DELETE FROM test.range_rel r USING test.tmp2 t
+WHERE t.id = r.id;
+ERROR:  DELETE and UPDATE queries with a join of partitioned tables are not supported
+ROLLBACK;
+/* DELETE + USING, partitioned table + two partitioned tables in subselect */
+EXPLAIN (COSTS OFF)
+DELETE FROM test.range_rel r
+USING (SELECT *
+	   FROM test.tmp2 a1
+	   JOIN test.tmp2 a2
+	   USING(id)) t
+WHERE t.id = r.id;
+ERROR:  DELETE and UPDATE queries with a join of partitioned tables are not supported
+BEGIN;
+DELETE FROM test.range_rel r
+USING (SELECT *
+	   FROM test.tmp2 a1
+	   JOIN test.tmp2 a2
+	   USING(id)) t
+WHERE t.id = r.id;
+ERROR:  DELETE and UPDATE queries with a join of partitioned tables are not supported
+ROLLBACK;
+/* DELETE + USING, single table + two partitioned tables in subselect */
+EXPLAIN (COSTS OFF)
+DELETE FROM test.tmp r
+USING (SELECT *
+	   FROM test.tmp2 a1
+	   JOIN test.tmp2 a2
+	   USING(id)) t
+WHERE t.id = r.id;
+                   QUERY PLAN                   
+------------------------------------------------
+ Delete on tmp r
+   ->  Nested Loop
+         ->  Nested Loop
+               ->  Seq Scan on tmp r
+               ->  Custom Scan (RuntimeAppend)
+                     Prune by: (r.id = a1.id)
+                     ->  Seq Scan on tmp2_1 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_2 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_3 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_4 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_5 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_6 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_7 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_8 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_9 a1
+                           Filter: (r.id = id)
+                     ->  Seq Scan on tmp2_10 a1
+                           Filter: (r.id = id)
+         ->  Custom Scan (RuntimeAppend)
+               Prune by: (a1.id = a2.id)
+               ->  Seq Scan on tmp2_1 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_2 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_3 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_4 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_5 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_6 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_7 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_8 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_9 a2
+                     Filter: (a1.id = id)
+               ->  Seq Scan on tmp2_10 a2
+                     Filter: (a1.id = id)
+(48 rows)
+
+BEGIN;
+DELETE FROM test.tmp r
+USING (SELECT *
+	   FROM test.tmp2 a1
+	   JOIN test.tmp2 a2
+	   USING(id)) t
+WHERE t.id = r.id;
+ROLLBACK;
+/* UPDATE + FROM, two partitioned tables */
+EXPLAIN (COSTS OFF)
+UPDATE test.range_rel r SET value = 1 FROM test.tmp2 t
+WHERE t.id = r.id;
+ERROR:  DELETE and UPDATE queries with a join of partitioned tables are not supported
+BEGIN;
+UPDATE test.range_rel r SET value = 1 FROM test.tmp2 t
+WHERE t.id = r.id;
+ERROR:  DELETE and UPDATE queries with a join of partitioned tables are not supported
+ROLLBACK;
+/*
+ * UPDATE + subquery with partitioned table (PG 9.5).
+ * See pathman_rel_pathlist_hook() + RELOPT_OTHER_MEMBER_REL.
+ */
+EXPLAIN (COSTS OFF)
+UPDATE test.tmp t SET value = 2
+WHERE t.id IN (SELECT id
+			   FROM test.tmp2 t2
+			   WHERE id = t.id);
+                 QUERY PLAN                 
+--------------------------------------------
+ Update on tmp t
+   ->  Seq Scan on tmp t
+         Filter: (SubPlan 1)
+         SubPlan 1
+           ->  Custom Scan (RuntimeAppend)
+                 Prune by: (t2.id = t.id)
+                 ->  Seq Scan on tmp2_1 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_2 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_3 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_4 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_5 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_6 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_7 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_8 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_9 t2
+                       Filter: (id = t.id)
+                 ->  Seq Scan on tmp2_10 t2
+                       Filter: (id = t.id)
+(26 rows)
+
+/* Test special rule for CTE; SELECT (PostgreSQL 9.5) */
+EXPLAIN (COSTS OFF)
+WITH q AS (SELECT * FROM test.range_rel r
+		   WHERE r.dt = '2010-01-02')
+DELETE FROM test.tmp USING q;
+                                         QUERY PLAN                                         
+--------------------------------------------------------------------------------------------
+ Delete on tmp
+   ->  Nested Loop
+         ->  Seq Scan on tmp
+         ->  Materialize
+               ->  Seq Scan on range_rel_1 r
+                     Filter: (dt = 'Sat Jan 02 00:00:00 2010'::timestamp without time zone)
+(6 rows)
+
+BEGIN;
+WITH q AS (SELECT * FROM test.range_rel r
+		   WHERE r.dt = '2010-01-02')
+DELETE FROM test.tmp USING q;
+ROLLBACK;
+/* Test special rule for CTE; DELETE (PostgreSQL 9.5) */
+EXPLAIN (COSTS OFF)
+WITH q AS (DELETE FROM test.range_rel r
+		   WHERE r.dt = '2010-01-02'
+		   RETURNING *)
+DELETE FROM test.tmp USING q;
+                                       QUERY PLAN                                       
+----------------------------------------------------------------------------------------
+ Delete on tmp
+   CTE q
+     ->  Delete on range_rel_1 r
+           ->  Seq Scan on range_rel_1 r
+                 Filter: (dt = 'Sat Jan 02 00:00:00 2010'::timestamp without time zone)
+   ->  Nested Loop
+         ->  Seq Scan on tmp
+         ->  CTE Scan on q
+(8 rows)
+
+BEGIN;
+WITH q AS (DELETE FROM test.range_rel r
+		   WHERE r.dt = '2010-01-02'
+		   RETURNING *)
+DELETE FROM test.tmp USING q;
+ROLLBACK;
+/* Test special rule for CTE; DELETE + USING (PostgreSQL 9.5) */
+EXPLAIN (COSTS OFF)
+WITH q AS (DELETE FROM test.tmp t
+		   USING test.range_rel r
+		   WHERE r.dt = '2010-01-02' AND r.id = t.id
+		   RETURNING *)
+DELETE FROM test.tmp USING q;
+                                          QUERY PLAN                                          
+----------------------------------------------------------------------------------------------
+ Delete on tmp
+   CTE q
+     ->  Delete on tmp t
+           ->  Nested Loop
+                 ->  Seq Scan on tmp t
+                 ->  Index Scan using range_rel_1_pkey on range_rel_1 r
+                       Index Cond: (id = t.id)
+                       Filter: (dt = 'Sat Jan 02 00:00:00 2010'::timestamp without time zone)
+   ->  Nested Loop
+         ->  Seq Scan on tmp
+         ->  CTE Scan on q
+(11 rows)
+
+BEGIN;
+WITH q AS (DELETE FROM test.tmp t
+		   USING test.range_rel r
+		   WHERE r.dt = '2010-01-02' AND r.id = t.id
+		   RETURNING *)
+DELETE FROM test.tmp USING q;
+ROLLBACK;
+/* Test special rule for CTE; Nested CTEs (PostgreSQL 9.5) */
+EXPLAIN (COSTS OFF)
+WITH q AS (WITH n AS (SELECT id FROM test.tmp2 WHERE id = 2)
+		   DELETE FROM test.tmp t
+		   USING n
+		   WHERE t.id = n.id
+		   RETURNING *)
+DELETE FROM test.tmp USING q;
+                 QUERY PLAN                  
+---------------------------------------------
+ Delete on tmp
+   CTE q
+     ->  Delete on tmp t
+           ->  Nested Loop
+                 ->  Seq Scan on tmp t
+                       Filter: (id = 2)
+                 ->  Seq Scan on tmp2_2 tmp2
+                       Filter: (id = 2)
+   ->  Nested Loop
+         ->  Seq Scan on tmp
+         ->  CTE Scan on q
+(11 rows)
+
+/* Test special rule for CTE; CTE in quals (PostgreSQL 9.5) */
+EXPLAIN (COSTS OFF)
+WITH q AS (SELECT id FROM test.tmp2
+		   WHERE id < 3)
+DELETE FROM test.tmp t WHERE t.id in (SELECT id FROM q);
+                          QUERY PLAN                          
+--------------------------------------------------------------
+ Delete on tmp t
+   ->  Nested Loop Semi Join
+         ->  Seq Scan on tmp t
+         ->  Custom Scan (RuntimeAppend)
+               Prune by: ((tmp2.id < 3) AND (t.id = tmp2.id))
+               ->  Seq Scan on tmp2_1 tmp2
+                     Filter: (t.id = id)
+               ->  Seq Scan on tmp2_2 tmp2
+                     Filter: (t.id = id)
+(9 rows)
+
+BEGIN;
+WITH q AS (SELECT id FROM test.tmp2
+		   WHERE id < 3)
+DELETE FROM test.tmp t WHERE t.id in (SELECT id FROM q);
+ROLLBACK;
+DROP TABLE test.tmp CASCADE;
+DROP TABLE test.tmp2 CASCADE;
+NOTICE:  drop cascades to 11 other objects
+DROP TABLE test.range_rel CASCADE;
+NOTICE:  drop cascades to 13 other objects
+DROP SCHEMA test;
+DROP EXTENSION pg_pathman CASCADE;
+DROP SCHEMA pathman;

From 4058dde01aca9c920f2479628767d234a2e5a47a Mon Sep 17 00:00:00 2001
From: Vlada Pogozhelskaya <v.pogozhelskaya@postgrespro.ru>
Date: Mon, 21 Oct 2024 17:04:36 +0300
Subject: [PATCH 5/5] [PGPRO-10100] Fixed regression test in pathman_upd_del

---
 expected/pathman_upd_del_4.out | 46 +++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/expected/pathman_upd_del_4.out b/expected/pathman_upd_del_4.out
index 70b41e7d..8932a23e 100644
--- a/expected/pathman_upd_del_4.out
+++ b/expected/pathman_upd_del_4.out
@@ -23,7 +23,7 @@ INSERT INTO test.tmp VALUES (1, 1), (2, 2);
 CREATE TABLE test.tmp2 (id INTEGER NOT NULL, value INTEGER NOT NULL);
 INSERT INTO test.tmp2 SELECT i % 10 + 1, i FROM generate_series(1, 100) i;
 SELECT pathman.create_range_partitions('test.tmp2', 'id', 1, 1, 10);
- create_range_partitions 
+ create_range_partitions
 -------------------------
                       10
 (1 row)
@@ -38,7 +38,7 @@ FROM generate_series('2010-01-01'::date, '2010-12-31'::date, '1 day') AS g;
 SELECT pathman.create_range_partitions('test.range_rel', 'dt',
 									   '2010-01-01'::date, '1 month'::interval,
 									   12);
- create_range_partitions 
+ create_range_partitions
 -------------------------
                       12
 (1 row)
@@ -49,7 +49,7 @@ VACUUM ANALYZE;
  */
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Update on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -59,7 +59,7 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id  |            dt            | value 
+ id  |            dt            | value
 -----+--------------------------+-------
  166 | Tue Jun 15 00:00:00 2010 |   111
 (1 row)
@@ -67,7 +67,7 @@ SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
 ROLLBACK;
 /* have partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Delete on range_rel_6
    ->  Seq Scan on range_rel_6
@@ -77,14 +77,14 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt = '2010-06-15';
 SELECT * FROM test.range_rel WHERE dt = '2010-06-15';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Update on range_rel
    ->  Seq Scan on range_rel
@@ -94,14 +94,14 @@ EXPLAIN (COSTS OFF) UPDATE test.range_rel SET value = 222 WHERE dt = '1990-01-01
 BEGIN;
 UPDATE test.range_rel SET value = 111 WHERE dt = '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt = '1990-01-01';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
 ROLLBACK;
 /* no partitions for this 'dt' */
 EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
-                                   QUERY PLAN                                   
+                                   QUERY PLAN
 --------------------------------------------------------------------------------
  Delete on range_rel
    ->  Seq Scan on range_rel
@@ -111,7 +111,7 @@ EXPLAIN (COSTS OFF) DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 BEGIN;
 DELETE FROM test.range_rel WHERE dt < '1990-01-01';
 SELECT * FROM test.range_rel WHERE dt < '1990-01-01';
- id | dt | value 
+ id | dt | value
 ----+----+-------
 (0 rows)
 
@@ -120,7 +120,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.range_rel r SET value = t.value
 FROM test.tmp t WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Update on range_rel_1 r
    ->  Nested Loop
@@ -138,7 +138,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 UPDATE test.tmp t SET value = r.value
 FROM test.range_rel r WHERE r.dt = '2010-01-01' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Update on tmp t
    ->  Nested Loop
@@ -156,7 +156,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.range_rel r USING test.tmp t
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Delete on range_rel_1 r
    ->  Nested Loop
@@ -174,7 +174,7 @@ ROLLBACK;
 EXPLAIN (COSTS OFF)
 DELETE FROM test.tmp t USING test.range_rel r
 WHERE r.dt = '2010-01-02' AND r.id = t.id;
-                                      QUERY PLAN                                      
+                                      QUERY PLAN
 --------------------------------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop
@@ -224,7 +224,7 @@ USING (SELECT *
 	   JOIN test.tmp2 a2
 	   USING(id)) t
 WHERE t.id = r.id;
-                   QUERY PLAN                   
+                   QUERY PLAN
 ------------------------------------------------
  Delete on tmp r
    ->  Nested Loop
@@ -303,7 +303,7 @@ UPDATE test.tmp t SET value = 2
 WHERE t.id IN (SELECT id
 			   FROM test.tmp2 t2
 			   WHERE id = t.id);
-                 QUERY PLAN                 
+                 QUERY PLAN
 --------------------------------------------
  Update on tmp t
    ->  Seq Scan on tmp t
@@ -338,7 +338,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT * FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02')
 DELETE FROM test.tmp USING q;
-                                         QUERY PLAN                                         
+                                         QUERY PLAN
 --------------------------------------------------------------------------------------------
  Delete on tmp
    ->  Nested Loop
@@ -359,7 +359,7 @@ WITH q AS (DELETE FROM test.range_rel r
 		   WHERE r.dt = '2010-01-02'
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                       QUERY PLAN                                       
+                                       QUERY PLAN
 ----------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -384,7 +384,7 @@ WITH q AS (DELETE FROM test.tmp t
 		   WHERE r.dt = '2010-01-02' AND r.id = t.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                                          QUERY PLAN                                          
+                                          QUERY PLAN
 ----------------------------------------------------------------------------------------------
  Delete on tmp
    CTE q
@@ -414,15 +414,15 @@ WITH q AS (WITH n AS (SELECT id FROM test.tmp2 WHERE id = 2)
 		   WHERE t.id = n.id
 		   RETURNING *)
 DELETE FROM test.tmp USING q;
-                 QUERY PLAN                  
----------------------------------------------
+               QUERY PLAN
+----------------------------------------
  Delete on tmp
    CTE q
      ->  Delete on tmp t
            ->  Nested Loop
                  ->  Seq Scan on tmp t
                        Filter: (id = 2)
-                 ->  Seq Scan on tmp2_2 tmp2
+                 ->  Seq Scan on tmp2_2
                        Filter: (id = 2)
    ->  Nested Loop
          ->  Seq Scan on tmp
@@ -434,7 +434,7 @@ EXPLAIN (COSTS OFF)
 WITH q AS (SELECT id FROM test.tmp2
 		   WHERE id < 3)
 DELETE FROM test.tmp t WHERE t.id in (SELECT id FROM q);
-                          QUERY PLAN                          
+                          QUERY PLAN
 --------------------------------------------------------------
  Delete on tmp t
    ->  Nested Loop Semi Join