Skip to content

Commit 2799075

Browse files
committed
Fix EXPLAIN for ConstraintAware and MergeAppend
This fixes a bug with an explain for ConstraintAware plans when all chunks were excluded and a MergeAppend was used. The problem was that the EXPLAIN output for MergeAppend nodes expected at least one child to print the sorting column info. When all chunks were excluded there are no children. This PR removes all child execution states from the ConstraintAware node when all chunks are excluded.
1 parent 8084594 commit 2799075

File tree

7 files changed

+300
-207
lines changed

7 files changed

+300
-207
lines changed

src/constraint_aware_append.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ ca_append_begin(CustomScanState *node, EState *estate, int eflags)
186186
}
187187

188188
state->num_append_subplans = list_length(*appendplans);
189-
node->custom_ps = list_make1(ExecInitNode(subplan, estate, eflags));
189+
if(state->num_append_subplans > 0)
190+
node->custom_ps = list_make1(ExecInitNode(subplan, estate, eflags));
190191
}
191192

192193
static TupleTableSlot *
@@ -242,7 +243,10 @@ ca_append_exec(CustomScanState *node)
242243
static void
243244
ca_append_end(CustomScanState *node)
244245
{
245-
ExecEndNode(linitial(node->custom_ps));
246+
if (node->custom_ps != NIL)
247+
{
248+
ExecEndNode(linitial(node->custom_ps));
249+
}
246250
}
247251

248252
static void
@@ -258,10 +262,12 @@ ca_append_explain(CustomScanState *node,
258262
ExplainState *es)
259263
{
260264
CustomScan *cscan = (CustomScan *) node->ss.ps.plan;
265+
ConstraintAwareAppendState *state = (ConstraintAwareAppendState *) node;
261266
Index rti = linitial_int(linitial(cscan->custom_private));
262267
RangeTblEntry *rte = rt_fetch(rti, es->rtable);
263268

264269
ExplainPropertyText("Hypertable", get_rel_name(rte->relid), es);
270+
ExplainPropertyInteger("Chunks left after exclusion", state->num_append_subplans, es);
265271
}
266272

267273

test/expected/append.out

Lines changed: 104 additions & 80 deletions
Large diffs are not rendered by default.

test/expected/append_unoptimized.out

Lines changed: 95 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,39 @@ psql:include/append.sql:45: NOTICE: Stable function now_s() called!
7171
------+------+---------
7272
(0 rows)
7373

74+
--query should exclude all chunks and be a MergeAppend
75+
EXPLAIN (costs off)
76+
SELECT * FROM append_test WHERE time > now_s() + '1 month'
77+
ORDER BY time DESC limit 1;
78+
psql:include/append.sql:50: NOTICE: Stable function now_s() called!
79+
psql:include/append.sql:50: NOTICE: Stable function now_s() called!
80+
psql:include/append.sql:50: NOTICE: Stable function now_s() called!
81+
psql:include/append.sql:50: NOTICE: Stable function now_s() called!
82+
QUERY PLAN
83+
----------------------------------------------------------------------------------------
84+
Limit
85+
-> Merge Append
86+
Sort Key: append_test."time" DESC
87+
-> Index Scan using append_test_time_idx on append_test
88+
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
89+
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
90+
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
91+
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
92+
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
93+
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
94+
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
95+
(11 rows)
96+
7497
-- when optimized, the plan should be a constraint-aware append and
7598
-- cover only one chunk. It should be a backward index scan due to
7699
-- descending index on time. Should also skip the main table, since it
77100
-- cannot hold tuples
78101
EXPLAIN (costs off)
79102
SELECT * FROM append_test WHERE time > now_s() - interval '2 months';
80-
psql:include/append.sql:52: NOTICE: Stable function now_s() called!
81-
psql:include/append.sql:52: NOTICE: Stable function now_s() called!
82-
psql:include/append.sql:52: NOTICE: Stable function now_s() called!
83-
psql:include/append.sql:52: NOTICE: Stable function now_s() called!
103+
psql:include/append.sql:57: NOTICE: Stable function now_s() called!
104+
psql:include/append.sql:57: NOTICE: Stable function now_s() called!
105+
psql:include/append.sql:57: NOTICE: Stable function now_s() called!
106+
psql:include/append.sql:57: NOTICE: Stable function now_s() called!
84107
QUERY PLAN
85108
----------------------------------------------------------------------------------
86109
Append
@@ -96,13 +119,13 @@ psql:include/append.sql:52: NOTICE: Stable function now_s() called!
96119

