Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

[PGPRO-5255] fix that ALTER TABLE IF EXISTS ... RENAME TO of not exis… #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions expected/pathman_declarative.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Check constraints:
"pathman_r4_check" CHECK (dt >= '06-01-2015'::date AND dt < '01-01-2016'::date)
Inherits: test.range_rel

ALTER TABLE IF EXISTS test.nonexistent_table ATTACH PARTITION baz DEFAULT;
NOTICE: relation "nonexistent_table" does not exist, skipping
ALTER TABLE IF EXISTS test.nonexistent_table DETACH PARTITION baz;
NOTICE: relation "nonexistent_table" does not exist, skipping
DROP SCHEMA test CASCADE;
NOTICE: drop cascades to 8 other objects
DROP EXTENSION pg_pathman CASCADE;
Expand Down
4 changes: 4 additions & 0 deletions expected/pathman_declarative_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Check constraints:
"pathman_r4_check" CHECK (dt >= '06-01-2015'::date AND dt < '01-01-2016'::date)
Inherits: test.range_rel

ALTER TABLE IF EXISTS test.nonexistent_table ATTACH PARTITION baz DEFAULT;
NOTICE: relation "nonexistent_table" does not exist, skipping
ALTER TABLE IF EXISTS test.nonexistent_table DETACH PARTITION baz;
NOTICE: relation "nonexistent_table" does not exist, skipping
DROP SCHEMA test CASCADE;
NOTICE: drop cascades to 8 other objects
DROP EXTENSION pg_pathman CASCADE;
Expand Down
66 changes: 66 additions & 0 deletions expected/pathman_utility_stmt.out
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,70 @@ SELECT create_hash_partitions('drop_index.test', 'val', 2);
DROP INDEX CONCURRENTLY drop_index.test_0_val_idx;
DROP SCHEMA drop_index CASCADE;
NOTICE: drop cascades to 3 other objects
/*
* Checking that ALTER TABLE IF EXISTS with loaded (and created) pg_pathman extension works the same as in vanilla
*/
CREATE SCHEMA test_nonexistance;
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME TO other_table_name;
NOTICE: relation "nonexistent_table" does not exist, skipping
/* renaming existent tables already tested earlier (see rename.plain_test) */
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table ADD COLUMN IF NOT EXISTS j INT4;
NOTICE: relation "nonexistent_table" does not exist, skipping
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS i INT4;
NOTICE: column "i" of relation "existent_table" already exists, skipping
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS j INT4;
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
attname
---------
i
j
(2 rows)

DROP TABLE test_nonexistance.existent_table;
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table DROP COLUMN IF EXISTS i;
NOTICE: relation "nonexistent_table" does not exist, skipping
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS i;
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS nonexistent_column;
NOTICE: column "nonexistent_column" of relation "existent_table" does not exist, skipping
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
attname
------------------------------
........pg.dropped.1........
(1 row)

DROP TABLE test_nonexistance.existent_table;
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME COLUMN i TO j;
NOTICE: relation "nonexistent_table" does not exist, skipping
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME COLUMN i TO j;
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
attname
---------
j
(1 row)

DROP TABLE test_nonexistance.existent_table;
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME CONSTRAINT baz TO bar;
NOTICE: relation "nonexistent_table" does not exist, skipping
CREATE TABLE test_nonexistance.existent_table(i INT4 CONSTRAINT existent_table_i_check CHECK (i < 100));
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME CONSTRAINT existent_table_i_check TO existent_table_i_other_check;
DROP TABLE test_nonexistance.existent_table;
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET SCHEMA nonexistent_schema;
NOTICE: relation "nonexistent_table" does not exist, skipping
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA nonexistent_schema;
ERROR: schema "nonexistent_schema" does not exist
CREATE SCHEMA test_nonexistance2;
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA test_nonexistance2;
DROP TABLE test_nonexistance2.existent_table;
DROP SCHEMA test_nonexistance2 CASCADE;
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET TABLESPACE nonexistent_tablespace;
NOTICE: relation "nonexistent_table" does not exist, skipping
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET TABLESPACE nonexistent_tablespace;
ERROR: tablespace "nonexistent_tablespace" does not exist
DROP TABLE test_nonexistance.existent_table;
DROP SCHEMA test_nonexistance CASCADE;
DROP EXTENSION pg_pathman;
3 changes: 3 additions & 0 deletions sql/pathman_declarative.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ CREATE TABLE test.r4 PARTITION OF test.range_rel
FOR VALUES FROM ('2015-06-01') TO ('2016-01-01');
\d+ test.r4;

