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

Commit 25b8094

Browse files
committed
Make constraint rename issue relcache invalidation on target relation
When a constraint gets renamed, it may have associated with it a target relation (for example domain constraints don't have one). Not invalidating the target relation cache when issuing the renaming can result in issues with subsequent commands that refer to the old constraint name using the relation cache, causing various failures. One pattern spotted was using CREATE TABLE LIKE after a constraint renaming. Reported-by: Stuart <sfbarbee@gmail.com> Author: Amit Langote Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/2047094.V130LYfLq4@station53.ousa.org
1 parent b054498 commit 25b8094

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,8 +3026,15 @@ rename_constraint_internal(Oid myrelid,
30263026
ReleaseSysCache(tuple);
30273027

30283028
if (targetrelation)
3029+
{
30293030
relation_close(targetrelation, NoLock); /* close rel but keep lock */
30303031

3032+
/*
3033+
* Invalidate relcache so as others can see the new constraint name.
3034+
*/
3035+
CacheInvalidateRelcache(targetrelation);
3036+
}
3037+
30313038
return address;
30323039
}
30333040

src/test/regress/expected/alter_table.out

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,28 @@ ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; --
399399
NOTICE: relation "constraint_not_exist" does not exist, skipping
400400
ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
401401
NOTICE: relation "constraint_rename_test" does not exist, skipping
402+
-- renaming constraints with cache reset of target relation
403+
CREATE TABLE constraint_rename_cache (a int,
404+
CONSTRAINT chk_a CHECK (a > 0),
405+
PRIMARY KEY (a));
406+
ALTER TABLE constraint_rename_cache
407+
RENAME CONSTRAINT chk_a TO chk_a_new;
408+
ALTER TABLE constraint_rename_cache
409+
RENAME CONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
410+
CREATE TABLE like_constraint_rename_cache
411+
(LIKE constraint_rename_cache INCLUDING ALL);
412+
\d like_constraint_rename_cache
413+
Table "public.like_constraint_rename_cache"
414+
Column | Type | Collation | Nullable | Default
415+
--------+---------+-----------+----------+---------
416+
a | integer | | not null |
417+
Indexes:
418+
"like_constraint_rename_cache_pkey" PRIMARY KEY, btree (a)
419+
Check constraints:
420+
"chk_a_new" CHECK (a > 0)
421+
422+
DROP TABLE constraint_rename_cache;
423+
DROP TABLE like_constraint_rename_cache;
402424
-- FOREIGN KEY CONSTRAINT adding TEST
403425
CREATE TABLE attmp2 (a int primary key);
404426
CREATE TABLE attmp3 (a int, b int);

src/test/regress/sql/alter_table.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ DROP TABLE constraint_rename_test;
301301
ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
302302
ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
303303

304+
-- renaming constraints with cache reset of target relation
305+
CREATE TABLE constraint_rename_cache (a int,
306+
CONSTRAINT chk_a CHECK (a > 0),
307+
PRIMARY KEY (a));
308+
ALTER TABLE constraint_rename_cache
309+
RENAME CONSTRAINT chk_a TO chk_a_new;
310+
ALTER TABLE constraint_rename_cache
311+
RENAME CONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
312+
CREATE TABLE like_constraint_rename_cache
313+
(LIKE constraint_rename_cache INCLUDING ALL);
314+
\d like_constraint_rename_cache
315+
DROP TABLE constraint_rename_cache;
316+
DROP TABLE like_constraint_rename_cache;
317+
304318
-- FOREIGN KEY CONSTRAINT adding TEST
305319

306320
CREATE TABLE attmp2 (a int primary key);

0 commit comments

Comments
 (0)