97120
-- the expected output should be the same as the non-optimized query
98121
SELECT * FROM append_test WHERE time > now_s() - interval '2 months';
99-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
100-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
101-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
102-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
103-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
104-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
105-
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
122+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
123+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
124+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
125+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
126+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
127+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
128+
psql:include/append.sql:60: NOTICE: Stable function now_s() called!
106129
time | temp | colorid
107130
--------------------------+------+---------
108131
Tue Aug 22 09:18:22 2017 | 34.1 | 3
@@ -113,10 +136,10 @@ psql:include/append.sql:55: NOTICE: Stable function now_s() called!
113136
EXPLAIN (costs off)
114137
SELECT * FROM append_test WHERE time > now_s() - interval '2 months'
115138
ORDER BY time LIMIT 3;
116-
psql:include/append.sql:61: NOTICE: Stable function now_s() called!
117-
psql:include/append.sql:61: NOTICE: Stable function now_s() called!
118-
psql:include/append.sql:61: NOTICE: Stable function now_s() called!
119-
psql:include/append.sql:61: NOTICE: Stable function now_s() called!
139+
psql:include/append.sql:66: NOTICE: Stable function now_s() called!
140+
psql:include/append.sql:66: NOTICE: Stable function now_s() called!
141+
psql:include/append.sql:66: NOTICE: Stable function now_s() called!
142+
psql:include/append.sql:66: NOTICE: Stable function now_s() called!
120143
QUERY PLAN
121144
-------------------------------------------------------------------------------------------------
122145
Limit
@@ -135,14 +158,14 @@ psql:include/append.sql:61: NOTICE: Stable function now_s() called!
135158
-- the expected output should be the same as the non-optimized query
136159
SELECT * FROM append_test WHERE time > now_s() - interval '2 months'
137160
ORDER BY time LIMIT 3;
138-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
139-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
140-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
141-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
142-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
143-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
144-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
145-
psql:include/append.sql:65: NOTICE: Stable function now_s() called!
161+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
162+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
163+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
164+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
165+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
166+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
167+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
168+
psql:include/append.sql:70: NOTICE: Stable function now_s() called!
146169
time | temp | colorid
147170
--------------------------+------+---------
148171
Tue Aug 22 09:18:22 2017 | 34.1 | 3
@@ -154,8 +177,8 @@ psql:include/append.sql:65: NOTICE: Stable function now_s() called!
154177
EXPLAIN (costs off)
155178
SELECT * FROM append_test WHERE time > now_i() - interval '2 months'
156179
ORDER BY time;
157-
psql:include/append.sql:72: NOTICE: Immutable function now_i() called!
158-
psql:include/append.sql:72: NOTICE: Immutable function now_i() called!
180+
psql:include/append.sql:77: NOTICE: Immutable function now_i() called!
181+
psql:include/append.sql:77: NOTICE: Immutable function now_i() called!
159182
QUERY PLAN
160183
----------------------------------------------------------------------------------------------------
161184
Sort
@@ -193,11 +216,11 @@ ORDER BY time;
193216

