Skip to content

Commit

Permalink
Fix rename for distributed hypertable
Browse files Browse the repository at this point in the history
Fix ALTER TABLE RENAME TO command execution on a distributed
hypertable, make sure data node list is set and command is
executed on the data nodes.

Fix #4491
  • Loading branch information
pmwkaa committed Aug 22, 2022
1 parent 90cace4 commit 81c092a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
30 changes: 9 additions & 21 deletions tsl/src/remote/dist_ddl.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,21 +329,6 @@ dist_ddl_state_set_hypertable(const ProcessUtilityArgs *args)
return true;
}

static HypertableType
dist_ddl_state_get_hypertable_type(void)
{
Cache *hcache;
Hypertable *ht;
HypertableType type;

hcache = ts_hypertable_cache_pin();
ht = ts_hypertable_cache_get_entry(hcache, dist_ddl_state.relid, CACHE_FLAG_NONE);
Assert(ht != NULL);
type = ts_hypertable_get_type(ht);
ts_cache_release(hcache);
return type;
}

static void
dist_ddl_process_create_schema(const ProcessUtilityArgs *args)
{
Expand Down Expand Up @@ -1220,14 +1205,17 @@ dist_ddl_end(EventTriggerData *command)
return;
}

/* Do delayed block of SET SCHEMA and RENAME commands.
*
* In the future those commands might be unblocked and data_node_list could
* be updated here as well.
*/
/* Do delayed block of SET SCHEMA and RENAME commands */
if (OidIsValid(dist_ddl_state.relid))
{
HypertableType type = dist_ddl_state_get_hypertable_type();
/* Get hypertable type and update data node list */
Cache *hcache = ts_hypertable_cache_pin();
Hypertable *ht =
ts_hypertable_cache_get_entry(hcache, dist_ddl_state.relid, CACHE_FLAG_NONE);
Assert(ht != NULL);
HypertableType type = ts_hypertable_get_type(ht);
dist_ddl_state_add_data_node_list_from_ht(ht);
ts_cache_release(hcache);

/* Ensure this operation is executed by the access node session. */
if (type == HYPERTABLE_DISTRIBUTED_MEMBER)
Expand Down
37 changes: 35 additions & 2 deletions tsl/test/expected/dist_ddl.out
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,47 @@ TRUNCATE non_disttable1, non_disttable2;
TRUNCATE disttable;
-- RENAME TO
ALTER TABLE disttable RENAME TO disttable2;
SELECT true FROM pg_tables WHERE tablename = 'disttable2';
bool
------
t
(1 row)

\c :MY_DB1
SELECT true FROM pg_tables WHERE tablename = 'disttable2';
bool
------
t
(1 row)

\c :TEST_DBNAME :ROLE_CLUSTER_SUPERUSER;
SET ROLE :ROLE_1;
ALTER TABLE disttable2 RENAME TO disttable;
SELECT true FROM pg_tables WHERE tablename = 'disttable';
bool
------
t
(1 row)

\c :MY_DB1
SELECT true FROM pg_tables WHERE tablename = 'disttable';
bool
------
t
(1 row)

\c :TEST_DBNAME :ROLE_CLUSTER_SUPERUSER;
SET ROLE :ROLE_1;
-- SET SCHEMA
ALTER TABLE disttable SET SCHEMA some_schema;
ALTER TABLE some_schema.disttable SET SCHEMA public;
\set ON_ERROR_STOP 0
ALTER TABLE disttable SET SCHEMA some_unexist_schema;
ERROR: schema "some_unexist_schema" does not exist
\set ON_ERROR_STOP 1
-- some_schema was not created on data nodes
\set ON_ERROR_STOP 0
ALTER TABLE disttable SET SCHEMA some_schema;
ERROR: [data_node_1]: schema "some_schema" does not exist
\set ON_ERROR_STOP 1
-- OWNER TO
RESET ROLE;
ALTER TABLE disttable OWNER TO :ROLE_2;
Expand Down
18 changes: 16 additions & 2 deletions tsl/test/sql/dist_ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,29 @@ TRUNCATE disttable;

-- RENAME TO
ALTER TABLE disttable RENAME TO disttable2;

SELECT true FROM pg_tables WHERE tablename = 'disttable2';
\c :MY_DB1
SELECT true FROM pg_tables WHERE tablename = 'disttable2';
\c :TEST_DBNAME :ROLE_CLUSTER_SUPERUSER;
SET ROLE :ROLE_1;
ALTER TABLE disttable2 RENAME TO disttable;
SELECT true FROM pg_tables WHERE tablename = 'disttable';
\c :MY_DB1
SELECT true FROM pg_tables WHERE tablename = 'disttable';
\c :TEST_DBNAME :ROLE_CLUSTER_SUPERUSER;
SET ROLE :ROLE_1;

-- SET SCHEMA
ALTER TABLE disttable SET SCHEMA some_schema;
ALTER TABLE some_schema.disttable SET SCHEMA public;
\set ON_ERROR_STOP 0
ALTER TABLE disttable SET SCHEMA some_unexist_schema;
\set ON_ERROR_STOP 1

-- some_schema was not created on data nodes
\set ON_ERROR_STOP 0
ALTER TABLE disttable SET SCHEMA some_schema;
\set ON_ERROR_STOP 1

-- OWNER TO
RESET ROLE;
ALTER TABLE disttable OWNER TO :ROLE_2;
Expand Down

0 comments on commit 81c092a

Please sign in to comment.