Fix use-after-free bug when renaming constraints
authorMichael Paquier <michael@paquier.xyz>
Mon, 17 Dec 2018 03:43:39 +0000 (12:43 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 17 Dec 2018 03:43:39 +0000 (12:43 +0900)
This is an oversight from recent commit b13fd344.  While on it, tweak
the previous test with a better name for the renamed primary key.

Detected by buildfarm member prion which forces relation cache release
with -DRELCACHE_FORCE_RELEASE.  Back-patch down to 9.4 as the previous
commit.

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 0f03a9b271d8d872966b713b2fb92048ee996e27..c096a8c4bb3ea52ae1c423bb763256c6cfe06c83 100644 (file)
@@ -3027,12 +3027,12 @@ rename_constraint_internal(Oid myrelid,
 
    if (targetrelation)
    {
-       relation_close(targetrelation, NoLock); /* close rel but keep lock */
-
        /*
         * Invalidate relcache so as others can see the new constraint name.
         */
        CacheInvalidateRelcache(targetrelation);
+
+       relation_close(targetrelation, NoLock); /* close rel but keep lock */
    }
 
    return address;
index 0734296c13102f1941a9a6d07cbee57874650544..83febdae81660619454a09ab3b239ef445db7a26 100644 (file)
@@ -406,7 +406,7 @@ CREATE TABLE constraint_rename_cache (a int,
 ALTER TABLE constraint_rename_cache
   RENAME CONSTRAINT chk_a TO chk_a_new;
 ALTER TABLE constraint_rename_cache
-  RENAME CONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
+  RENAME CONSTRAINT constraint_rename_cache_pkey TO constraint_rename_pkey_new;
 CREATE TABLE like_constraint_rename_cache
   (LIKE constraint_rename_cache INCLUDING ALL);
 \d like_constraint_rename_cache
index 4ec5d1010daee9869dec5e1d783a76dc344b9554..8b2f7d5b2b22beef644f213c3551488a8e5604b2 100644 (file)
@@ -308,7 +308,7 @@ CREATE TABLE constraint_rename_cache (a int,
 ALTER TABLE constraint_rename_cache
   RENAME CONSTRAINT chk_a TO chk_a_new;
 ALTER TABLE constraint_rename_cache
-  RENAME CONSTRAINT constraint_rename_cache_pkey TO chk_a_gt_zero;
+  RENAME CONSTRAINT constraint_rename_cache_pkey TO constraint_rename_pkey_new;
 CREATE TABLE like_constraint_rename_cache
   (LIKE constraint_rename_cache INCLUDING ALL);
 \d like_constraint_rename_cache