From c4f4d4eab30c9f265e8e179356f43454bafa5948 Mon Sep 17 00:00:00 2001 From: gayyappan Date: Fri, 28 Oct 2022 16:32:43 -0400 Subject: [PATCH] Create index fails if hypertable has foreign table chunk We cannot create indexes on foreign tables. This PR modifies process_index_chunk to skip OSM chunks --- src/process_utility.c | 14 ++++++++++---- tsl/test/expected/chunk_utils_internal.out | 17 +++++++++++++++++ tsl/test/sql/chunk_utils_internal.sql | 8 ++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/process_utility.c b/src/process_utility.c index f2e559929d4..020610b5d9c 100644 --- a/src/process_utility.c +++ b/src/process_utility.c @@ -2430,7 +2430,8 @@ process_index_chunk(Hypertable *ht, Oid chunk_relid, void *arg) Assert(!hypertable_is_distributed(ht)); chunk = ts_chunk_get_by_relid(chunk_relid, true); - + if (IS_OSM_CHUNK(chunk)) /*cannot create index on foreign OSM chunk */ + return; chunk_rel = table_open(chunk_relid, ShareLock); hypertable_index_rel = index_open(info->obj.objectId, AccessShareLock); indexinfo = BuildIndexInfo(hypertable_index_rel); @@ -2494,6 +2495,14 @@ process_index_chunk_multitransaction(int32 hypertable_id, Oid chunk_relid, void } #endif + chunk = ts_chunk_get_by_relid(chunk_relid, true); + if (IS_OSM_CHUNK(chunk)) /*cannot create index on foreign OSM chunk */ + { + PopActiveSnapshot(); + CommitTransactionCommand(); + return; + } + /* * Change user since chunks are typically located in an internal schema * and chunk indexes require metadata changes. In the single-transaction @@ -2514,9 +2523,6 @@ process_index_chunk_multitransaction(int32 hypertable_id, Oid chunk_relid, void */ chunk_rel = table_open(chunk_relid, ShareLock); hypertable_index_rel = index_open(info->obj.objectId, AccessShareLock); - - chunk = ts_chunk_get_by_relid(chunk_relid, true); - /* * Validation happens when creating the hypertable's index, which goes * through the usual DefineIndex mechanism. diff --git a/tsl/test/expected/chunk_utils_internal.out b/tsl/test/expected/chunk_utils_internal.out index 60f5f6b72fb..517b519575e 100644 --- a/tsl/test/expected/chunk_utils_internal.out +++ b/tsl/test/expected/chunk_utils_internal.out @@ -634,6 +634,23 @@ ORDER BY chunk_name; child_hyper_constr | Sat Jan 09 20:00:54.7758 294247 PST | infinity (1 row) +--Utility functions -check index creation on hypertable with OSM chunk +CREATE INDEX hyper_constr_mid_idx ON hyper_constr( mid, time); +SELECT indexname, tablename FROM pg_indexes WHERE indexname = 'hyper_constr_mid_idx'; + indexname | tablename +----------------------+-------------- + hyper_constr_mid_idx | hyper_constr +(1 row) + +DROP INDEX hyper_constr_mid_idx; +CREATE INDEX hyper_constr_mid_idx ON hyper_constr(mid, time) WITH (timescaledb.transaction_per_chunk); +SELECT indexname, tablename FROM pg_indexes WHERE indexname = 'hyper_constr_mid_idx'; + indexname | tablename +----------------------+-------------- + hyper_constr_mid_idx | hyper_constr +(1 row) + +DROP INDEX hyper_constr_mid_idx; -- clean up databases created \c :TEST_DBNAME :ROLE_SUPERUSER DROP DATABASE postgres_fdw_db; diff --git a/tsl/test/sql/chunk_utils_internal.sql b/tsl/test/sql/chunk_utils_internal.sql index 3e629a2bb2d..ca90a35ddcc 100644 --- a/tsl/test/sql/chunk_utils_internal.sql +++ b/tsl/test/sql/chunk_utils_internal.sql @@ -394,6 +394,14 @@ FROM chunk_view WHERE hypertable_name = 'hyper_constr' ORDER BY chunk_name; +--Utility functions -check index creation on hypertable with OSM chunk +CREATE INDEX hyper_constr_mid_idx ON hyper_constr( mid, time); +SELECT indexname, tablename FROM pg_indexes WHERE indexname = 'hyper_constr_mid_idx'; +DROP INDEX hyper_constr_mid_idx; + +CREATE INDEX hyper_constr_mid_idx ON hyper_constr(mid, time) WITH (timescaledb.transaction_per_chunk); +SELECT indexname, tablename FROM pg_indexes WHERE indexname = 'hyper_constr_mid_idx'; +DROP INDEX hyper_constr_mid_idx; -- clean up databases created \c :TEST_DBNAME :ROLE_SUPERUSER