Skip to content

Commit

Permalink
Mark hypertable parent as dummy rel for UPDATE
Browse files Browse the repository at this point in the history
When postgres expands an inheritance tree it also adds the
parent hypertable as child relation. Since for a hypertable
the parent will never have any data we can mark this
relation as dummy relation so it gets ignored in later
steps. This is only relevant for code paths that use the
postgres inheritance code as we don't include the hypertable
as child when expanding the hypertable ourself.

This is similar to 3c40f92 which did the same adjustment for DELETE.

This patch also moves the marking into get_relation_info_hook so
it happens a bit earlier and prevents some additional cycles.
  • Loading branch information
svenklemm committed Apr 20, 2022
1 parent 6d40c30 commit fca865c
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 557 deletions.
2 changes: 1 addition & 1 deletion src/nodes/hypertable_modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ hypertable_modify_begin(CustomScanState *node, EState *estate, int eflags)
* we need to set the hypertable as the rootRelation otherwise
* statement trigger defined only on the hypertable will not fire.
*/
if (mt->operation == CMD_DELETE)
if (mt->operation == CMD_DELETE || mt->operation == CMD_UPDATE)
mt->rootRelation = mt->nominalRelation;

ps = ExecInitNode(&mt->plan, estate, eflags);
Expand Down
27 changes: 14 additions & 13 deletions src/planner.c
Original file line number Diff line number Diff line change
Expand Up @@ -1042,19 +1042,6 @@ timescaledb_set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti, Rang
switch (reltype)
{
case TS_REL_HYPERTABLE_CHILD:
/* When postgres expands an inheritance tree it also adds the
* parent hypertable as child relation. Since for a hypertable the
* parent will never have any data we can mark this relation as
* dummy relation so it gets ignored in later steps. This is only
* relevant for code paths that use the postgres inheritance code
* as we don't include the hypertable as child when expanding the
* hypertable ourself.
* We do exclude distributed hypertables for now to not alter
* the trigger behaviour on access nodes, which would otherwise
* no longer fire.
*/
if (root->parse->commandType == CMD_DELETE && !hypertable_is_distributed(ht))
mark_dummy_rel(rel);
break;
case TS_REL_CHUNK:
case TS_REL_CHUNK_CHILD:
Expand Down Expand Up @@ -1163,6 +1150,20 @@ timescaledb_get_relation_info_hook(PlannerInfo *root, Oid relation_objectid, boo
break;
}
case TS_REL_HYPERTABLE_CHILD:
/* When postgres expands an inheritance tree it also adds the
* parent hypertable as child relation. Since for a hypertable the
* parent will never have any data we can mark this relation as
* dummy relation so it gets ignored in later steps. This is only
* relevant for code paths that use the postgres inheritance code
* as we don't include the hypertable as child when expanding the
* hypertable ourself.
* We do exclude distributed hypertables for now to not alter
* the trigger behaviour on access nodes, which would otherwise
* no longer fire.
*/
if (IS_UPDL_CMD(root->parse) && !hypertable_is_distributed(ht))
mark_dummy_rel(rel);
break;
case TS_REL_OTHER:
break;
}
Expand Down
7 changes: 1 addition & 6 deletions test/expected/cursor.out → test/expected/cursor-12.out
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@ FETCH NEXT FROM c1;
Sat Jan 01 00:00:00 2000 PST | 1 | 0.5
(1 row)

