Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 363efa9

Browse files
committed
[PGPRO-5255] corrections based on the review
1 parent 2062ab9 commit 363efa9

7 files changed

+131
-11
lines changed

expected/pathman_declarative.out

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Check constraints:
9494
"pathman_r4_check" CHECK (dt >= '06-01-2015'::date AND dt < '01-01-2016'::date)
9595
Inherits: test.range_rel
9696

97+
ALTER TABLE IF EXISTS test.nonexistent_table ATTACH PARTITION baz DEFAULT;
98+
NOTICE: relation "nonexistent_table" does not exist, skipping
99+
ALTER TABLE IF EXISTS test.nonexistent_table DETACH PARTITION baz;
100+
NOTICE: relation "nonexistent_table" does not exist, skipping
97101
DROP SCHEMA test CASCADE;
98102
NOTICE: drop cascades to 8 other objects
99103
DROP EXTENSION pg_pathman CASCADE;

expected/pathman_declarative_1.out

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ Check constraints:
9494
"pathman_r4_check" CHECK (dt >= '06-01-2015'::date AND dt < '01-01-2016'::date)
9595
Inherits: test.range_rel
9696

97+
ALTER TABLE IF EXISTS test.nonexistent_table ATTACH PARTITION baz DEFAULT;
98+
NOTICE: relation "nonexistent_table" does not exist, skipping
99+
ALTER TABLE IF EXISTS test.nonexistent_table DETACH PARTITION baz;
100+
NOTICE: relation "nonexistent_table" does not exist, skipping
97101
DROP SCHEMA test CASCADE;
98102
NOTICE: drop cascades to 8 other objects
99103
DROP EXTENSION pg_pathman CASCADE;

expected/pathman_utility_stmt.out

+63-4
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,69 @@ DROP INDEX CONCURRENTLY drop_index.test_0_val_idx;
371371
DROP SCHEMA drop_index CASCADE;
372372
NOTICE: drop cascades to 3 other objects
373373
/*
374-
* Test, that ALTER TABLE IF EXISTS ... RENAME TO of not existed table generate NOTICE instead of ERROR
374+
* Checking that ALTER TABLE IF EXISTS with loaded (and created) pg_pathman extension works the same as in vanilla
375375
*/
376-
CREATE SCHEMA rename_nonexistent;
377-
ALTER TABLE IF EXISTS rename_nonexistent.nonexistent_table RENAME TO other_table_name;
376+
CREATE SCHEMA test_nonexistance;
377+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME TO other_table_name;
378378
NOTICE: relation "nonexistent_table" does not exist, skipping
379-
DROP SCHEMA rename_nonexistent CASCADE;
379+
/* renaming existent tables already tested earlier (see rename.plain_test) */
380+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table ADD COLUMN IF NOT EXISTS j INT4;
381+
NOTICE: relation "nonexistent_table" does not exist, skipping
382+
CREATE TABLE test_nonexistance.existent_table(i INT4);
383+
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS i INT4;
384+
NOTICE: column "i" of relation "existent_table" already exists, skipping
385+
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS j INT4;
386+
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
387+
attname
388+
---------
389+
i
390+
j
391+
(2 rows)
392+
393+
DROP TABLE test_nonexistance.existent_table;
394+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table DROP COLUMN IF EXISTS i;
395+
NOTICE: relation "nonexistent_table" does not exist, skipping
396+
CREATE TABLE test_nonexistance.existent_table(i INT4);
397+
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS i;
398+
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS nonexistent_column;
399+
NOTICE: column "nonexistent_column" of relation "existent_table" does not exist, skipping
400+
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
401+
attname
402+
------------------------------
403+
........pg.dropped.1........
404+
(1 row)
405+
406+
DROP TABLE test_nonexistance.existent_table;
407+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME COLUMN i TO j;
408+
NOTICE: relation "nonexistent_table" does not exist, skipping
409+
CREATE TABLE test_nonexistance.existent_table(i INT4);
410+
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME COLUMN i TO j;
411+
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
412+
attname
413+
---------
414+
j
415+
(1 row)
416+
417+
DROP TABLE test_nonexistance.existent_table;
418+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME CONSTRAINT baz TO bar;
419+
NOTICE: relation "nonexistent_table" does not exist, skipping
420+
CREATE TABLE test_nonexistance.existent_table(i INT4 CONSTRAINT existent_table_i_check CHECK (i < 100));
421+
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME CONSTRAINT existent_table_i_check TO existent_table_i_other_check;
422+
DROP TABLE test_nonexistance.existent_table;
423+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET SCHEMA nonexistent_schema;
424+
NOTICE: relation "nonexistent_table" does not exist, skipping
425+
CREATE TABLE test_nonexistance.existent_table(i INT4);
426+
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA nonexistent_schema;
427+
ERROR: schema "nonexistent_schema" does not exist
428+
CREATE SCHEMA test_nonexistance2;
429+
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA test_nonexistance2;
430+
DROP TABLE test_nonexistance2.existent_table;
431+
DROP SCHEMA test_nonexistance2 CASCADE;
432+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET TABLESPACE nonexistent_tablespace;
433+
NOTICE: relation "nonexistent_table" does not exist, skipping
434+
CREATE TABLE test_nonexistance.existent_table(i INT4);
435+
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET TABLESPACE nonexistent_tablespace;
436+
ERROR: tablespace "nonexistent_tablespace" does not exist
437+
DROP TABLE test_nonexistance.existent_table;
438+
DROP SCHEMA test_nonexistance CASCADE;
380439
DROP EXTENSION pg_pathman;

sql/pathman_declarative.sql

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ CREATE TABLE test.r4 PARTITION OF test.range_rel
3939
FOR VALUES FROM ('2015-06-01') TO ('2016-01-01');
4040
\d+ test.r4;
4141

