From 8b744f2cc49bf6559cf3a2584acd79fcbff30a77 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Mon, 18 Jan 2021 12:11:22 +0100 Subject: [PATCH] Make plan_hypertable_inline test output PG version specific PG13 changes the relation name aliasing in EXPLAIN output making explain output different from previous PG versions. --- ...line.out => plan_hypertable_inline-12.out} | 0 test/expected/plan_hypertable_inline-13.out | 149 ++++++++++++++++++ test/sql/.gitignore | 1 + test/sql/CMakeLists.txt | 48 +++--- ...line.sql => plan_hypertable_inline.sql.in} | 0 5 files changed, 175 insertions(+), 23 deletions(-) rename test/expected/{plan_hypertable_inline.out => plan_hypertable_inline-12.out} (100%) create mode 100644 test/expected/plan_hypertable_inline-13.out rename test/sql/{plan_hypertable_inline.sql => plan_hypertable_inline.sql.in} (100%) diff --git a/test/expected/plan_hypertable_inline.out b/test/expected/plan_hypertable_inline-12.out similarity index 100% rename from test/expected/plan_hypertable_inline.out rename to test/expected/plan_hypertable_inline-12.out diff --git a/test/expected/plan_hypertable_inline-13.out b/test/expected/plan_hypertable_inline-13.out new file mode 100644 index 000000000..3755dc87d --- /dev/null +++ b/test/expected/plan_hypertable_inline-13.out @@ -0,0 +1,149 @@ +-- This file and its contents are licensed under the Apache License 2.0. +-- Please see the included NOTICE for copyright information and +-- LICENSE-APACHE for a copy of the license. +-- test hypertable classification when query is in an inlineable function +\set PREFIX 'EXPLAIN (costs off)' +CREATE TABLE test (a int, b bigint NOT NULL); +SELECT create_hypertable('public.test', 'b', chunk_time_interval=>10); + create_hypertable +------------------- + (1,public,test,t) +(1 row) + +INSERT INTO test SELECT i, i FROM generate_series(1, 20) i; +CREATE OR REPLACE FUNCTION test_f(_ts bigint) +RETURNS SETOF test LANGUAGE SQL STABLE +as $f$ + SELECT DISTINCT ON (a) * FROM test WHERE b >= _ts AND b <= _ts + 2 +$f$; +-- plans must be the same in both cases +-- specifically, the first plan should not contain the parent hypertable +-- as that is a sign the pruning was not done successfully +:PREFIX SELECT * FROM test_f(5); + QUERY PLAN +------------------------------------------------------------------------------ + Unique + -> Sort + Sort Key: _hyper_1_1_chunk.a + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk + Index Cond: ((b >= '5'::bigint) AND (b <= '7'::bigint)) +(5 rows) + +:PREFIX SELECT DISTINCT ON (a) * FROM test WHERE b >= 5 AND b <= 5 + 2; + QUERY PLAN +------------------------------------------------------------------------------ + Unique + -> Sort + Sort Key: _hyper_1_1_chunk.a + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk + Index Cond: ((b >= 5) AND (b <= 7)) +(5 rows) + +-- test with FOR UPDATE +CREATE OR REPLACE FUNCTION test_f(_ts bigint) +RETURNS SETOF test LANGUAGE SQL STABLE +as $f$ + SELECT * FROM test WHERE b >= _ts AND b <= _ts + 2 FOR UPDATE +$f$; +-- pruning should not be done by TimescaleDb in this case +-- specifically, the parent hypertable must exist in the output plan +:PREFIX SELECT * FROM test_f(5); + QUERY PLAN +------------------------------------------------------------------------------------------- + Subquery Scan on test_f + -> LockRows + -> Append + -> Seq Scan on test test_1 + Filter: ((b >= '5'::bigint) AND (b <= '7'::bigint)) + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk test_2 + Index Cond: ((b >= '5'::bigint) AND (b <= '7'::bigint)) +(7 rows) + +:PREFIX SELECT * FROM test WHERE b >= 5 AND b <= 5 + 2 FOR UPDATE; + QUERY PLAN +------------------------------------------------------------------------------------- + LockRows + -> Append + -> Seq Scan on test test_1 + Filter: ((b >= 5) AND (b <= 7)) + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk test_2 + Index Cond: ((b >= 5) AND (b <= 7)) +(6 rows) + +-- test with CTE +-- these cases are just to make sure we're everything is alright with +-- the way we identify hypertables to prune chunks - we abuse ctename +-- for this purpose. So double-check if we're not breaking plans +-- with CTEs here. +CREATE OR REPLACE FUNCTION test_f(_ts bigint) +RETURNS SETOF test LANGUAGE SQL STABLE +as $f$ + WITH ct AS MATERIALIZED ( + SELECT DISTINCT ON (a) * FROM test WHERE b >= _ts AND b <= _ts + 2 + ) + SELECT * FROM ct +$f$; +:PREFIX SELECT * FROM test_f(5); + QUERY PLAN +-------------------------------------------------------------------------------------- + CTE Scan on ct + CTE ct + -> Unique + -> Sort + Sort Key: _hyper_1_1_chunk.a + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk + Index Cond: ((b >= '5'::bigint) AND (b <= '7'::bigint)) +(7 rows) + +:PREFIX +WITH ct AS MATERIALIZED ( + SELECT DISTINCT ON (a) * FROM test WHERE b >= 5 AND b <= 5 + 2 +) +SELECT * FROM ct; + QUERY PLAN +-------------------------------------------------------------------------------------- + CTE Scan on ct + CTE ct + -> Unique + -> Sort + Sort Key: _hyper_1_1_chunk.a + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk + Index Cond: ((b >= 5) AND (b <= 7)) +(7 rows) + +-- CTE within CTE +:PREFIX +WITH ct AS MATERIALIZED ( + SELECT * FROM test_f(5) +) +SELECT * FROM ct; + QUERY PLAN +---------------------------------------------------------------------------------------------- + CTE Scan on ct + CTE ct + -> CTE Scan on ct ct_1 + CTE ct + -> Unique + -> Sort + Sort Key: _hyper_1_1_chunk.a + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk + Index Cond: ((b >= '5'::bigint) AND (b <= '7'::bigint)) +(9 rows) + +-- CTE within NO MATERIALIZED CTE +:PREFIX +WITH ct AS NOT MATERIALIZED ( + SELECT * FROM test_f(5) +) +SELECT * FROM ct; + QUERY PLAN +-------------------------------------------------------------------------------------- + CTE Scan on ct + CTE ct + -> Unique + -> Sort + Sort Key: _hyper_1_1_chunk.a + -> Index Scan using _hyper_1_1_chunk_test_b_idx on _hyper_1_1_chunk + Index Cond: ((b >= '5'::bigint) AND (b <= '7'::bigint)) +(7 rows) + diff --git a/test/sql/.gitignore b/test/sql/.gitignore index 0b39dac6b..e3d878a26 100644 --- a/test/sql/.gitignore +++ b/test/sql/.gitignore @@ -18,6 +18,7 @@ /plan_hashagg_optimized-*.sql /plan_hashagg-*.sql /plan_hypertable_cache-*.sql +/plan_hypertable_inline-*.sql /plan_ordered_append-*.sql /query-*.sql /sort_optimization-*.sql diff --git a/test/sql/CMakeLists.txt b/test/sql/CMakeLists.txt index a83a51a48..f2da265ed 100644 --- a/test/sql/CMakeLists.txt +++ b/test/sql/CMakeLists.txt @@ -47,6 +47,28 @@ set(TEST_FILES license.sql ) +set(TEST_TEMPLATES + agg_bookends.sql.in + append.sql.in + insert.sql.in + chunk_adaptive.sql.in + constraint.sql.in + copy.sql.in + ddl.sql.in + delete.sql.in + parallel.sql.in + partition.sql.in + partitionwise.sql.in + plan_expand_hypertable.sql.in + #hashagg is different in 9.6 and 10 because of hashagg parallelism + plan_hashagg.sql.in + query.sql.in + # Query tests differ in how PostgreSQL deals with projections on partitioned tables. + sort_optimization.sql.in + sql_query.sql.in + update.sql.in +) + # tests that fail or are unreliable when run in parallel # bgw tests need to run first otherwise they are flaky set(SOLO_TESTS @@ -93,33 +115,13 @@ if ((${PG_VERSION_MAJOR} GREATER_EQUAL "12")) list(APPEND TEST_FILES generated_columns.sql misc.sql - plan_hypertable_inline.sql tableam.sql ) + list(APPEND TEST_TEMPLATES + plan_hypertable_inline.sql.in + ) endif() -set(TEST_TEMPLATES - agg_bookends.sql.in - append.sql.in - insert.sql.in - chunk_adaptive.sql.in - constraint.sql.in - copy.sql.in - ddl.sql.in - delete.sql.in - parallel.sql.in - partition.sql.in - partitionwise.sql.in - plan_expand_hypertable.sql.in - #hashagg is different in 9.6 and 10 because of hashagg parallelism - plan_hashagg.sql.in - query.sql.in - # Query tests differ in how PostgreSQL deals with projections on partitioned tables. - sort_optimization.sql.in - sql_query.sql.in - update.sql.in -) - if (CMAKE_BUILD_TYPE MATCHES Debug) list(APPEND TEST_TEMPLATES multi_transaction_index.sql.in diff --git a/test/sql/plan_hypertable_inline.sql b/test/sql/plan_hypertable_inline.sql.in similarity index 100% rename from test/sql/plan_hypertable_inline.sql rename to test/sql/plan_hypertable_inline.sql.in