194217
SELECT * FROM append_test WHERE time > now_v() - interval '2 months'
195218
ORDER BY time;
196-
psql:include/append.sql:83: NOTICE: Volatile function now_v() called!
197-
psql:include/append.sql:83: NOTICE: Volatile function now_v() called!
198-
psql:include/append.sql:83: NOTICE: Volatile function now_v() called!
199-
psql:include/append.sql:83: NOTICE: Volatile function now_v() called!
200-
psql:include/append.sql:83: NOTICE: Volatile function now_v() called!
219+
psql:include/append.sql:88: NOTICE: Volatile function now_v() called!
220+
psql:include/append.sql:88: NOTICE: Volatile function now_v() called!
221+
psql:include/append.sql:88: NOTICE: Volatile function now_v() called!
222+
psql:include/append.sql:88: NOTICE: Volatile function now_v() called!
223+
psql:include/append.sql:88: NOTICE: Volatile function now_v() called!
201224
time | temp | colorid
202225
--------------------------+------+---------
203226
Tue Aug 22 09:18:22 2017 | 34.1 | 3
@@ -209,14 +232,14 @@ PREPARE query_opt AS
209232
SELECT * FROM append_test WHERE time > now_s() - interval '2 months'
210233
ORDER BY time;
211234
EXECUTE query_opt;
212-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
213-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
214-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
215-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
216-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
217-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
218-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
219-
psql:include/append.sql:91: NOTICE: Stable function now_s() called!
235+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
236+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
237+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
238+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
239+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
240+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
241+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
242+
psql:include/append.sql:96: NOTICE: Stable function now_s() called!
220243
time | temp | colorid
221244
--------------------------+------+---------
222245
Tue Aug 22 09:18:22 2017 | 34.1 | 3
@@ -243,13 +266,13 @@ SELECT date_trunc('year', time) t, avg(temp) FROM append_test
243266
WHERE time > now_s() - interval '4 months'
244267
GROUP BY t
245268
ORDER BY t DESC;
246-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
247-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
248-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
249-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
250-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
251-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
252-
psql:include/append.sql:100: NOTICE: Stable function now_s() called!
269+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
270+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
271+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
272+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
273+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
274+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
275+
psql:include/append.sql:105: NOTICE: Stable function now_s() called!
253276
t | avg
254277
--------------------------+------
255278
Sun Jan 01 00:00:00 2017 | 28.5
@@ -261,10 +284,10 @@ SELECT date_trunc('year', time) t, avg(temp) FROM append_test
261284
WHERE time > now_s() - interval '4 months'
262285
GROUP BY t
263286
ORDER BY t DESC;
264-
psql:include/append.sql:107: NOTICE: Stable function now_s() called!
265-
psql:include/append.sql:107: NOTICE: Stable function now_s() called!
266-
psql:include/append.sql:107: NOTICE: Stable function now_s() called!
267-
psql:include/append.sql:107: NOTICE: Stable function now_s() called!
287+
psql:include/append.sql:112: NOTICE: Stable function now_s() called!
288+
psql:include/append.sql:112: NOTICE: Stable function now_s() called!
289+
psql:include/append.sql:112: NOTICE: Stable function now_s() called!
290+
psql:include/append.sql:112: NOTICE: Stable function now_s() called!
268291
QUERY PLAN
269292
----------------------------------------------------------------------------------------------------
270293
Sort
@@ -290,7 +313,7 @@ PREPARE query_param AS
290313
SELECT * FROM append_test WHERE time > $1 ORDER BY time;
291314
EXPLAIN (costs off)
292315
EXECUTE query_param(now_s() - interval '2 months');
293-
psql:include/append.sql:116: NOTICE: Stable function now_s() called!
316+
psql:include/append.sql:121: NOTICE: Stable function now_s() called!
294317
QUERY PLAN
295318
----------------------------------------------------------------------------------------------------
296319
Sort
@@ -324,14 +347,14 @@ set enable_material = 'off';
324347
EXPLAIN (costs off)
325348
SELECT * FROM append_test a INNER JOIN join_test j ON (a.colorid = j.colorid)
326349
WHERE a.time > now_s() - interval '3 hours' AND j.time > now_s() - interval '3 hours';
327-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
328-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
329-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
330-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
331-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
332-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
333-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
334-
psql:include/append.sql:136: NOTICE: Stable function now_s() called!
350+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
351+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
352+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
353+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
354+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
355+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
356+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
357+
psql:include/append.sql:141: NOTICE: Stable function now_s() called!
335358
QUERY PLAN
336359
--------------------------------------------------------------------------------------------
337360
Nested Loop
@@ -359,20 +382,20 @@ psql:include/append.sql:136: NOTICE: Stable function now_s() called!
359382
-- result should be the same as when optimizations are turned off
360383
SELECT * FROM append_test a INNER JOIN join_test j ON (a.colorid = j.colorid)
361384
WHERE a.time > now_s() - interval '3 hours' AND j.time > now_s() - interval '3 hours';
362-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
363-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
364-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
365-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
366-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
367-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
368-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
369-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
370-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
371-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
372-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
373-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
374-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
375-
psql:include/append.sql:140: NOTICE: Stable function now_s() called!
385+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
386+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
387+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
388+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
389+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
390+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
391+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
392+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
393+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
394+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
395+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
396+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
397+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
398+
psql:include/append.sql:145: NOTICE: Stable function now_s() called!
376399
time | temp | colorid | time | temp | colorid
377400
--------------------------+------+---------+--------------------------+------+---------
378401
Tue Aug 22 09:18:22 2017 | 34.1 | 3 | Tue Aug 22 09:18:22 2017 | 23.1 | 3

0 commit comments

Comments
 (0)