42+
ALTER TABLE IF EXISTS test.nonexistent_table ATTACH PARTITION baz DEFAULT;
43+
ALTER TABLE IF EXISTS test.nonexistent_table DETACH PARTITION baz;
44+
4245
DROP SCHEMA test CASCADE;
4346
DROP EXTENSION pg_pathman CASCADE;
4447
DROP SCHEMA pathman CASCADE;

sql/pathman_utility_stmt.sql

+45-4
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,52 @@ DROP INDEX CONCURRENTLY drop_index.test_0_val_idx;
251251
DROP SCHEMA drop_index CASCADE;
252252

253253
/*
254-
* Test, that ALTER TABLE IF EXISTS ... RENAME TO of not existed table generate NOTICE instead of ERROR
254+
* Checking that ALTER TABLE IF EXISTS with loaded (and created) pg_pathman extension works the same as in vanilla
255255
*/
256-
CREATE SCHEMA rename_nonexistent;
257-
ALTER TABLE IF EXISTS rename_nonexistent.nonexistent_table RENAME TO other_table_name;
258-
DROP SCHEMA rename_nonexistent CASCADE;
256+
CREATE SCHEMA test_nonexistance;
257+
258+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME TO other_table_name;
259+
/* renaming existent tables already tested earlier (see rename.plain_test) */
260+
261+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table ADD COLUMN IF NOT EXISTS j INT4;
262+
CREATE TABLE test_nonexistance.existent_table(i INT4);
263+
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS i INT4;
264+
ALTER TABLE IF EXISTS test_nonexistance.existent_table ADD COLUMN IF NOT EXISTS j INT4;
265+
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
266+
DROP TABLE test_nonexistance.existent_table;
267+
268+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table DROP COLUMN IF EXISTS i;
269+
CREATE TABLE test_nonexistance.existent_table(i INT4);
270+
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS i;
271+
ALTER TABLE IF EXISTS test_nonexistance.existent_table DROP COLUMN IF EXISTS nonexistent_column;
272+
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
273+
DROP TABLE test_nonexistance.existent_table;
274+
275+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME COLUMN i TO j;
276+
CREATE TABLE test_nonexistance.existent_table(i INT4);
277+
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME COLUMN i TO j;
278+
SELECT attname FROM pg_attribute WHERE attnum > 0 AND attrelid = 'test_nonexistance.existent_table'::REGCLASS;
279+
DROP TABLE test_nonexistance.existent_table;
280+
281+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME CONSTRAINT baz TO bar;
282+
CREATE TABLE test_nonexistance.existent_table(i INT4 CONSTRAINT existent_table_i_check CHECK (i < 100));
283+
ALTER TABLE IF EXISTS test_nonexistance.existent_table RENAME CONSTRAINT existent_table_i_check TO existent_table_i_other_check;
284+
DROP TABLE test_nonexistance.existent_table;
285+
286+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET SCHEMA nonexistent_schema;
287+
CREATE TABLE test_nonexistance.existent_table(i INT4);
288+
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA nonexistent_schema;
289+
CREATE SCHEMA test_nonexistance2;
290+
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET SCHEMA test_nonexistance2;
291+
DROP TABLE test_nonexistance2.existent_table;
292+
DROP SCHEMA test_nonexistance2 CASCADE;
293+
294+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table SET TABLESPACE nonexistent_tablespace;
295+
CREATE TABLE test_nonexistance.existent_table(i INT4);
296+
ALTER TABLE IF EXISTS test_nonexistance.existent_table SET TABLESPACE nonexistent_tablespace;
297+
DROP TABLE test_nonexistance.existent_table;
298+
299+
DROP SCHEMA test_nonexistance CASCADE;
259300

260301

261302
DROP EXTENSION pg_pathman;

src/declarative.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ is_pathman_related_partitioning_cmd(Node *parsetree, Oid *parent_relid)
7575
AlterTableStmt *stmt = (AlterTableStmt *) parsetree;
7676
int cnt = 0;
7777

78-
*parent_relid = RangeVarGetRelid(stmt->relation, NoLock, false);
78+
*parent_relid = RangeVarGetRelid(stmt->relation, NoLock, stmt->missing_ok);
79+
80+
if (stmt->missing_ok && *parent_relid == InvalidOid)
81+
return false;
82+
7983
if ((prel = get_pathman_relation_info(*parent_relid)) == NULL)
8084
return false;
8185

src/utility_stmt_hooking.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ is_pathman_related_table_rename(Node *parsetree,
176176
relation_oid = RangeVarGetRelid(rename_stmt->relation,
177177
AccessShareLock,
178178
rename_stmt->missing_ok);
179-
/* PGPRO-5255: check ALTER TABLE IF EXISTS of non existent table */
179+
180+
/* Check ALTER TABLE ... IF EXISTS of nonexistent table */
180181
if (rename_stmt->missing_ok && relation_oid == InvalidOid)
181182
return false;
182183

@@ -235,7 +236,11 @@ is_pathman_related_alter_column_type(Node *parsetree,
235236
/* Assume it's a parent, fetch its Oid */
236237
parent_relid = RangeVarGetRelid(alter_table_stmt->relation,
237238
AccessShareLock,
238-
false);
239+
alter_table_stmt->missing_ok);
240+
241+
/* Check ALTER TABLE ... IF EXISTS of nonexistent table */
242+
if (alter_table_stmt->missing_ok && parent_relid == InvalidOid)
243+
return false;
239244

240245
/* Is parent partitioned? */
241246
if ((prel = get_pathman_relation_info(parent_relid)) != NULL)

0 commit comments

Comments
 (0)