mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-28 09:46:44 +08:00
An attempt to unify the error messages to conform with the PostgreSQL error messages style guide. See the link below: https://www.postgresql.org/docs/current/static/error-style-guide.html
520 lines
26 KiB
Plaintext
520 lines
26 KiB
Plaintext
SET timescaledb.disable_optimizations = ON;
|
|
\ir include/append.sql
|
|
-- create a now() function for repeatable testing that always returns
|
|
-- the same timestamp. It needs to be marked STABLE
|
|
CREATE OR REPLACE FUNCTION now_s()
|
|
RETURNS timestamptz LANGUAGE PLPGSQL STABLE AS
|
|
$BODY$
|
|
BEGIN
|
|
RAISE NOTICE 'Stable function now_s() called!';
|
|
RETURN '2017-08-22T10:00:00'::timestamptz;
|
|
END;
|
|
$BODY$;
|
|
CREATE OR REPLACE FUNCTION now_i()
|
|
RETURNS timestamptz LANGUAGE PLPGSQL IMMUTABLE AS
|
|
$BODY$
|
|
BEGIN
|
|
RAISE NOTICE 'Immutable function now_i() called!';
|
|
RETURN '2017-08-22T10:00:00'::timestamptz;
|
|
END;
|
|
$BODY$;
|
|
CREATE OR REPLACE FUNCTION now_v()
|
|
RETURNS timestamptz LANGUAGE PLPGSQL VOLATILE AS
|
|
$BODY$
|
|
BEGIN
|
|
RAISE NOTICE 'Volatile function now_v() called!';
|
|
RETURN '2017-08-22T10:00:00'::timestamptz;
|
|
END;
|
|
$BODY$;
|
|
CREATE TABLE append_test(time timestamptz, temp float, colorid integer);
|
|
SELECT create_hypertable('append_test', 'time', chunk_time_interval => 2628000000000);
|
|
psql:include/append.sql:31: NOTICE: adding not-null constraint to column "time"
|
|
create_hypertable
|
|
-------------------
|
|
|
|
(1 row)
|
|
|
|
-- create three chunks
|
|
INSERT INTO append_test VALUES ('2017-03-22T09:18:22', 23.5, 1),
|
|
('2017-03-22T09:18:23', 21.5, 1),
|
|
('2017-05-22T09:18:22', 36.2, 2),
|
|
('2017-05-22T09:18:23', 15.2, 2),
|
|
('2017-08-22T09:18:22', 34.1, 3);
|
|
-- query should exclude all chunks with optimization on
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test WHERE time > now_s() + '1 month';
|
|
psql:include/append.sql:42: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:42: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:42: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:42: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
----------------------------------------------------------------------------------
|
|
Append
|
|
-> Seq Scan on append_test
|
|
Filter: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
(9 rows)
|
|
|
|
SELECT * FROM append_test WHERE time > now_s() + '1 month';
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:43: NOTICE: Stable function now_s() called!
|
|
time | temp | colorid
|
|
------+------+---------
|
|
(0 rows)
|
|
|
|
--query should exclude all chunks and be a MergeAppend
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test WHERE time > now_s() + '1 month'
|
|
ORDER BY time DESC limit 1;
|
|
psql:include/append.sql:48: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:48: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:48: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:48: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
----------------------------------------------------------------------------------------
|
|
Limit
|
|
-> Merge Append
|
|
Sort Key: append_test."time" DESC
|
|
-> Index Scan using append_test_time_idx on append_test
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() + '@ 1 mon'::interval))
|
|
(11 rows)
|
|
|
|
-- when optimized, the plan should be a constraint-aware append and
|
|
-- cover only one chunk. It should be a backward index scan due to
|
|
-- descending index on time. Should also skip the main table, since it
|
|
-- cannot hold tuples
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test WHERE time > now_s() - interval '2 months';
|
|
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:55: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
----------------------------------------------------------------------------------
|
|
Append
|
|
-> Seq Scan on append_test
|
|
Filter: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
(9 rows)
|
|
|
|
-- the expected output should be the same as the non-optimized query
|
|
SELECT * FROM append_test WHERE time > now_s() - interval '2 months';
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:58: NOTICE: Stable function now_s() called!
|
|
time | temp | colorid
|
|
------------------------------+------+---------
|
|
Tue Aug 22 09:18:22 2017 PDT | 34.1 | 3
|
|
(1 row)
|
|
|
|
-- adding ORDER BY and LIMIT should turn the plan into an optimized
|
|
-- merge append plan
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test WHERE time > now_s() - interval '2 months'
|
|
ORDER BY time LIMIT 3;
|
|
psql:include/append.sql:64: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:64: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:64: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:64: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
-------------------------------------------------------------------------------------------------
|
|
Limit
|
|
-> Merge Append
|
|
Sort Key: append_test."time"
|
|
-> Index Scan Backward using append_test_time_idx on append_test
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan Backward using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan Backward using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan Backward using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
(11 rows)
|
|
|
|
-- the expected output should be the same as the non-optimized query
|
|
SELECT * FROM append_test WHERE time > now_s() - interval '2 months'
|
|
ORDER BY time LIMIT 3;
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:68: NOTICE: Stable function now_s() called!
|
|
time | temp | colorid
|
|
------------------------------+------+---------
|
|
Tue Aug 22 09:18:22 2017 PDT | 34.1 | 3
|
|
(1 row)
|
|
|
|
-- no optimized plan for queries with restrictions that can be
|
|
-- constified at planning time. Regular planning-time constraint
|
|
-- exclusion should occur.
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test WHERE time > now_i() - interval '2 months'
|
|
ORDER BY time;
|
|
psql:include/append.sql:75: NOTICE: Immutable function now_i() called!
|
|
QUERY PLAN
|
|
------------------------------------------------------------------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: append_test."time"
|
|
-> Append
|
|
-> Seq Scan on append_test
|
|
Filter: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
-> Bitmap Heap Scan on _hyper_1_1_chunk
|
|
Recheck Cond: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
-> Bitmap Index Scan on _hyper_1_1_chunk_append_test_time_idx
|
|
Index Cond: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
-> Bitmap Heap Scan on _hyper_1_2_chunk
|
|
Recheck Cond: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
-> Bitmap Index Scan on _hyper_1_2_chunk_append_test_time_idx
|
|
Index Cond: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
-> Bitmap Heap Scan on _hyper_1_3_chunk
|
|
Recheck Cond: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
-> Bitmap Index Scan on _hyper_1_3_chunk_append_test_time_idx
|
|
Index Cond: ("time" > ('Tue Aug 22 10:00:00 2017 PDT'::timestamp with time zone - '@ 2 mons'::interval))
|
|
(17 rows)
|
|
|
|
-- currently, we cannot distinguish between stable and volatile
|
|
-- functions as far as applying our modified plan. However, volatile
|
|
-- function should not be pre-evaluated to constants, so no chunk
|
|
-- exclusion should occur.
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test WHERE time > now_v() - interval '2 months'
|
|
ORDER BY time;
|
|
QUERY PLAN
|
|
-------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: append_test."time"
|
|
-> Append
|
|
-> Seq Scan on append_test
|
|
Filter: ("time" > (now_v() - '@ 2 mons'::interval))
|
|
-> Seq Scan on _hyper_1_1_chunk
|
|
Filter: ("time" > (now_v() - '@ 2 mons'::interval))
|
|
-> Seq Scan on _hyper_1_2_chunk
|
|
Filter: ("time" > (now_v() - '@ 2 mons'::interval))
|
|
-> Seq Scan on _hyper_1_3_chunk
|
|
Filter: ("time" > (now_v() - '@ 2 mons'::interval))
|
|
(11 rows)
|
|
|
|
SELECT * FROM append_test WHERE time > now_v() - interval '2 months'
|
|
ORDER BY time;
|
|
psql:include/append.sql:86: NOTICE: Volatile function now_v() called!
|
|
psql:include/append.sql:86: NOTICE: Volatile function now_v() called!
|
|
psql:include/append.sql:86: NOTICE: Volatile function now_v() called!
|
|
psql:include/append.sql:86: NOTICE: Volatile function now_v() called!
|
|
psql:include/append.sql:86: NOTICE: Volatile function now_v() called!
|
|
time | temp | colorid
|
|
------------------------------+------+---------
|
|
Tue Aug 22 09:18:22 2017 PDT | 34.1 | 3
|
|
(1 row)
|
|
|
|
-- prepared statement output should be the same regardless of
|
|
-- optimizations
|
|
PREPARE query_opt AS
|
|
SELECT * FROM append_test WHERE time > now_s() - interval '2 months'
|
|
ORDER BY time;
|
|
EXECUTE query_opt;
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:94: NOTICE: Stable function now_s() called!
|
|
time | temp | colorid
|
|
------------------------------+------+---------
|
|
Tue Aug 22 09:18:22 2017 PDT | 34.1 | 3
|
|
(1 row)
|
|
|
|
EXPLAIN (costs off)
|
|
EXECUTE query_opt;
|
|
QUERY PLAN
|
|
-------------------------------------------------------------------------------------------
|
|
Merge Append
|
|
Sort Key: append_test."time"
|
|
-> Index Scan Backward using append_test_time_idx on append_test
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan Backward using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan Backward using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
-> Index Scan Backward using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 2 mons'::interval))
|
|
(10 rows)
|
|
|
|
-- aggregates should produce same output
|
|
SELECT date_trunc('year', time) t, avg(temp) FROM append_test
|
|
WHERE time > now_s() - interval '4 months'
|
|
GROUP BY t
|
|
ORDER BY t DESC;
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:103: NOTICE: Stable function now_s() called!
|
|
t | avg
|
|
------------------------------+------
|
|
Sun Jan 01 00:00:00 2017 PST | 28.5
|
|
(1 row)
|
|
|
|
-- aggregates should use optimized plan when possible
|
|
EXPLAIN (costs off)
|
|
SELECT date_trunc('year', time) t, avg(temp) FROM append_test
|
|
WHERE time > now_s() - interval '4 months'
|
|
GROUP BY t
|
|
ORDER BY t DESC;
|
|
psql:include/append.sql:110: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:110: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:110: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:110: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
----------------------------------------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: (date_trunc('year'::text, append_test."time")) DESC
|
|
-> HashAggregate
|
|
Group Key: date_trunc('year'::text, append_test."time")
|
|
-> Result
|
|
-> Append
|
|
-> Seq Scan on append_test
|
|
Filter: ("time" > (now_s() - '@ 4 mons'::interval))
|
|
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 4 mons'::interval))
|
|
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 4 mons'::interval))
|
|
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 4 mons'::interval))
|
|
(14 rows)
|
|
|
|
-- querying outside the time range should return nothing. This tests
|
|
-- that ConstraintAwareAppend can handle the case when an Append node
|
|
-- is turned into a Result node due to no children
|
|
EXPLAIN (costs off)
|
|
SELECT date_trunc('year', time) t, avg(temp)
|
|
FROM append_test
|
|
WHERE time < '2016-03-22'
|
|
AND date_part('dow', time) between 1 and 5
|
|
GROUP BY t
|
|
ORDER BY t DESC;
|
|
QUERY PLAN
|
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
|
GroupAggregate
|
|
Group Key: (date_trunc('year'::text, append_test."time"))
|
|
-> Sort
|
|
Sort Key: (date_trunc('year'::text, append_test."time")) DESC
|
|
-> Result
|
|
-> Append
|
|
-> Seq Scan on append_test
|
|
Filter: (("time" < 'Tue Mar 22 00:00:00 2016 PDT'::timestamp with time zone) AND (date_part('dow'::text, "time") >= '1'::double precision) AND (date_part('dow'::text, "time") <= '5'::double precision))
|
|
(8 rows)
|
|
|
|
-- a parameterized query can safely constify params, so won't be
|
|
-- optimized by constraint-aware append since regular constraint
|
|
-- exclusion works just fine
|
|
PREPARE query_param AS
|
|
SELECT * FROM append_test WHERE time > $1 ORDER BY time;
|
|
EXPLAIN (costs off)
|
|
EXECUTE query_param(now_s() - interval '2 months');
|
|
psql:include/append.sql:130: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
-----------------------------------------------------------------------------------------------------
|
|
Sort
|
|
Sort Key: append_test."time"
|
|
-> Append
|
|
-> Seq Scan on append_test
|
|
Filter: ("time" > 'Thu Jun 22 10:00:00 2017 PDT'::timestamp with time zone)
|
|
-> Bitmap Heap Scan on _hyper_1_3_chunk
|
|
Recheck Cond: ("time" > 'Thu Jun 22 10:00:00 2017 PDT'::timestamp with time zone)
|
|
-> Bitmap Index Scan on _hyper_1_3_chunk_append_test_time_idx
|
|
Index Cond: ("time" > 'Thu Jun 22 10:00:00 2017 PDT'::timestamp with time zone)
|
|
(9 rows)
|
|
|
|
--test with cte
|
|
EXPLAIN (costs off)
|
|
WITH data AS (
|
|
SELECT time_bucket(INTERVAL '30 day', TIME) AS btime, AVG(temp) AS VALUE
|
|
FROM append_test
|
|
WHERE
|
|
TIME > now_s() - INTERVAL '400 day'
|
|
AND colorid > 0
|
|
GROUP BY btime
|
|
),
|
|
period AS (
|
|
SELECT time_bucket(INTERVAL '30 day', TIME) AS btime
|
|
FROM GENERATE_SERIES('2017-03-22T01:01:01', '2017-08-23T01:01:01', INTERVAL '30 day') TIME
|
|
)
|
|
SELECT period.btime, VALUE
|
|
FROM period
|
|
LEFT JOIN DATA USING (btime)
|
|
ORDER BY period.btime;
|
|
psql:include/append.sql:149: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:149: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:149: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:149: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
-------------------------------------------------------------------------------------------------------
|
|
Merge Left Join
|
|
Merge Cond: (period.btime = data.btime)
|
|
CTE data
|
|
-> HashAggregate
|
|
Group Key: time_bucket('@ 30 days'::interval, append_test."time")
|
|
-> Result
|
|
-> Append
|
|
-> Seq Scan on append_test
|
|
Filter: ((colorid > 0) AND ("time" > (now_s() - '@ 400 days'::interval)))
|
|
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 400 days'::interval))
|
|
Filter: (colorid > 0)
|
|
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 400 days'::interval))
|
|
Filter: (colorid > 0)
|
|
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk
|
|
Index Cond: ("time" > (now_s() - '@ 400 days'::interval))
|
|
Filter: (colorid > 0)
|
|
CTE period
|
|
-> Function Scan on generate_series "time"
|
|
-> Sort
|
|
Sort Key: period.btime
|
|
-> CTE Scan on period
|
|
-> Sort
|
|
Sort Key: data.btime
|
|
-> CTE Scan on data
|
|
(26 rows)
|
|
|
|
WITH data AS (
|
|
SELECT time_bucket(INTERVAL '30 day', TIME) AS btime, AVG(temp) AS VALUE
|
|
FROM append_test
|
|
WHERE
|
|
TIME > now_s() - INTERVAL '400 day'
|
|
AND colorid > 0
|
|
GROUP BY btime
|
|
),
|
|
period AS (
|
|
SELECT time_bucket(INTERVAL '30 day', TIME) AS btime
|
|
FROM GENERATE_SERIES('2017-03-22T01:01:01', '2017-08-23T01:01:01', INTERVAL '30 day') TIME
|
|
)
|
|
SELECT period.btime, VALUE
|
|
FROM period
|
|
LEFT JOIN DATA USING (btime)
|
|
ORDER BY period.btime;
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:166: NOTICE: Stable function now_s() called!
|
|
btime | value
|
|
------------------------------+-------
|
|
Wed Mar 01 16:00:00 2017 PST | 22.5
|
|
Fri Mar 31 17:00:00 2017 PDT |
|
|
Sun Apr 30 17:00:00 2017 PDT | 25.7
|
|
Tue May 30 17:00:00 2017 PDT |
|
|
Thu Jun 29 17:00:00 2017 PDT |
|
|
Sat Jul 29 17:00:00 2017 PDT | 34.1
|
|
(6 rows)
|
|
|
|
-- Create another hypertable to join with
|
|
CREATE TABLE join_test(time timestamptz, temp float, colorid integer);
|
|
SELECT create_hypertable('join_test', 'time', chunk_time_interval => 2628000000000);
|
|
psql:include/append.sql:172: NOTICE: adding not-null constraint to column "time"
|
|
create_hypertable
|
|
-------------------
|
|
|
|
(1 row)
|
|
|
|
INSERT INTO join_test VALUES ('2017-01-22T09:18:22', 15.2, 1),
|
|
('2017-02-22T09:18:22', 24.5, 2),
|
|
('2017-08-22T09:18:22', 23.1, 3);
|
|
-- force nested loop join with no materialization. This tests that the
|
|
-- inner ConstraintAwareScan supports resetting its scan for every
|
|
-- iteration of the outer relation loop
|
|
set enable_hashjoin = 'off';
|
|
set enable_mergejoin = 'off';
|
|
set enable_material = 'off';
|
|
EXPLAIN (costs off)
|
|
SELECT * FROM append_test a INNER JOIN join_test j ON (a.colorid = j.colorid)
|
|
WHERE a.time > now_s() - interval '3 hours' AND j.time > now_s() - interval '3 hours';
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:187: NOTICE: Stable function now_s() called!
|
|
QUERY PLAN
|
|
--------------------------------------------------------------------------------------------
|
|
Nested Loop
|
|
Join Filter: (a.colorid = j.colorid)
|
|
-> Append
|
|
-> Seq Scan on append_test a
|
|
Filter: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Index Scan using _hyper_1_1_chunk_append_test_time_idx on _hyper_1_1_chunk a_1
|
|
Index Cond: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Index Scan using _hyper_1_2_chunk_append_test_time_idx on _hyper_1_2_chunk a_2
|
|
Index Cond: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Index Scan using _hyper_1_3_chunk_append_test_time_idx on _hyper_1_3_chunk a_3
|
|
Index Cond: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Append
|
|
-> Seq Scan on join_test j
|
|
Filter: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Index Scan using _hyper_2_4_chunk_join_test_time_idx on _hyper_2_4_chunk j_1
|
|
Index Cond: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Index Scan using _hyper_2_5_chunk_join_test_time_idx on _hyper_2_5_chunk j_2
|
|
Index Cond: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
-> Index Scan using _hyper_2_6_chunk_join_test_time_idx on _hyper_2_6_chunk j_3
|
|
Index Cond: ("time" > (now_s() - '@ 3 hours'::interval))
|
|
(20 rows)
|
|
|
|
-- result should be the same as when optimizations are turned off
|
|
SELECT * FROM append_test a INNER JOIN join_test j ON (a.colorid = j.colorid)
|
|
WHERE a.time > now_s() - interval '3 hours' AND j.time > now_s() - interval '3 hours';
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
psql:include/append.sql:191: NOTICE: Stable function now_s() called!
|
|
time | temp | colorid | time | temp | colorid
|
|
------------------------------+------+---------+------------------------------+------+---------
|
|
Tue Aug 22 09:18:22 2017 PDT | 34.1 | 3 | Tue Aug 22 09:18:22 2017 PDT | 23.1 | 3
|
|
(1 row)
|
|
|