Skip to content

Commit 2c424c2

Browse files
authored
Remove wrong assertion in first/last initplan optimization (#9724)
It did not correctly handle the case of SELECT FROM ONLY hypertable. Besides that, the code that restored RangeTblEntry.inh was targeting the wrong PlannerInfo -- the one from original query instead of the copy used for the optimization. Remove this code as well, since it doesn't do anything useful.
1 parent 900c0ab commit 2c424c2

6 files changed

Lines changed: 116 additions & 12 deletions

File tree

src/planner/agg_bookend.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,6 @@ build_first_last_path(PlannerInfo *root, FirstLastAggInfo *fl_info, Oid eqop, Oi
723723
{
724724
ListCell *prev = NULL;
725725
ListCell *next = list_head(subroot->append_rel_list);
726-
Assert(rte->inh);
727726
rte->inh = false;
728727
/* query planner gets confused when entries in the
729728
* append_rel_list refer to entries in the relarray that
@@ -757,17 +756,6 @@ build_first_last_path(PlannerInfo *root, FirstLastAggInfo *fl_info, Oid eqop, Oi
757756

758757
final_rel = query_planner(subroot, first_last_qp_callback, NULL);
759758

760-
/* we need to disable inheritance so the chunks are re-expanded correctly in the subroot */
761-
foreach (lc, root->parse->rtable)
762-
{
763-
RangeTblEntry *rte = lfirst_node(RangeTblEntry, lc);
764-
765-
if (ts_rte_is_hypertable(rte))
766-
{
767-
rte->inh = true;
768-
}
769-
}
770-
771759
/*
772760
* Since we didn't go through subquery_planner() to handle the subquery,
773761
* we have to do some of the same cleanup it would do, in particular cope

test/expected/agg_bookends-15.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
501501
-> Seq Scan on _hyper_1_4_chunk (actual rows=1.00 loops=1)
502502
-> Seq Scan on _hyper_1_5_chunk (actual rows=1.00 loops=1)
503503

504+
-- parent-only scan
505+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
506+
--- QUERY PLAN ---
507+
Aggregate (actual rows=1.00 loops=1)
508+
-> Seq Scan on btest (actual rows=0.00 loops=1)
509+
510+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
511+
--- QUERY PLAN ---
512+
Aggregate (actual rows=1.00 loops=1)
513+
-> Seq Scan on btest (actual rows=0.00 loops=1)
514+
504515
-- test constants
505516
:PREFIX SELECT first(100, 100) FROM btest;
506517
--- QUERY PLAN ---
@@ -940,6 +951,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
940951
2 | 35.3
941952
2 | 35.3
942953

954+
-- parent-only scan
955+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
956+
first
957+
-------
958+
959+
960+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
961+
last
962+
------
963+
964+
943965
-- test constants
944966
:PREFIX SELECT first(100, 100) FROM btest;
945967
first
@@ -1132,6 +1154,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
11321154
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
11331155
-- can't do index scan when using WINDOW function
11341156
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1157+
-- parent-only scan
1158+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1159+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
11351160
-- test constants
11361161
:PREFIX SELECT first(100, 100) FROM btest;
11371162
-- create an index so we can test optimization
@@ -1252,6 +1277,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
12521277
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
12531278
-- can't do index scan when using WINDOW function
12541279
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1280+
-- parent-only scan
1281+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1282+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
12551283
-- test constants
12561284
:PREFIX SELECT first(100, 100) FROM btest;
12571285
-- create an index so we can test optimization

test/expected/agg_bookends-16.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
501501
-> Seq Scan on _hyper_1_4_chunk (actual rows=1.00 loops=1)
502502
-> Seq Scan on _hyper_1_5_chunk (actual rows=1.00 loops=1)
503503

504+
-- parent-only scan
505+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
506+
--- QUERY PLAN ---
507+
Aggregate (actual rows=1.00 loops=1)
508+
-> Seq Scan on btest (actual rows=0.00 loops=1)
509+
510+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
511+
--- QUERY PLAN ---
512+
Aggregate (actual rows=1.00 loops=1)
513+
-> Seq Scan on btest (actual rows=0.00 loops=1)
514+
504515
-- test constants
505516
:PREFIX SELECT first(100, 100) FROM btest;
506517
--- QUERY PLAN ---
@@ -940,6 +951,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
940951
2 | 35.3
941952
2 | 35.3
942953

954+
-- parent-only scan
955+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
956+
first
957+
-------
958+
959+
960+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
961+
last
962+
------
963+
964+
943965
-- test constants
944966
:PREFIX SELECT first(100, 100) FROM btest;
945967
first
@@ -1132,6 +1154,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
11321154
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
11331155
-- can't do index scan when using WINDOW function
11341156
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1157+
-- parent-only scan
1158+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1159+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
11351160
-- test constants
11361161
:PREFIX SELECT first(100, 100) FROM btest;
11371162
-- create an index so we can test optimization
@@ -1252,6 +1277,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
12521277
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
12531278
-- can't do index scan when using WINDOW function
12541279
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1280+
-- parent-only scan
1281+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1282+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
12551283
-- test constants
12561284
:PREFIX SELECT first(100, 100) FROM btest;
12571285
-- create an index so we can test optimization

test/expected/agg_bookends-17.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
461461
-> Seq Scan on _hyper_1_4_chunk (actual rows=1.00 loops=1)
462462
-> Seq Scan on _hyper_1_5_chunk (actual rows=1.00 loops=1)
463463

464+
-- parent-only scan
465+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
466+
--- QUERY PLAN ---
467+
Aggregate (actual rows=1.00 loops=1)
468+
-> Seq Scan on btest (actual rows=0.00 loops=1)
469+
470+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
471+
--- QUERY PLAN ---
472+
Aggregate (actual rows=1.00 loops=1)
473+
-> Seq Scan on btest (actual rows=0.00 loops=1)
474+
464475
-- test constants
465476
:PREFIX SELECT first(100, 100) FROM btest;
466477
--- QUERY PLAN ---
@@ -890,6 +901,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
890901
2 | 35.3
891902
2 | 35.3
892903

904+
-- parent-only scan
905+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
906+
first
907+
-------
908+
909+
910+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
911+
last
912+
------
913+
914+
893915
-- test constants
894916
:PREFIX SELECT first(100, 100) FROM btest;
895917
first
@@ -1082,6 +1104,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
10821104
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
10831105
-- can't do index scan when using WINDOW function
10841106
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1107+
-- parent-only scan
1108+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1109+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
10851110
-- test constants
10861111
:PREFIX SELECT first(100, 100) FROM btest;
10871112
-- create an index so we can test optimization
@@ -1202,6 +1227,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
12021227
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
12031228
-- can't do index scan when using WINDOW function
12041229
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1230+
-- parent-only scan
1231+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1232+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
12051233
-- test constants
12061234
:PREFIX SELECT first(100, 100) FROM btest;
12071235
-- create an index so we can test optimization

test/expected/agg_bookends-18.out

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
461461
-> Seq Scan on _hyper_1_4_chunk (actual rows=1.00 loops=1)
462462
-> Seq Scan on _hyper_1_5_chunk (actual rows=1.00 loops=1)
463463

464+
-- parent-only scan
465+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
466+
--- QUERY PLAN ---
467+
Aggregate (actual rows=1.00 loops=1)
468+
-> Seq Scan on btest (actual rows=0.00 loops=1)
469+
470+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
471+
--- QUERY PLAN ---
472+
Aggregate (actual rows=1.00 loops=1)
473+
-> Seq Scan on btest (actual rows=0.00 loops=1)
474+
464475
-- test constants
465476
:PREFIX SELECT first(100, 100) FROM btest;
466477
--- QUERY PLAN ---
@@ -890,6 +901,17 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
890901
2 | 35.3
891902
2 | 35.3
892903

904+
-- parent-only scan
905+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
906+
first
907+
-------
908+
909+
910+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
911+
last
912+
------
913+
914+
893915
-- test constants
894916
:PREFIX SELECT first(100, 100) FROM btest;
895917
first
@@ -1082,6 +1104,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
10821104
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
10831105
-- can't do index scan when using WINDOW function
10841106
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1107+
-- parent-only scan
1108+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1109+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
10851110
-- test constants
10861111
:PREFIX SELECT first(100, 100) FROM btest;
10871112
-- create an index so we can test optimization
@@ -1202,6 +1227,9 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
12021227
:PREFIX SELECT first(temp, time) FROM btest WHERE time >= '2017-01-20 09:00:47';
12031228
-- can't do index scan when using WINDOW function
12041229
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
1230+
-- parent-only scan
1231+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
1232+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
12051233
-- test constants
12061234
:PREFIX SELECT first(100, 100) FROM btest;
12071235
-- create an index so we can test optimization

test/sql/include/agg_bookends_query.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ INSERT INTO btest_numeric VALUES('2020-01-20T09:00:43', 30.5);
103103
-- can't do index scan when using WINDOW function
104104
:PREFIX SELECT gp, last(temp, time) OVER (PARTITION BY gp) AS last FROM btest;
105105

106+
-- parent-only scan
107+
:PREFIX SELECT first(temp, time) FROM ONLY btest;
108+
:PREFIX SELECT last(temp, time) FROM ONLY btest;
109+
106110
-- test constants
107111
:PREFIX SELECT first(100, 100) FROM btest;
108112

0 commit comments

Comments
 (0)