Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dDxO1-0001SQ-7q for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2017 18:21:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dDxNz-0008ID-Uo for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2017 18:21:23 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dDxMR-0005Dh-EE for pgsql-hackers@postgresql.org; Thu, 25 May 2017 18:19:47 +0000 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dDxMO-000518-Dn for pgsql-hackers@postgresql.org; Thu, 25 May 2017 18:19:46 +0000 Received: by mail-wm0-x234.google.com with SMTP id b84so102610284wmh.0 for ; Thu, 25 May 2017 11:19:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=from:organization:to:subject:message-id:date:user-agent :mime-version:content-language; bh=IXOCQ8o+o5IMIboKpRy/gTfeNbk0dcfsf22VeWe4POs=; b=ZyqoDtMKuQSCmxvHdE9+qw7epkRj+kyNd+uvBVNheH9WxwHd6k8dnFX1zj6SXJBHLq QyKUDRfV1Dkfhae9YZbgkXEXtvrchXmA3HxRJvtJtWigUtKYqgrOylh8n1U9PzdR28OH gF8QI2HdhrNVhODIwwOu39J2BPMmu4Ts6Kp0a39y1/d2kPy5CfwQMRdKXqK9uc8XDLXd grvvIZnbOhDBga771QFHPT3FW8AP4EMcF/tP1rWvZ52hyaA8bDlHeG6uoZpnyVYMEfYh 42BjMtdsoTmtNgV2CeUs6fVjngEMlJZ27LsukNKhgRmjr5th5L+bnm7LmZmt8mUzgJ7V zlBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:organization:to:subject:message-id:date :user-agent:mime-version:content-language; bh=IXOCQ8o+o5IMIboKpRy/gTfeNbk0dcfsf22VeWe4POs=; b=fUq/viJHiHxhzhTRo47kX7vEXYXhh0LWPLOuxprnkjR+RhXoTUcfyKLN0iEHT5v9k5 0NtTYgq7xD8Oxoy6jp2hfeW9hj6YvuOCZ1yVz+dHKaDlE3xClm4xrffjihruVcPFmZXv 94/yH7k/9Bk1tulEOEekM8uVyvbzeAdrwXQNzVr60Q5rN497aQasGNkmUovKJWcigZQb CsqZjR0nv6ShonBT2ZTHGCRqhpGcSeEpvAcaNqiuzLYc/O4om9EqTsLV9158Pwa4v4ZW P1uSNkr479iYLp25sjcylR1//jLRoqmUf5JFcRPAWpaSIxxQaNaUeSC6+mRkdWqecTVg C3ag== X-Gm-Message-State: AODbwcAaV5aJyku19BUOKMVyHPvcq0updLnFMxRnwf3Hq0IuYls+jP4J EJdADDTFtZdVtk3uUh2NNR8t9ky6W/5FHFXgtK+4Flt6DIPFu6YbVldO1rR2pELvcC+eHR7fGNi xgGB7hI70T5kFHxerOPXNxNr8phzw0KnRohXwqs8/SFGKkuf9FI2Vmfxuh2hSnjbpLbDb+/g5UI Dz X-Received: by 10.223.154.242 with SMTP id a105mr1367076wrc.47.1495736382319; Thu, 25 May 2017 11:19:42 -0700 (PDT) Received: from [10.103.21.2] ([104.222.149.33]) by smtp.gmail.com with ESMTPSA id v7sm7154998wme.5.2017.05.25.11.19.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 25 May 2017 11:19:41 -0700 (PDT) From: Vik Fearing Organization: 2ndQuadrant France To: PostgreSQL-development Subject: Renaming a table to an array's autogenerated name Message-ID: <0f4ade49-4f0b-a9a3-c120-7589f01d1eb8@2ndquadrant.com> Date: Thu, 25 May 2017 14:19:37 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------AF65BDD48FD5D4CA76A3E155" Content-Language: en-US X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------AF65BDD48FD5D4CA76A3E155 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit In commit 9aa3c782c93, Tom fixed a bug in which creating a table _foo when an array type of that name already existed would make the array type change its name to get out of the way. But it missed a trick in that renaming a table would still cause a conflict. Steps to reproduce: postgres=# create table foo (id int); CREATE TABLE postgres=# create table bar (id int); CREATE TABLE postgres=# alter table bar rename to _foo; ERROR: type "_foo" already exists Attached is a patch that fixes this bug. One interesting thing to note however, is that if you rename a table to its own array's name (which was my case when I found this bug), the new array name will be ___foo instead of just __foo. I don't know if it's worth worrying about that. -- Vik Fearing +33 6 46 75 15 36 http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support --------------AF65BDD48FD5D4CA76A3E155 Content-Type: text/x-patch; name="rename-_foo-v01.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="rename-_foo-v01.patch" diff --git a/src/backend/catalog/pg_type.c b/src/backend/catalog/pg_type.c index 04c10c6347..9e4e46f9f5 100644 --- a/src/backend/catalog/pg_type.c +++ b/src/backend/catalog/pg_type.c @@ -695,6 +695,7 @@ RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace) HeapTuple tuple; Form_pg_type typ; Oid arrayOid; + Oid typoid; pg_type_desc = heap_open(TypeRelationId, RowExclusiveLock); @@ -708,10 +709,22 @@ RenameTypeInternal(Oid typeOid, const char *newTypeName, Oid typeNamespace) arrayOid = typ->typarray; + typoid = GetSysCacheOid2(TYPENAMENSP, + CStringGetDatum(newTypeName), + ObjectIdGetDatum(typeNamespace)); + + /* + * See if it's an autogenerated array type, and if so + * rename it out of the way. + */ + if (OidIsValid(typoid) && get_typisdefined(typoid)) + { + if (moveArrayTypeName(typoid, newTypeName, typeNamespace)) + typoid = InvalidOid; + } + /* Just to give a more friendly error than unique-index violation */ - if (SearchSysCacheExists2(TYPENAMENSP, - CStringGetDatum(newTypeName), - ObjectIdGetDatum(typeNamespace))) + if (OidIsValid(typoid)) ereport(ERROR, (errcode(ERRCODE_DUPLICATE_OBJECT), errmsg("type \"%s\" already exists", newTypeName))); diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index c88fd76848..142a782bff 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -128,6 +128,15 @@ SELECT * FROM tmp_new2; DROP TABLE tmp_new; DROP TABLE tmp_new2; +-- renaming to an array's autogenerated name (the array's name should get out of the way) +CREATE TABLE tmp_array (id int); +CREATE TABLE tmp_array2 (id int); +ALTER TABLE tmp_array2 RENAME TO _tmp_array; +DROP TABLE _tmp_array; +DROP TABLE tmp_array; +CREATE TABLE tmp_array (id int); +ALTER TABLE tmp_array RENAME TO _tmp_array; +DROP TABLE _tmp_array; -- ALTER TABLE ... RENAME on non-table relations -- renaming indexes (FIXME: this should probably test the index's functionality) ALTER INDEX IF EXISTS __onek_unique1 RENAME TO tmp_onek_unique1; diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index c0e29720dc..1a893e9e2c 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -165,6 +165,16 @@ SELECT * FROM tmp_new2; DROP TABLE tmp_new; DROP TABLE tmp_new2; +-- renaming to an array's autogenerated name (the array's name should get out of the way) +CREATE TABLE tmp_array (id int); +CREATE TABLE tmp_array2 (id int); +ALTER TABLE tmp_array2 RENAME TO _tmp_array; +DROP TABLE _tmp_array; +DROP TABLE tmp_array; + +CREATE TABLE tmp_array (id int); +ALTER TABLE tmp_array RENAME TO _tmp_array; +DROP TABLE _tmp_array; -- ALTER TABLE ... RENAME on non-table relations -- renaming indexes (FIXME: this should probably test the index's functionality) --------------AF65BDD48FD5D4CA76A3E155 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------AF65BDD48FD5D4CA76A3E155--