From e302aa2ae97bbd682b1d5d1324e57a77131401d0 Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Fri, 2 Jun 2023 09:26:39 +0200 Subject: [PATCH] Fix handling of Result nodes below Sort nodes in ConstraintAwareAppend With PG 15 Result nodes can appear between Sort nodes and DecompressChunk when postgres tries to adjust the targetlist. --- .unreleased/bugfix_5742 | 2 ++ .../constraint_aware_append.c | 5 ++-- .../expected/constraint_aware_append.out | 28 +++++++++++++++++++ tsl/test/shared/sql/CMakeLists.txt | 1 + .../shared/sql/constraint_aware_append.sql | 20 +++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 .unreleased/bugfix_5742 create mode 100644 tsl/test/shared/expected/constraint_aware_append.out create mode 100644 tsl/test/shared/sql/constraint_aware_append.sql diff --git a/.unreleased/bugfix_5742 b/.unreleased/bugfix_5742 new file mode 100644 index 000000000..a14ab8d85 --- /dev/null +++ b/.unreleased/bugfix_5742 @@ -0,0 +1,2 @@ +Fixes: #5742 Fix Result node handling with ConstraintAwareAppend on compressed chunks +Thanks: @xvaara for reporting an issue with Result node handling in ConstraintAwareAppend diff --git a/src/nodes/constraint_aware_append/constraint_aware_append.c b/src/nodes/constraint_aware_append/constraint_aware_append.c index bf96bdbe1..735a810c5 100644 --- a/src/nodes/constraint_aware_append/constraint_aware_append.c +++ b/src/nodes/constraint_aware_append/constraint_aware_append.c @@ -28,6 +28,7 @@ #include "compat/compat.h" #include "constraint_aware_append.h" +#include "debug_assert.h" #include "nodes/chunk_append/transform.h" #include "guc.h" #include "utils.h" @@ -64,8 +65,8 @@ get_plans_for_exclusion(Plan *plan) { case T_Result: case T_Sort: - Assert(plan->lefttree != NULL && plan->righttree == NULL); - return plan->lefttree; + Ensure(plan->lefttree != NULL, "subplan is null"); + return get_plans_for_exclusion(plan->lefttree); default: return plan; diff --git a/tsl/test/shared/expected/constraint_aware_append.out b/tsl/test/shared/expected/constraint_aware_append.out new file mode 100644 index 000000000..d691ebc9c --- /dev/null +++ b/tsl/test/shared/expected/constraint_aware_append.out @@ -0,0 +1,28 @@ +-- This file and its contents are licensed under the Timescale License. +-- Please see the included NOTICE for copyright information and +-- LICENSE-TIMESCALE for a copy of the license. +-- test handling of ConstraintAwareAppend -> Sort -> Result -> Scan +-- issue 5739 +CREATE TABLE ca_append_result(time timestamptz NULL, device text, value float); +SELECT table_name FROM create_hypertable('ca_append_result', 'time'); +NOTICE: adding not-null constraint to column "time" + table_name + ca_append_result +(1 row) + +ALTER TABLE ca_append_result SET (timescaledb.compress); +INSERT INTO ca_append_result SELECT '2000-01-03','d1',0.3; +SELECT count(compress_chunk(ch)) AS compressed FROM show_chunks('ca_append_result') ch; + compressed + 1 +(1 row) + +INSERT INTO ca_append_result SELECT '2000-01-13','d1',0.6; +SET enable_seqscan TO FALSE; +SELECT time_bucket('20d',time) AS day from ca_append_result WHERE time > '1900-01-01'::text::timestamp GROUP BY day; + day + Sun Jan 02 16:00:00 2000 PST +(1 row) + +RESET enable_seqscan; +DROP TABLE ca_append_result; diff --git a/tsl/test/shared/sql/CMakeLists.txt b/tsl/test/shared/sql/CMakeLists.txt index 48acc8447..398e5706f 100644 --- a/tsl/test/shared/sql/CMakeLists.txt +++ b/tsl/test/shared/sql/CMakeLists.txt @@ -2,6 +2,7 @@ set(TEST_FILES_SHARED cagg_compression.sql classify_relation.sql constify_timestamptz_op_interval.sql + constraint_aware_append.sql constraint_exclusion_prepared.sql decompress_placeholdervar.sql dist_chunk.sql diff --git a/tsl/test/shared/sql/constraint_aware_append.sql b/tsl/test/shared/sql/constraint_aware_append.sql new file mode 100644 index 000000000..9651462f1 --- /dev/null +++ b/tsl/test/shared/sql/constraint_aware_append.sql @@ -0,0 +1,20 @@ +-- This file and its contents are licensed under the Timescale License. +-- Please see the included NOTICE for copyright information and +-- LICENSE-TIMESCALE for a copy of the license. + +-- test handling of ConstraintAwareAppend -> Sort -> Result -> Scan +-- issue 5739 +CREATE TABLE ca_append_result(time timestamptz NULL, device text, value float); +SELECT table_name FROM create_hypertable('ca_append_result', 'time'); +ALTER TABLE ca_append_result SET (timescaledb.compress); + +INSERT INTO ca_append_result SELECT '2000-01-03','d1',0.3; +SELECT count(compress_chunk(ch)) AS compressed FROM show_chunks('ca_append_result') ch; +INSERT INTO ca_append_result SELECT '2000-01-13','d1',0.6; + +SET enable_seqscan TO FALSE; +SELECT time_bucket('20d',time) AS day from ca_append_result WHERE time > '1900-01-01'::text::timestamp GROUP BY day; +RESET enable_seqscan; + +DROP TABLE ca_append_result; +