From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Day, David <david.day@redcom.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: Rename a column if not already renamed.?
Date: Tue, 20 Aug 2019 12:16:24 -0700
Message-ID: <80e512f9-b6c9-c9b0-24db-b3662ff67a94@aklaver.com> (raw)
In-Reply-To: <ef59a820008b4d3299428db52ecc32e0@redcom.com>
References: <ef59a820008b4d3299428db52ecc32e0@redcom.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, david.day@redcom.com
Subject: Re: Rename a column if not already renamed.?
In-Reply-To: <80e512f9-b6c9-c9b0-24db-b3662ff67a94@aklaver.com>
References: <ef59a820008b4d3299428db52ecc32e0@redcom.com> <80e512f9-b6c9-c9b0-24db-b3662ff67a94@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox