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

Commit25b8094

Browse files
committed
Make constraint rename issue relcache invalidation on target relation
When a constraint gets renamed, it may have associated with it a targetrelation (for example domain constraints don't have one). Notinvalidating the target relation cache when issuing the renaming canresult in issues with subsequent commands that refer to the oldconstraint name using the relation cache, causing various failures. Onepattern spotted was using CREATE TABLE LIKE after a constraintrenaming.Reported-by: Stuart <sfbarbee@gmail.com>Author: Amit LangoteReviewed-by: Michael PaquierDiscussion:https://postgr.es/m/2047094.V130LYfLq4@station53.ousa.org
1 parentb054498 commit25b8094

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
returnaddress;
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
ALTERTABLE IF EXISTS constraint_not_exist RENAMECONSTRAINT con3 TO con3foo;-- ok
302302
ALTERTABLE IF EXISTS constraint_rename_test ADDCONSTRAINT con4 UNIQUE (a);
303303

304+
-- renaming constraints with cache reset of target relation
305+
CREATETABLEconstraint_rename_cache (aint,
306+
CONSTRAINT chk_aCHECK (a>0),
307+
PRIMARY KEY (a));
308+
ALTERTABLE constraint_rename_cache
309+
RENAMECONSTRAINT chk_a TO chk_a_new;
310+
ALTERTABLE constraint_rename_cache
311+
RENAMECONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
312+
CREATETABLElike_constraint_rename_cache
313+
(LIKE constraint_rename_cache INCLUDING ALL);
314+
\d like_constraint_rename_cache
315+
DROPTABLE constraint_rename_cache;
316+
DROPTABLE like_constraint_rename_cache;
317+
304318
-- FOREIGN KEY CONSTRAINT adding TEST
305319

306320
CREATETABLEattmp2 (aintprimary key);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp