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 1i09cO-0004HH-Ho for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 19:16: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 1i09cN-0005R3-4C for pgsql-general@arkaria.postgresql.org; Tue, 20 Aug 2019 19:16:31 +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 1i09cL-0005Qw-LK for pgsql-general@lists.postgresql.org; Tue, 20 Aug 2019 19:16:30 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i09cI-0000Yr-PG for pgsql-general@postgresql.org; Tue, 20 Aug 2019 19:16:28 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id BC7B521CFD; Tue, 20 Aug 2019 15:16:25 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 20 Aug 2019 15:16:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=f 7a7/rnqFCE/QQ3iLsT9WFnkGEwiRV4zMuvBzvpYLhg=; b=KIZRjWRJzBjC33TIW 2xGDK994zMPfsjbn+Y5XcJwm1iJXRqy4KBTCuj/vFJvkCcZA3GfX72MKsDxsUXmc pP6aWnt5YXsxWKTd1oBs1FuP5sqdRA6H3okPDWeR89NIQ98zZGGr1peDzSdN0AGC SGg+GjX8UMp7ObAp8xJhf1tECE00Lu1IsyyR4h6UeRfDbdlENvFLHJKaslioKxyz 6/YVje/SiMUCDl53Cm1IZWK/jcWTv1ycvi4dkdq8LgbwOatDqX5xT63GP6sN9tH6 O0028r9egUIc1eSodXS+Y6uuktQxJxqYNSRm9L8J3lRtrbs4qoF6DlMA+yAMScPm A+C7A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=f7a7/rnqFCE/QQ3iLsT9WFnkGEwiRV4zMuvBzvpYL hg=; b=gCDBzSW+cYWgyGcA+bodGowa1ClQ6riPEJ8vHzhWSBrilPG0mw5W15oli sLElaFiJg7JIDyATPRAVJd7sphkEn2ejIHnTFvsDlmioxuaqhz8Jyt6DlHRDCtbs AA1PW10gzan649e3URGgsfVB3D9sp/FTouXSz/sPcPd/DujO0zxWO61gJAupRYD1 DMjG7E6qbDYE6Qi1CiWkg7kh9ctlRVYGx6fWU2kdM0Yqk64kCFL9tf7CmjDChVcO bPPexdU3zc19NkvmsSnVr3sc/xENAHqnx5L+Iz3bshbjXyz3ekAYJQAYmfdzEGcF MG8hxXXAwMBxL3/XANs8nKvXt/msg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudeguddgudeffecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhf rhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhush htvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id CC69980059; Tue, 20 Aug 2019 15:16:24 -0400 (EDT) Subject: Re: Rename a column if not already renamed.? To: "Day, David" , "pgsql-general@postgresql.org" References: From: Adrian Klaver Message-ID: <80e512f9-b6c9-c9b0-24db-b3662ff67a94@aklaver.com> Date: Tue, 20 Aug 2019 12:16:24 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 8/20/19 12:07 PM, Day, David wrote: > 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. > > > CREATE OR REPLACE FUNCTION sys.rename_column(schema_table_ regclass, old_name_ text, new_name text) > RETURNS boolean > LANGUAGE plpgsql > AS $function$ > BEGIN > > IF EXISTS(SELECT 1 FROM pg_attribute > WHERE attrelid = schema_table_ > AND attname = old_name_ > AND NOT attisdropped) THEN > EXECUTE format ('ALTER TABLE %s RENAME %s TO %s', schema_table_, old_name_, 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 has already been renamed in the 9.6 branch the above existence test fails > and an exception is generated. Not clear to me when in process the function is run? What is the exception generated? > > It all seems to work correctly if I repeat this same patch in the 11.3 branch. > > 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 > > > > > > > > > > -- Adrian Klaver adrian.klaver@aklaver.com