-- this will produce an error because PostgreSQL checks
-- this will produce an error on PG < 14 because PostgreSQL checks
-- for the existence of a scan node with the relation id for every relation
-- used in the update plan in the plan of the cursor.
--
-- constraint exclusion will make this easier to hit
-- because constraint exclusion only works for SELECT
-- and UPDATE statements will still have parent tables in
-- the generated plans
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
ERROR: cursor "c1" is not a simply updatable scan of table "cursor_test"
COMMIT;
Expand Down
53 changes: 53 additions & 0 deletions test/expected/cursor-13.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
-- 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.
CREATE TABLE cursor_test(time timestamptz, device_id int, temp float);
SELECT create_hypertable('cursor_test','time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
--------------------------
(1,public,cursor_test,t)
(1 row)

INSERT INTO cursor_test SELECT '2000-01-01',1,0.5;
INSERT INTO cursor_test SELECT '2001-01-01',1,0.5;
INSERT INTO cursor_test SELECT '2002-01-01',1,0.5;
\set ON_ERROR_STOP 0
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test;
FETCH NEXT FROM c1;
time | device_id | temp
------------------------------+-----------+------
Sat Jan 01 00:00:00 2000 PST | 1 | 0.5
(1 row)

-- this will produce an error on PG < 14 because PostgreSQL checks
-- for the existence of a scan node with the relation id for every relation
-- used in the update plan in the plan of the cursor.
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
ERROR: cursor "c1" is not a simply updatable scan of table "cursor_test"
COMMIT;
-- test cursor with no chunks left after runtime exclusion
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test WHERE time > now();
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
ERROR: cursor "c1" is not a simply updatable scan of table "cursor_test"
COMMIT;
-- test cursor with no chunks left after planning exclusion
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test WHERE time > '2010-01-01';
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
ERROR: cursor "c1" is not a simply updatable scan of table "cursor_test"
COMMIT;
\set ON_ERROR_STOP 1
SET timescaledb.enable_constraint_exclusion TO off;
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test;
FETCH NEXT FROM c1;
time | device_id | temp
------------------------------+-----------+------
Sat Jan 01 00:00:00 2000 PST | 1 | 0.5
(1 row)

UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
COMMIT;
52 changes: 52 additions & 0 deletions test/expected/cursor-14.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- 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.
CREATE TABLE cursor_test(time timestamptz, device_id int, temp float);
SELECT create_hypertable('cursor_test','time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
--------------------------
(1,public,cursor_test,t)
(1 row)

INSERT INTO cursor_test SELECT '2000-01-01',1,0.5;
INSERT INTO cursor_test SELECT '2001-01-01',1,0.5;
INSERT INTO cursor_test SELECT '2002-01-01',1,0.5;
\set ON_ERROR_STOP 0
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test;
FETCH NEXT FROM c1;
time | device_id | temp
------------------------------+-----------+------
Sat Jan 01 00:00:00 2000 PST | 1 | 0.5
(1 row)

-- this will produce an error on PG < 14 because PostgreSQL checks
-- for the existence of a scan node with the relation id for every relation
-- used in the update plan in the plan of the cursor.
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
COMMIT;
-- test cursor with no chunks left after runtime exclusion
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test WHERE time > now();
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
ERROR: cursor "c1" is not a simply updatable scan of table "_hyper_1_1_chunk"
COMMIT;
-- test cursor with no chunks left after planning exclusion
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test WHERE time > '2010-01-01';
UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
ERROR: cursor "c1" is not a simply updatable scan of table "_hyper_1_1_chunk"
COMMIT;
\set ON_ERROR_STOP 1
SET timescaledb.enable_constraint_exclusion TO off;
BEGIN;
DECLARE c1 SCROLL CURSOR FOR SELECT * FROM cursor_test;
FETCH NEXT FROM c1;
time | device_id | temp
------------------------------+-----------+------
Sat Jan 01 00:00:00 2000 PST | 1 | 0.7
(1 row)

UPDATE cursor_test SET temp = 0.7 WHERE CURRENT OF c1;
COMMIT;
28 changes: 10 additions & 18 deletions test/expected/rowsecurity-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -2269,16 +2269,13 @@ EXPLAIN (COSTS OFF) UPDATE bv1 SET b = 'yyy' WHERE a = 4 AND f_leak(b);
-----------------------------------------------------------------------------------------
Custom Scan (HypertableModify)
-> Update on b1
Update on b1 b1_1
Update on _hyper_8_41_chunk b1_2
Update on _hyper_8_41_chunk b1_1
-> Custom Scan (ChunkAppend) on b1
Chunks excluded during startup: 0
-> Seq Scan on b1 b1_1
Filter: ((a > 0) AND (a = 4) AND ((a % 2) = 0) AND f_leak(b))
-> Index Scan using _hyper_8_41_chunk_b1_a_idx on _hyper_8_41_chunk b1_2
-> Index Scan using _hyper_8_41_chunk_b1_a_idx on _hyper_8_41_chunk b1_1
Index Cond: ((a > 0) AND (a = 4))
Filter: (((a % 2) = 0) AND f_leak(b))
(11 rows)
(8 rows)

UPDATE bv1 SET b = 'yyy' WHERE a = 4 AND f_leak(b);
NOTICE: f_leak => a87ff679a2f3e71d9181a67b7542122c
Expand Down Expand Up @@ -4353,20 +4350,15 @@ SELECT * FROM current_check;

-- Plan should be a subquery TID scan
EXPLAIN (COSTS OFF) UPDATE current_check SET payload = payload WHERE CURRENT OF current_check_cursor;
QUERY PLAN
-------------------------------------------------------------------------
QUERY PLAN
-------------------------------------------------------------------
Custom Scan (HypertableModify)
-> Update on current_check
Update on current_check current_check_1
Update on _hyper_21_104_chunk current_check_2
-> Append
-> Tid Scan on current_check current_check_1
TID Cond: CURRENT OF current_check_cursor
Filter: ((currentid = 4) AND ((currentid % 2) = 0))
-> Tid Scan on _hyper_21_104_chunk current_check_2
TID Cond: CURRENT OF current_check_cursor
Filter: ((currentid = 4) AND ((currentid % 2) = 0))
(11 rows)
Update on _hyper_21_104_chunk current_check_1
-> Tid Scan on _hyper_21_104_chunk current_check_1
TID Cond: CURRENT OF current_check_cursor
Filter: ((currentid = 4) AND ((currentid % 2) = 0))
(6 rows)

-- Similarly can only delete row 4
FETCH ABSOLUTE 1 FROM current_check_cursor;
Expand Down
26 changes: 9 additions & 17 deletions test/expected/update-12.out
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ WHERE series_1 IN (SELECT series_1 FROM "one_Partition" WHERE series_1 > series_
Update on _hyper_1_2_chunk
Update on _hyper_1_3_chunk
-> Hash Join
Hash Cond: ("one_Partition_1".series_1 = "one_Partition".series_1)
Hash Cond: (_hyper_1_1_chunk_1.series_1 = "one_Partition".series_1)
-> HashAggregate
Group Key: "one_Partition_1".series_1
Group Key: _hyper_1_1_chunk_1.series_1
-> Append
-> Seq Scan on "one_Partition" "one_Partition_1"
Filter: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_1_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_1_chunk _hyper_1_1_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_2_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_2_chunk _hyper_1_2_chunk_1
Expand All @@ -104,51 +102,45 @@ WHERE series_1 IN (SELECT series_1 FROM "one_Partition" WHERE series_1 > series_
-> Hash
-> Seq Scan on "one_Partition"
-> Hash Join
Hash Cond: (_hyper_1_1_chunk.series_1 = "one_Partition_1".series_1)
Hash Cond: (_hyper_1_1_chunk.series_1 = _hyper_1_1_chunk_1.series_1)
-> Seq Scan on _hyper_1_1_chunk
-> Hash
-> HashAggregate
Group Key: "one_Partition_1".series_1
Group Key: _hyper_1_1_chunk_1.series_1
-> Append
-> Seq Scan on "one_Partition" "one_Partition_1"
Filter: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_1_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_1_chunk _hyper_1_1_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_2_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_2_chunk _hyper_1_2_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_3_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_3_chunk _hyper_1_3_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Hash Join
Hash Cond: (_hyper_1_2_chunk.series_1 = "one_Partition_1".series_1)
Hash Cond: (_hyper_1_2_chunk.series_1 = _hyper_1_1_chunk_1.series_1)
-> Seq Scan on _hyper_1_2_chunk
-> Hash
-> HashAggregate
Group Key: "one_Partition_1".series_1
Group Key: _hyper_1_1_chunk_1.series_1
-> Append
-> Seq Scan on "one_Partition" "one_Partition_1"
Filter: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_1_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_1_chunk _hyper_1_1_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_2_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_2_chunk _hyper_1_2_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_3_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_3_chunk _hyper_1_3_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Hash Join
Hash Cond: (_hyper_1_3_chunk.series_1 = "one_Partition_1".series_1)
Hash Cond: (_hyper_1_3_chunk.series_1 = _hyper_1_1_chunk_1.series_1)
-> Seq Scan on _hyper_1_3_chunk
-> Hash
-> HashAggregate
Group Key: "one_Partition_1".series_1
Group Key: _hyper_1_1_chunk_1.series_1
-> Append
-> Seq Scan on "one_Partition" "one_Partition_1"
Filter: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_1_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_1_chunk _hyper_1_1_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_2_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_2_chunk _hyper_1_2_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
-> Index Scan using "_hyper_1_3_chunk_one_Partition_timeCustom_series_1_idx" on _hyper_1_3_chunk _hyper_1_3_chunk_1
Index Cond: (series_1 > (series_val())::double precision)
(65 rows)
(57 rows)

SELECT * FROM "one_Partition" ORDER BY "timeCustom", device_id, series_0, series_1, series_2;
timeCustom | device_id | series_0 | series_1 | series_2 | series_bool
Expand Down
Loading

0 comments on commit fca865c

Please sign in to comment.