Skip to content

Commit

Permalink
Create index fails if hypertable has foreign table chunk
Browse files Browse the repository at this point in the history
We cannot create indexes on foreign tables. This PR modifies
process_index_chunk to skip OSM chunks
  • Loading branch information
gayyappan committed Oct 28, 2022
1 parent e08e0a5 commit c4f4d4e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/process_utility.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions tsl/test/expected/chunk_utils_internal.out
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tsl/test/sql/chunk_utils_internal.sql
Expand Up @@ -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
Expand Down

0 comments on commit c4f4d4e

Please sign in to comment.