Skip to content

Commit b95c847

Browse files
Don't create hypertable for published tables
Logical Replication of hypertables is not supported (for implementation reasons). We do not allow creation of hypertables with publications and now throw an error if attempted. (cherry picked from commit 7173276)
1 parent 353db20 commit b95c847

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

.unreleased/pr_7911

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes: #7911 Don't create hypertable for published tables
2+
Thanks: @soedirgo for reporting that published tables don't get dropped when they have a hypertable

src/hypertable.c

+12
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,18 @@ ts_hypertable_create_from_info(Oid table_relid, int32 hypertable_id, uint32 flag
18641864
ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("invalid relation type")));
18651865
}
18661866

1867+
/*
1868+
* Check that the table is not part of any publication
1869+
*/
1870+
if (GetRelationPublications(table_relid) != NIL)
1871+
{
1872+
ereport(ERROR,
1873+
(errcode(ERRCODE_TS_OPERATION_NOT_SUPPORTED),
1874+
errmsg("cannot create hypertable for table \"%s\" because it is part of a "
1875+
"publication",
1876+
get_rel_name(table_relid))));
1877+
}
1878+
18671879
/* Check that the table doesn't have any unsupported constraints */
18681880
hypertable_validate_constraints(table_relid);
18691881

test/expected/create_hypertable.out

+30
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ SELECT * FROM show_chunks('test') ch, LATERAL test.show_indexes(ch) ORDER BY 1,
11211121
_timescaledb_internal._hyper_25_24_chunk | _timescaledb_internal._hyper_25_24_chunk_test_val_idx | {val} | | f | f | f |
11221122
(1 row)
11231123

1124+
DROP TABLE test;
11241125
-- test creating a hypertable with a primary key where the partitioning column is not part of the primary key
11251126
CREATE TABLE test_schema.partition_not_pk (id INT NOT NULL, device_id INT NOT NULL, time TIMESTAMPTZ NOT NULL, a TEXT NOT NULL, PRIMARY KEY (id));
11261127
\set ON_ERROR_STOP 0
@@ -1137,3 +1138,32 @@ ERROR: cannot create a unique index without the column "time" (used in partitio
11371138
HINT: If you're creating a hypertable on a table with a primary key, ensure the partitioning column is part of the primary or composite key.
11381139
\set ON_ERROR_STOP 1
11391140
DROP TABLE test_schema.partition_not_pk;
1141+
-- test hypertable is not created for a table that is a part of a publication
1142+
SET client_min_messages = ERROR;
1143+
CREATE TABLE test (timestamp TIMESTAMPTZ NOT NULL);
1144+
CREATE PUBLICATION publication_test;
1145+
ALTER PUBLICATION publication_test ADD TABLE test;
1146+
\set ON_ERROR_STOP 0
1147+
SELECT create_hypertable('test', 'timestamp');
1148+
ERROR: cannot create hypertable for table "test" because it is part of a publication
1149+
\set ON_ERROR_STOP 1
1150+
INSERT INTO test (timestamp) values (now());
1151+
ALTER PUBLICATION publication_test DROP TABLE test;
1152+
DROP PUBLICATION publication_test;
1153+
DROP TABLE test;
1154+
CREATE TABLE test (timestamp TIMESTAMPTZ NOT NULL);
1155+
CREATE PUBLICATION publication_test1;
1156+
CREATE PUBLICATION publication_test2;
1157+
ALTER PUBLICATION publication_test1 ADD TABLE test;
1158+
ALTER PUBLICATION publication_test2 ADD TABLE test;
1159+
\set ON_ERROR_STOP 0
1160+
SELECT create_hypertable('test', 'timestamp');
1161+
ERROR: cannot create hypertable for table "test" because it is part of a publication
1162+
\set ON_ERROR_STOP 1
1163+
INSERT INTO test (timestamp) values (now());
1164+
ALTER PUBLICATION publication_test1 DROP TABLE test;
1165+
ALTER PUBLICATION publication_test2 DROP TABLE test;
1166+
DROP PUBLICATION publication_test1;
1167+
DROP PUBLICATION publication_test2;
1168+
DROP TABLE test;
1169+
RESET client_min_messages;

test/sql/create_hypertable.sql

+30
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ SELECT FROM create_hypertable('test', 'time', create_default_indexes => FALSE, m
666666
-- only user indexes should be returned
667667
SELECT * FROM test.show_indexes('test') ORDER BY 1;
668668
SELECT * FROM show_chunks('test') ch, LATERAL test.show_indexes(ch) ORDER BY 1, 2;
669+
DROP TABLE test;
669670

670671
-- test creating a hypertable with a primary key where the partitioning column is not part of the primary key
671672
CREATE TABLE test_schema.partition_not_pk (id INT NOT NULL, device_id INT NOT NULL, time TIMESTAMPTZ NOT NULL, a TEXT NOT NULL, PRIMARY KEY (id));
@@ -680,3 +681,32 @@ CREATE TABLE test_schema.partition_not_pk (id INT NOT NULL, device_id INT NOT NU
680681
select create_hypertable ('test_schema.partition_not_pk', 'time');
681682
\set ON_ERROR_STOP 1
682683
DROP TABLE test_schema.partition_not_pk;
684+
685+
-- test hypertable is not created for a table that is a part of a publication
686+
SET client_min_messages = ERROR;
687+
CREATE TABLE test (timestamp TIMESTAMPTZ NOT NULL);
688+
CREATE PUBLICATION publication_test;
689+
ALTER PUBLICATION publication_test ADD TABLE test;
690+
\set ON_ERROR_STOP 0
691+
SELECT create_hypertable('test', 'timestamp');
692+
\set ON_ERROR_STOP 1
693+
INSERT INTO test (timestamp) values (now());
694+
ALTER PUBLICATION publication_test DROP TABLE test;
695+
DROP PUBLICATION publication_test;
696+
DROP TABLE test;
697+
698+
CREATE TABLE test (timestamp TIMESTAMPTZ NOT NULL);
699+
CREATE PUBLICATION publication_test1;
700+
CREATE PUBLICATION publication_test2;
701+
ALTER PUBLICATION publication_test1 ADD TABLE test;
702+
ALTER PUBLICATION publication_test2 ADD TABLE test;
703+
\set ON_ERROR_STOP 0
704+
SELECT create_hypertable('test', 'timestamp');
705+
\set ON_ERROR_STOP 1
706+
INSERT INTO test (timestamp) values (now());
707+
ALTER PUBLICATION publication_test1 DROP TABLE test;
708+
ALTER PUBLICATION publication_test2 DROP TABLE test;
709+
DROP PUBLICATION publication_test1;
710+
DROP PUBLICATION publication_test2;
711+
DROP TABLE test;
712+
RESET client_min_messages;

0 commit comments

Comments
 (0)