@@ -401,7 +401,7 @@ INSERT INTO test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0, 'data1');
401401-- Create FOR ALL TABLES publication
402402CREATE PUBLICATION test_pub_all_tables FOR ALL TABLES;
403403-- Verify initial state (1 chunk)
404- SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub_all_tables' AND tablename LIKE '%test_hypertable%' OR tablename LIKE '_hyper_%' ORDER BY schemaname, tablename;
404+ SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub_all_tables' AND ( tablename LIKE '%test_hypertable%' OR tablename LIKE '_hyper_%') ORDER BY schemaname, tablename;
405405 schemaname | tablename | attnames | rowfilter
406406-----------------------+-------------------+------------------------------+-----------
407407 _timescaledb_internal | _hyper_6_41_chunk | {time,device_id,value,extra} |
@@ -411,7 +411,7 @@ SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHE
411411INSERT INTO test_hypertable VALUES ('2024-01-02 00:00:00+00', 2, 2.0, 'data2');
412412INSERT INTO test_hypertable VALUES ('2024-01-03 00:00:00+00', 3, 3.0, 'data3');
413413-- Verify state (3 chunks)
414- SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub_all_tables' AND tablename LIKE '%test_hypertable%' OR tablename LIKE '_hyper_%' ORDER BY schemaname, tablename;
414+ SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub_all_tables' AND ( tablename LIKE '%test_hypertable%' OR tablename LIKE '_hyper_%') ORDER BY schemaname, tablename;
415415 schemaname | tablename | attnames | rowfilter
416416-----------------------+-------------------+------------------------------+-----------
417417 _timescaledb_internal | _hyper_6_41_chunk | {time,device_id,value,extra} |
@@ -426,7 +426,7 @@ INSERT INTO test_hypertable VALUES ('2024-01-06 00:00:00+00', 6, 6.0, 'data6');
426426INSERT INTO test_hypertable VALUES ('2024-01-07 00:00:00+00', 7, 7.0, 'data7');
427427INSERT INTO test_hypertable VALUES ('2024-01-08 00:00:00+00', 8, 8.0, 'data8');
428428-- Verify final state (8 chunks)
429- SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub_all_tables' AND tablename LIKE '%test_hypertable%' OR tablename LIKE '_hyper_%' ORDER BY schemaname, tablename;
429+ SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub_all_tables' AND ( tablename LIKE '%test_hypertable%' OR tablename LIKE '_hyper_%') ORDER BY schemaname, tablename;
430430 schemaname | tablename | attnames | rowfilter
431431-----------------------+-------------------+------------------------------+-----------
432432 _timescaledb_internal | _hyper_6_41_chunk | {time,device_id,value,extra} |
@@ -845,6 +845,101 @@ DROP PUBLICATION test_pub_dst CASCADE;
845845DROP TABLE ht_dst_schema.test_hypertable CASCADE;
846846DROP SCHEMA ht_src_schema;
847847DROP SCHEMA ht_dst_schema;
848+ -- Test 6j: DROP TABLES IN SCHEMA on the chunk-home schema while the
849+ -- hypertable's schema stays published. The chunks were covered natively via
850+ -- their own schema (no explicit rows); dropping that schema from the
851+ -- publication would silently unpublish them, so the reconcile must add
852+ -- explicit rows for chunks whose hypertable schema is still covered.
853+ CREATE SCHEMA ht_root_schema;
854+ CREATE SCHEMA ht_chunk_home;
855+ CREATE TABLE ht_root_schema.test_hypertable (time timestamptz NOT NULL, device_id int, value float);
856+ SELECT create_hypertable('ht_root_schema.test_hypertable', 'time',
857+ chunk_time_interval => interval '1 day',
858+ associated_schema_name => 'ht_chunk_home');
859+ create_hypertable
860+ ---------------------------------------
861+ (15,ht_root_schema,test_hypertable,t)
862+
863+ INSERT INTO ht_root_schema.test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0);
864+ INSERT INTO ht_root_schema.test_hypertable VALUES ('2024-01-02 00:00:00+00', 2, 2.0);
865+ CREATE PUBLICATION test_pub_both FOR TABLES IN SCHEMA ht_root_schema, ht_chunk_home;
866+ -- Chunks are covered natively via ht_chunk_home: no explicit rows.
867+ SELECT count(*) AS explicit_rows FROM pg_publication_rel r
868+ JOIN pg_publication p ON p.oid = r.prpubid WHERE p.pubname = 'test_pub_both';
869+ explicit_rows
870+ ---------------
871+ 0
872+
873+ -- Drop the chunk-home schema: the hypertable stays published via
874+ -- ht_root_schema, so the chunks get explicit rows to stay published too.
875+ ALTER PUBLICATION test_pub_both DROP TABLES IN SCHEMA ht_chunk_home;
876+ SELECT count(*) AS explicit_rows FROM pg_publication_rel r
877+ JOIN pg_publication p ON p.oid = r.prpubid WHERE p.pubname = 'test_pub_both';
878+ explicit_rows
879+ ---------------
880+ 2
881+
882+ SELECT schemaname, tablename FROM pg_publication_tables
883+ WHERE pubname = 'test_pub_both' ORDER BY schemaname, tablename;
884+ schemaname | tablename
885+ ----------------+--------------------
886+ ht_chunk_home | _hyper_15_68_chunk
887+ ht_chunk_home | _hyper_15_69_chunk
888+ ht_root_schema | test_hypertable
889+
890+ -- A chunk created afterwards converges to the same state (explicit row).
891+ INSERT INTO ht_root_schema.test_hypertable VALUES ('2024-01-03 00:00:00+00', 3, 3.0);
892+ SELECT count(*) AS explicit_rows FROM pg_publication_rel r
893+ JOIN pg_publication p ON p.oid = r.prpubid WHERE p.pubname = 'test_pub_both';
894+ explicit_rows
895+ ---------------
896+ 3
897+
898+ DROP PUBLICATION test_pub_both CASCADE;
899+ DROP TABLE ht_root_schema.test_hypertable CASCADE;
900+ DROP SCHEMA ht_root_schema;
901+ DROP SCHEMA ht_chunk_home;
902+ -- Test 6k: Documented limitation - with the GUC turned off after chunks were
903+ -- backfilled, DROP TABLES IN SCHEMA skips the reconcile entirely and leaves
904+ -- the explicit chunk rows orphaned (they keep the chunks published). This
905+ -- pins the behavior; see ts_chunk_publication_reconcile_schema_chunks.
906+ CREATE SCHEMA ht_guc_orphan;
907+ CREATE TABLE ht_guc_orphan.test_hypertable (time timestamptz NOT NULL, device_id int, value float);
908+ SELECT create_hypertable('ht_guc_orphan.test_hypertable', 'time', chunk_time_interval => interval '1 day');
909+ create_hypertable
910+ --------------------------------------
911+ (16,ht_guc_orphan,test_hypertable,t)
912+
913+ INSERT INTO ht_guc_orphan.test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0);
914+ INSERT INTO ht_guc_orphan.test_hypertable VALUES ('2024-01-02 00:00:00+00', 2, 2.0);
915+ CREATE PUBLICATION test_pub_orphan FOR TABLES IN SCHEMA ht_guc_orphan;
916+ SELECT count(*) AS explicit_rows FROM pg_publication_rel r
917+ JOIN pg_publication p ON p.oid = r.prpubid WHERE p.pubname = 'test_pub_orphan';
918+ explicit_rows
919+ ---------------
920+ 2
921+
922+ -- With the GUC off, the DROP reconcile is skipped: the explicit chunk rows
923+ -- are orphaned and the chunks stay published.
924+ SET timescaledb.enable_chunk_auto_publication = false;
925+ ALTER PUBLICATION test_pub_orphan DROP TABLES IN SCHEMA ht_guc_orphan;
926+ SELECT count(*) AS explicit_rows FROM pg_publication_rel r
927+ JOIN pg_publication p ON p.oid = r.prpubid WHERE p.pubname = 'test_pub_orphan';
928+ explicit_rows
929+ ---------------
930+ 2
931+
932+ SELECT schemaname, tablename FROM pg_publication_tables
933+ WHERE pubname = 'test_pub_orphan' ORDER BY schemaname, tablename;
934+ schemaname | tablename
935+ -----------------------+--------------------
936+ _timescaledb_internal | _hyper_16_71_chunk
937+ _timescaledb_internal | _hyper_16_72_chunk
938+
939+ SET timescaledb.enable_chunk_auto_publication = true;
940+ DROP PUBLICATION test_pub_orphan CASCADE;
941+ DROP TABLE ht_guc_orphan.test_hypertable CASCADE;
942+ DROP SCHEMA ht_guc_orphan;
848943-- Test 6i: GUC off suppresses reconciliation on the ALTER PUBLICATION and
849944-- schema-change paths (not only on the per-chunk create hook tested in Test 9).
850945SET timescaledb.enable_chunk_auto_publication = false;
@@ -854,7 +949,7 @@ CREATE TABLE ht_guc_schema.test_hypertable (time timestamptz NOT NULL, device_id
854949SELECT create_hypertable('ht_guc_schema.test_hypertable', 'time', chunk_time_interval => interval '1 day');
855950 create_hypertable
856951--------------------------------------
857- (15 ,ht_guc_schema,test_hypertable,t)
952+ (17 ,ht_guc_schema,test_hypertable,t)
858953
859954INSERT INTO ht_guc_schema.test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0);
860955INSERT INTO ht_guc_schema.test_hypertable VALUES ('2024-01-02 00:00:00+00', 2, 2.0);
@@ -870,7 +965,7 @@ CREATE TABLE ht_guc_schema2.test_hypertable2 (time timestamptz NOT NULL, device_
870965SELECT create_hypertable('ht_guc_schema2.test_hypertable2', 'time', chunk_time_interval => interval '1 day');
871966 create_hypertable
872967----------------------------------------
873- (16 ,ht_guc_schema2,test_hypertable2,t)
968+ (18 ,ht_guc_schema2,test_hypertable2,t)
874969
875970INSERT INTO ht_guc_schema2.test_hypertable2 VALUES ('2024-02-01 00:00:00+00', 1, 1.0);
876971INSERT INTO ht_guc_schema2.test_hypertable2 VALUES ('2024-02-02 00:00:00+00', 2, 2.0);
@@ -897,10 +992,10 @@ ALTER PUBLICATION test_pub_guc SET TABLES IN SCHEMA ht_guc_schema, ht_guc_schema
897992SELECT schemaname, tablename FROM pg_publication_tables WHERE pubname = 'test_pub_guc' ORDER BY schemaname, tablename;
898993 schemaname | tablename
899994-----------------------+--------------------
900- _timescaledb_internal | _hyper_15_68_chunk
901- _timescaledb_internal | _hyper_15_69_chunk
902- _timescaledb_internal | _hyper_16_70_chunk
903- _timescaledb_internal | _hyper_16_71_chunk
995+ _timescaledb_internal | _hyper_17_73_chunk
996+ _timescaledb_internal | _hyper_17_74_chunk
997+ _timescaledb_internal | _hyper_18_75_chunk
998+ _timescaledb_internal | _hyper_18_76_chunk
904999 ht_guc_schema2 | test_hypertable
9051000 ht_guc_schema2 | test_hypertable2
9061001
@@ -914,7 +1009,7 @@ CREATE TABLE test_hypertable (time timestamptz NOT NULL, device_id int, value fl
9141009SELECT create_hypertable('test_hypertable', 'time', chunk_time_interval => interval '1 day');
9151010 create_hypertable
9161011-------------------------------
917- (17 ,public,test_hypertable,t)
1012+ (19 ,public,test_hypertable,t)
9181013
9191014-- Insert to create 8 chunks without any publication
9201015INSERT INTO test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0, 'data1');
@@ -939,7 +1034,7 @@ CREATE TABLE test_hypertable (time timestamptz NOT NULL, device_id int, value fl
9391034SELECT create_hypertable('test_hypertable', 'time', chunk_time_interval => interval '1 day');
9401035 create_hypertable
9411036-------------------------------
942- (18 ,public,test_hypertable,t)
1037+ (20 ,public,test_hypertable,t)
9431038
9441039-- Insert to create first chunk
9451040INSERT INTO test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0, 'data1');
@@ -949,7 +1044,7 @@ CREATE PUBLICATION test_pub FOR TABLE test_hypertable;
9491044SELECT schemaname, tablename, attnames, rowfilter FROM pg_publication_tables WHERE pubname = 'test_pub' ORDER BY schemaname, tablename;
9501045 schemaname | tablename | attnames | rowfilter
9511046-----------------------+--------------------+------------------------------+-----------
952- _timescaledb_internal | _hyper_18_80_chunk | {time,device_id,value,extra} |
1047+ _timescaledb_internal | _hyper_20_85_chunk | {time,device_id,value,extra} |
9531048 public | test_hypertable | {time,device_id,value,extra} |
9541049
9551050-- Drop the publication
@@ -972,7 +1067,7 @@ CREATE TABLE test_hypertable (time timestamptz NOT NULL, device_id int, value fl
9721067SELECT create_hypertable('test_hypertable', 'time', chunk_time_interval => interval '1 day');
9731068 create_hypertable
9741069-------------------------------
975- (19 ,public,test_hypertable,t)
1070+ (21 ,public,test_hypertable,t)
9761071
9771072-- Insert to create first chunk
9781073INSERT INTO test_hypertable VALUES ('2024-01-01 00:00:00+00', 1, 1.0, 'data1');
@@ -982,7 +1077,7 @@ CREATE PUBLICATION test_pub_guc FOR TABLE test_hypertable;
9821077SELECT schemaname, tablename FROM pg_publication_tables WHERE pubname = 'test_pub_guc' ORDER BY schemaname, tablename;
9831078 schemaname | tablename
9841079-----------------------+--------------------
985- _timescaledb_internal | _hyper_19_83_chunk
1080+ _timescaledb_internal | _hyper_21_88_chunk
9861081 public | test_hypertable
9871082
9881083-- Test Part 1: GUC enabled - chunks should be added to publication automatically
@@ -992,8 +1087,8 @@ INSERT INTO test_hypertable VALUES ('2024-01-02 00:00:00+00', 2, 2.0, 'data2');
9921087SELECT schemaname, tablename FROM pg_publication_tables WHERE pubname = 'test_pub_guc' ORDER BY schemaname, tablename;
9931088 schemaname | tablename
9941089-----------------------+--------------------
995- _timescaledb_internal | _hyper_19_83_chunk
996- _timescaledb_internal | _hyper_19_84_chunk
1090+ _timescaledb_internal | _hyper_21_88_chunk
1091+ _timescaledb_internal | _hyper_21_89_chunk
9971092 public | test_hypertable
9981093
9991094-- Test Part 2: Disable the GUC and create another chunk
@@ -1004,17 +1099,17 @@ INSERT INTO test_hypertable VALUES ('2024-01-03 00:00:00+00', 3, 3.0, 'data3');
10041099SELECT schemaname, tablename FROM pg_publication_tables WHERE pubname = 'test_pub_guc' ORDER BY schemaname, tablename;
10051100 schemaname | tablename
10061101-----------------------+--------------------
1007- _timescaledb_internal | _hyper_19_83_chunk
1008- _timescaledb_internal | _hyper_19_84_chunk
1102+ _timescaledb_internal | _hyper_21_88_chunk
1103+ _timescaledb_internal | _hyper_21_89_chunk
10091104 public | test_hypertable
10101105
10111106-- Verify that chunk 3 exists but is not in the publication
1012- SELECT chunk_schema, chunk_name FROM timescaledb_information.chunks WHERE hypertable_name = 'test_hypertable';
1107+ SELECT chunk_schema, chunk_name FROM timescaledb_information.chunks WHERE hypertable_name = 'test_hypertable' ORDER BY chunk_name ;
10131108 chunk_schema | chunk_name
10141109-----------------------+--------------------
1015- _timescaledb_internal | _hyper_19_83_chunk
1016- _timescaledb_internal | _hyper_19_84_chunk
1017- _timescaledb_internal | _hyper_19_85_chunk
1110+ _timescaledb_internal | _hyper_21_88_chunk
1111+ _timescaledb_internal | _hyper_21_89_chunk
1112+ _timescaledb_internal | _hyper_21_90_chunk
10181113
10191114-- Test Part 3: Re-enable the GUC and create another chunk
10201115SET timescaledb.enable_chunk_auto_publication = true;
@@ -1024,18 +1119,18 @@ INSERT INTO test_hypertable VALUES ('2024-01-04 00:00:00+00', 4, 4.0, 'data4');
10241119SELECT schemaname, tablename FROM pg_publication_tables WHERE pubname = 'test_pub_guc' ORDER BY schemaname, tablename;
10251120 schemaname | tablename
10261121-----------------------+--------------------
1027- _timescaledb_internal | _hyper_19_83_chunk
1028- _timescaledb_internal | _hyper_19_84_chunk
1029- _timescaledb_internal | _hyper_19_86_chunk
1122+ _timescaledb_internal | _hyper_21_88_chunk
1123+ _timescaledb_internal | _hyper_21_89_chunk
1124+ _timescaledb_internal | _hyper_21_91_chunk
10301125 public | test_hypertable
10311126
1032- SELECT chunk_schema, chunk_name FROM timescaledb_information.chunks WHERE hypertable_name = 'test_hypertable';
1127+ SELECT chunk_schema, chunk_name FROM timescaledb_information.chunks WHERE hypertable_name = 'test_hypertable' ORDER BY chunk_name ;
10331128 chunk_schema | chunk_name
10341129-----------------------+--------------------
1035- _timescaledb_internal | _hyper_19_83_chunk
1036- _timescaledb_internal | _hyper_19_84_chunk
1037- _timescaledb_internal | _hyper_19_85_chunk
1038- _timescaledb_internal | _hyper_19_86_chunk
1130+ _timescaledb_internal | _hyper_21_88_chunk
1131+ _timescaledb_internal | _hyper_21_89_chunk
1132+ _timescaledb_internal | _hyper_21_90_chunk
1133+ _timescaledb_internal | _hyper_21_91_chunk
10391134
10401135-- Cleanup
10411136DROP PUBLICATION test_pub_guc CASCADE;
0 commit comments