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

Appearance settings

Commit b13fd34

Browse filesBrowse 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 a73d083 commit b13fd34
Copy full SHA for b13fd34

File tree

3 files changed

+43
-0
lines changed
Filter options

3 files changed

+43
-0
lines changed

‎src/backend/commands/tablecmds.c

Copy file name to clipboardExpand all lines: src/backend/commands/tablecmds.c
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3019,8 +3019,15 @@ rename_constraint_internal(Oid myrelid,
30193019
ReleaseSysCache(tuple);
30203020

30213021
if (targetrelation)
3022+
{
30223023
relation_close(targetrelation, NoLock); /* close rel but keep lock */
30233024

3025+
/*
3026+
* Invalidate relcache so as others can see the new constraint name.
3027+
*/
3028+
CacheInvalidateRelcache(targetrelation);
3029+
}
3030+
30243031
return address;
30253032
}
30263033

‎src/test/regress/expected/alter_table.out

Copy file name to clipboardExpand all lines: src/test/regress/expected/alter_table.out
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,28 @@ ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; --
393393
NOTICE: relation "constraint_not_exist" does not exist, skipping
394394
ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
395395
NOTICE: relation "constraint_rename_test" does not exist, skipping
396+
-- renaming constraints with cache reset of target relation
397+
CREATE TABLE constraint_rename_cache (a int,
398+
CONSTRAINT chk_a CHECK (a > 0),
399+
PRIMARY KEY (a));
400+
ALTER TABLE constraint_rename_cache
401+
RENAME CONSTRAINT chk_a TO chk_a_new;
402+
ALTER TABLE constraint_rename_cache
403+
RENAME CONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
404+
CREATE TABLE like_constraint_rename_cache
405+
(LIKE constraint_rename_cache INCLUDING ALL);
406+
\d like_constraint_rename_cache
407+
Table "public.like_constraint_rename_cache"
408+
Column | Type | Collation | Nullable | Default
409+
--------+---------+-----------+----------+---------
410+
a | integer | | not null |
411+
Indexes:
412+
"like_constraint_rename_cache_pkey" PRIMARY KEY, btree (a)
413+
Check constraints:
414+
"chk_a_new" CHECK (a > 0)
415+
416+
DROP TABLE constraint_rename_cache;
417+
DROP TABLE like_constraint_rename_cache;
396418
-- FOREIGN KEY CONSTRAINT adding TEST
397419
CREATE TABLE attmp2 (a int primary key);
398420
CREATE TABLE attmp3 (a int, b int);

‎src/test/regress/sql/alter_table.sql

Copy file name to clipboardExpand all lines: src/test/regress/sql/alter_table.sql
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ DROP TABLE constraint_rename_test;
289289
ALTER TABLE IF EXISTS constraint_not_exist RENAME CONSTRAINT con3 TO con3foo; -- ok
290290
ALTER TABLE IF EXISTS constraint_rename_test ADD CONSTRAINT con4 UNIQUE (a);
291291

292+
-- renaming constraints with cache reset of target relation
293+
CREATE TABLE constraint_rename_cache (a int,
294+
CONSTRAINT chk_a CHECK (a > 0),
295+
PRIMARY KEY (a));
296+
ALTER TABLE constraint_rename_cache
297+
RENAME CONSTRAINT chk_a TO chk_a_new;
298+
ALTER TABLE constraint_rename_cache
299+
RENAME CONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
300+
CREATE TABLE like_constraint_rename_cache
301+
(LIKE constraint_rename_cache INCLUDING ALL);
302+
\d like_constraint_rename_cache
303+
DROP TABLE constraint_rename_cache;
304+
DROP TABLE like_constraint_rename_cache;
305+
292306
-- FOREIGN KEY CONSTRAINT adding TEST
293307

294308
CREATE TABLE attmp2 (a int primary key);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.