ALTER TABLE IF EXISTS test.nonexistent_table ATTACH PARTITION baz DEFAULT;
ALTER TABLE IF EXISTS test.nonexistent_table DETACH PARTITION baz;

DROP SCHEMA test CASCADE;
DROP EXTENSION pg_pathman CASCADE;
DROP SCHEMA pathman CASCADE;
47 changes: 47 additions & 0 deletions sql/pathman_utility_stmt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,53 @@ DROP INDEX CONCURRENTLY drop_index.test_0_val_idx;

DROP SCHEMA drop_index CASCADE;

/*
* Checking that ALTER TABLE IF EXISTS with loaded (and created) pg_pathman extension works the same as in vanilla
*/
CREATE SCHEMA test_nonexistance;

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME TO other_table_name;
/* renaming existent tables already tested earlier (see rename.plain_test) */

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table ADD COLUMN IF NOT EXISTS j INT4;
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS i INT4;
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS j INT4;
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
DROP TABLE test_nonexistance.existent_table;

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table DROP COLUMN IF EXISTS i;
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS i;
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS nonexistent_column;
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
DROP TABLE test_nonexistance.existent_table;

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME COLUMN i TO j;
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME COLUMN i TO j;
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
DROP TABLE test_nonexistance.existent_table;

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME CONSTRAINT baz TO bar;
CREATE TABLE test_nonexistance.existent_table(i INT4 CONSTRAINT existent_table_i_check CHECK (i < 100));
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME CONSTRAINT existent_table_i_check TO existent_table_i_other_check;
DROP TABLE test_nonexistance.existent_table;

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET SCHEMA nonexistent_schema;
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA nonexistent_schema;
CREATE SCHEMA test_nonexistance2;
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA test_nonexistance2;
DROP TABLE test_nonexistance2.existent_table;
DROP SCHEMA test_nonexistance2 CASCADE;

ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET TABLESPACE nonexistent_tablespace;
CREATE TABLE test_nonexistance.existent_table(i INT4);
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET TABLESPACE nonexistent_tablespace;
DROP TABLE test_nonexistance.existent_table;

DROP SCHEMA test_nonexistance CASCADE;


DROP EXTENSION pg_pathman;
6 changes: 5 additions & 1 deletion src/declarative.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ is_pathman_related_partitioning_cmd(Node *parsetree, Oid *parent_relid)
AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
int cnt = 0;

*parent_relid = RangeVarGetRelid(stmt->relation, NoLock, false);
*parent_relid = RangeVarGetRelid(stmt->relation, NoLock, stmt->missing_ok);

if (stmt->missing_ok && *parent_relid == InvalidOid)
return false;

if ((prel = get_pathman_relation_info(*parent_relid)) == NULL)
return false;

Expand Down
12 changes: 10 additions & 2 deletions src/utility_stmt_hooking.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ is_pathman_related_table_rename(Node *parsetree,
/* Fetch Oid of this relation */
relation_oid = RangeVarGetRelid(rename_stmt->relation,
AccessShareLock,
false);
rename_stmt->missing_ok);

/* Check ALTER TABLE ... IF EXISTS of nonexistent table */
if (rename_stmt->missing_ok && relation_oid == InvalidOid)
return false;

/* Assume it's a parent */
if (has_pathman_relation_info(relation_oid))
Expand Down Expand Up @@ -232,7 +236,11 @@ is_pathman_related_alter_column_type(Node *parsetree,
/* Assume it's a parent, fetch its Oid */
parent_relid = RangeVarGetRelid(alter_table_stmt->relation,
AccessShareLock,
false);
alter_table_stmt->missing_ok);

/* Check ALTER TABLE ... IF EXISTS of nonexistent table */
if (alter_table_stmt->missing_ok && parent_relid == InvalidOid)
return false;

/* Is parent partitioned? */
if ((prel = get_pathman_relation_info(parent_relid)) != NULL)
Expand Down