Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8cf5f29

Browse files
authored
Merge pull request#228 from postgrespro/pgpro-5255
[PGPRO-5255] fix that ALTER TABLE IF EXISTS ... RENAME TO of not exis…
2 parents6b484c2 +363efa9 commit8cf5f29

7 files changed

+139
-3
lines changed

‎expected/pathman_declarative.out

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,70 @@ SELECT create_hash_partitions('drop_index.test', 'val', 2);
370370
DROP INDEX CONCURRENTLY drop_index.test_0_val_idx;
371371
DROP SCHEMA drop_index CASCADE;
372372
NOTICE: drop cascades to 3 other objects
373+
/*
374+
* Checking that ALTER TABLE IF EXISTS with loaded (and created) pg_pathman extension works the same as in vanilla
375+
*/
376+
CREATE SCHEMA test_nonexistance;
377+
ALTER TABLE IF EXISTS test_nonexistance.nonexistent_table RENAME TO other_table_name;
378+
NOTICE: relation "nonexistent_table" does not exist, skipping
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;
373439
DROP EXTENSION pg_pathman;

‎sql/pathman_declarative.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ CREATE TABLE test.r4 PARTITION OF test.range_rel
3939
FORVALUESFROM ('2015-06-01') TO ('2016-01-01');
4040
\d+test.r4;
4141

42+
ALTERTABLE IF EXISTStest.nonexistent_table ATTACH PARTITION baz DEFAULT;
43+
ALTERTABLE IF EXISTStest.nonexistent_table DETACH PARTITION baz;
44+
4245
DROPSCHEMA test CASCADE;
4346
DROP EXTENSION pg_pathman CASCADE;
4447
DROPSCHEMA pathman CASCADE;

‎sql/pathman_utility_stmt.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,53 @@ DROP INDEX CONCURRENTLY drop_index.test_0_val_idx;
250250

251251
DROPSCHEMA drop_index CASCADE;
252252

253+
/*
254+
* Checking that ALTER TABLE IF EXISTS with loaded (and created) pg_pathman extension works the same as in vanilla
255+
*/
256+
CREATESCHEMAtest_nonexistance;
257+
258+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_table RENAME TO other_table_name;
259+
/* renaming existent tables already tested earlier (see rename.plain_test)*/
260+
261+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_table ADD COLUMN IF NOT EXISTS j INT4;
262+
CREATETABLEtest_nonexistance.existent_table(i INT4);
263+
ALTERTABLE IF EXISTStest_nonexistance.existent_table ADD COLUMN IF NOT EXISTS i INT4;
264+
ALTERTABLE IF EXISTStest_nonexistance.existent_table ADD COLUMN IF NOT EXISTS j INT4;
265+
SELECT attnameFROM pg_attributeWHERE attnum>0AND attrelid='test_nonexistance.existent_table'::REGCLASS;
266+
DROPTABLEtest_nonexistance.existent_table;
267+
268+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_table DROP COLUMN IF EXISTS i;
269+
CREATETABLEtest_nonexistance.existent_table(i INT4);
270+
ALTERTABLE IF EXISTStest_nonexistance.existent_table DROP COLUMN IF EXISTS i;
271+
ALTERTABLE IF EXISTStest_nonexistance.existent_table DROP COLUMN IF EXISTS nonexistent_column;
272+
SELECT attnameFROM pg_attributeWHERE attnum>0AND attrelid='test_nonexistance.existent_table'::REGCLASS;
273+
DROPTABLEtest_nonexistance.existent_table;
274+
275+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_table RENAME COLUMN i TO j;
276+
CREATETABLEtest_nonexistance.existent_table(i INT4);
277+
ALTERTABLE IF EXISTStest_nonexistance.existent_table RENAME COLUMN i TO j;
278+
SELECT attnameFROM pg_attributeWHERE attnum>0AND attrelid='test_nonexistance.existent_table'::REGCLASS;
279+
DROPTABLEtest_nonexistance.existent_table;
280+
281+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_table RENAMECONSTRAINT baz TO bar;
282+
CREATETABLEtest_nonexistance.existent_table(i INT4CONSTRAINT existent_table_i_checkCHECK (i<100));
283+
ALTERTABLE IF EXISTStest_nonexistance.existent_table RENAMECONSTRAINT existent_table_i_check TO existent_table_i_other_check;
284+
DROPTABLEtest_nonexistance.existent_table;
285+
286+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_tableSET SCHEMA nonexistent_schema;
287+
CREATETABLEtest_nonexistance.existent_table(i INT4);
288+
ALTERTABLE IF EXISTStest_nonexistance.existent_tableSET SCHEMA nonexistent_schema;
289+
CREATESCHEMAtest_nonexistance2;
290+
ALTERTABLE IF EXISTStest_nonexistance.existent_tableSET SCHEMA test_nonexistance2;
291+
DROPTABLEtest_nonexistance2.existent_table;
292+
DROPSCHEMA test_nonexistance2 CASCADE;
293+
294+
ALTERTABLE IF EXISTStest_nonexistance.nonexistent_tableSET TABLESPACE nonexistent_tablespace;
295+
CREATETABLEtest_nonexistance.existent_table(i INT4);
296+
ALTERTABLE IF EXISTStest_nonexistance.existent_tableSET TABLESPACE nonexistent_tablespace;
297+
DROPTABLEtest_nonexistance.existent_table;
298+
299+
DROPSCHEMA test_nonexistance CASCADE;
253300

254301

255302
DROP EXTENSION pg_pathman;

‎src/declarative.c

Lines changed: 5 additions & 1 deletion
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
intcnt=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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ is_pathman_related_table_rename(Node *parsetree,
175175
/* Fetch Oid of this relation */
176176
relation_oid=RangeVarGetRelid(rename_stmt->relation,
177177
AccessShareLock,
178-
false);
178+
rename_stmt->missing_ok);
179+
180+
/* Check ALTER TABLE ... IF EXISTS of nonexistent table */
181+
if (rename_stmt->missing_ok&&relation_oid==InvalidOid)
182+
return false;
179183

180184
/* Assume it's a parent */
181185
if (has_pathman_relation_info(relation_oid))
@@ -232,7 +236,11 @@ is_pathman_related_alter_column_type(Node *parsetree,
232236
/* Assume it's a parent, fetch its Oid */
233237
parent_relid=RangeVarGetRelid(alter_table_stmt->relation,
234238
AccessShareLock,
235-
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;
236244

237245
/* Is parent partitioned? */
238246
if ((prel=get_pathman_relation_info(parent_relid))!=NULL)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp