Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i09Th-0003pU-O3 for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 19:07:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i09Tg-00056c-If for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 19:07:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i09Tg-0004yX-3D for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2019 19:07:32 +0000 Received: from smtp1.redcom.com ([192.86.3.143]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1i09Td-0000UH-2a for pgsql-general@postgresql.org; Tue, 20 Aug 2019 19:07:30 +0000 Received: from localhost (localhost [127.0.0.1]) by smtp1.redcom.com (Postfix) with ESMTP id 37813A2AB for ; Tue, 20 Aug 2019 15:07:28 -0400 (EDT) X-Virus-Scanned: amavisd-new at redcom.com Received: from smtp1.redcom.com ([127.0.0.1]) by localhost (smtp1.redcom.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VBtCpl6L9JG5 for ; Tue, 20 Aug 2019 15:07:26 -0400 (EDT) Received: from pie.redcom.com (pie [192.168.33.15]) by smtp1.redcom.com (Postfix) with ESMTP id 56BC6A2A7 for ; Tue, 20 Aug 2019 15:07:26 -0400 (EDT) Received: from exch-02.redcom.com (exch-02.redcom.com [192.168.32.9]) by pie.redcom.com (8.11.7p1+Sun/8.10.2) with ESMTP id x7KJ7Pf24520 for ; Tue, 20 Aug 2019 15:07:26 -0400 (EDT) Received: from exch-03.redcom.com (fd00::8549:68c0:3d5f:ee62) by exch-02.redcom.com (fd00::ccaa:c259:22f8:6f4b) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 20 Aug 2019 15:07:25 -0400 Received: from exch-03.redcom.com ([fe80::a442:ce34:c9c8:268f]) by exch-03.redcom.com ([fe80::a442:ce34:c9c8:268f%3]) with mapi id 15.02.0330.005; Tue, 20 Aug 2019 15:07:25 -0400 From: "Day, David" To: "pgsql-general@postgresql.org" Subject: Rename a column if not already renamed.? Thread-Topic: Rename a column if not already renamed.? Thread-Index: AdVXikX1liX7eXu6QciLSjE751XxEw== Date: Tue, 20 Aug 2019 19:07:24 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [192.168.84.42] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I have a function used by my patch files which attempts to determine if the= old column name exists, if so it proceeds with the rename.=20 CREATE OR REPLACE FUNCTION sys.rename_column(schema_table_ regclass, old_na= me_ text, new_name text) RETURNS boolean LANGUAGE plpgsql AS $function$ BEGIN IF EXISTS(SELECT 1 FROM pg_attribute WHERE attrelid =3D schema_table_ AND attname =3D old_name_ AND NOT attisdropped) THEN EXECUTE format ('ALTER TABLE %s RENAME %s TO %s', schema_table_, old_na= me_, new_name); RETURN TRUE; ELSE RETURN FALSE; END IF; END; $function$; This seems to function correctly except. If I am upgrading a site from Postgres 9.6 to version 11.3 and the column h= as already been renamed in the 9.6 branch the above existence test fails and an exception is generated. It all seems to work correctly if I repeat this same patch in the 11.3 bran= ch. The function definition is the same for both branches. I suspect I am overlooking some fundamental issue here. Anyone with a thought. Thanks Dave Day