Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d5xq6-00082Z-Pl for pgsql-general@arkaria.postgresql.org; Wed, 03 May 2017 17:13:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d5xq6-0000SO-7d for pgsql-general@arkaria.postgresql.org; Wed, 03 May 2017 17:13:22 +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 1d5xnf-0003of-7a for pgsql-general@postgresql.org; Wed, 03 May 2017 17:10:51 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d5xnb-0002RU-Mo for pgsql-general@postgresql.org; Wed, 03 May 2017 17:10:50 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 61E4720A6E; Wed, 3 May 2017 13:10:45 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Wed, 03 May 2017 13:10:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=fm1; bh=Y3cyORewGlShoZ9N86 6pf25lo/JZ9quARKy1TY+DQa8=; b=nS7rnDd1cx9WgUdeqdIwiFPI1KwDmkCHZz R1hB4WJ0XgMjWYA/Eltk/gcefYmy9HxjvG5T5Zf+GQnWG8DubCkCSjF7BYap1EUU IiLyqJ2/IuyfpMhyS34nBZh/DPdfhZTT8UU9/z+wrzYIG+nhGIuhjxG0LxIfYBAv M/ImN5sEF43GUew2AY5qffQHfpJ53xbKqgSF6E9PlfQqYwsOj3Rfl1JiBzHpuPOW lBuKFr1TNDJiub1HHsqZM55z0wTJmlNYp66xXI4dzYYj0bdoZEONETKE0OprdPO+ Rp8FQP6GjNfD1lyIVbwfbf46p01XPvih1SnKFnZC9HwzQVDoG6ug== 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-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s= fm1; bh=Y3cyORewGlShoZ9N866pf25lo/JZ9quARKy1TY+DQa8=; b=mlL2CoCB 00qbcZcDRCv5uzgo25a321RofgDT/paeDGQPWxYMhkCJbSMDCWAgxm4jIkiEHmls ZiLet6E/o2p+1+uEN2HsagHPzzbJbR5S8Estl5ApZhNjbWKeSMCAvoriqnTAgbuC zaHCdWW5/ax/uEDuvBYD+f68MWNAFZAJsYj7NLCY8dWx+3xXr0Z9AvcHZCGC4iZh 4ao0mj3HMurzSEw4cbwJhWl6MHaeX/nusehU0ufDoFwrwlZXpwnI0wKq60HdVc/v qog7IEnZhpUq3XkadVbYvxLo8Xel1g2Dp9hMkg6q6ZRaxl/PKxQFJnRAbaf1B8Pl XMY0gPrxeE8ERQ== X-ME-Sender: X-Sasl-enc: i53aiyWXixspkA5ZfICkWytD5vrry4QYsPzTTsuPUPJ7 1493831445 Received: from ranger.suse (unknown [74.94.73.222]) by mail.messagingengine.com (Postfix) with ESMTPA id E2D207E316; Wed, 3 May 2017 13:10:44 -0400 (EDT) Subject: Re: Column rename in an extension update script To: Philippe BEAUDOIN , pgsql-general@postgresql.org References: <2cf7941e-4e41-7714-3de8-37b1a8f74dff@free.fr> From: Adrian Klaver Message-ID: <331e2430-9c74-ebeb-6e6c-e2257058bf92@aklaver.com> Date: Wed, 3 May 2017 10:10:44 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <2cf7941e-4e41-7714-3de8-37b1a8f74dff@free.fr> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org On 04/30/2017 11:54 PM, Philippe BEAUDOIN wrote: > Hi all, > > I am coding an update script for an extension. And I am in trouble when > trying to rename a column of an existing table. > > Just after the ALTER TABLE statement, I want to access this table. But > at this time, the altered column is not visible with its new name. > > From the error it looks to me like the statements are each run in a separate session and the UPDATE is not seeing the ALTER TABLE. A quick search of the source indicates this is handled in extension.c: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/commands/extension.c;h=33b0de0a7657298729ad5c3b185dc2f4aab0bb73;hb=6a18e4bc2d13d077c52cf90a4c6ec68343808ba7 In particular execute_sql_string line 684. I do not understand C well enough to figure out if the above is actually creating separate sessions or not. Maybe you understand it or someone else can chime in. > > # issue in postgres extension when trying to access a column that has > been renamed inside an extension update script > # > export EXTDIR="/tmp" > export PGDIR="/usr/local/pg962/share/postgresql/extension" > export PGHOST=localhost > export PGPORT=5496 > export PGDATABASE='postgres' > > echo "create files for the extension" > echo "------------------------------" > cat >$EXTDIR/myextension.control <<*END* > default_version = '1' > directory = '$EXTDIR' > *END* > sudo ln -s $EXTDIR/myextension.control $PGDIR/myextension.control > > cat >$EXTDIR/myextension--1.sql <<*END* > CREATE TABLE mytable (col_old INT); > *END* > > cat >$EXTDIR/myextension--1--2.sql <<*END* > ALTER TABLE mytable RENAME col_old TO col_new; > UPDATE mytable SET col_new = 0; > *END* > > echo "psql: run the test ==> FAILS" > echo "----------------------------" > psql -a <<*END* > select version(); > CREATE EXTENSION myextension VERSION '1'; > ALTER EXTENSION myextension UPDATE TO '2'; > DROP EXTENSION IF EXISTS myextension; > *END* > > echo "psql: similar statements outside extension ==> WORKS" > echo "----------------------------------------------------" > psql -a <<*END* > CREATE TABLE mytable (col_old INT); > BEGIN; > ALTER TABLE mytable RENAME col_old TO col_new; > UPDATE mytable SET col_new = 0; > COMMIT; > DROP TABLE IF EXISTS mytable; > *END* > > sudo rm $PGDIR/myextension.control > rm $EXTDIR/myextension* > > And here is the result: > > create files for the extension > ------------------------------ > psql: run the test ==> FAILS > ---------------------------- > select version(); > > version > ----------------------------------------------------------------------------------------------------------------- > PostgreSQL 9.6.2 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu > 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609, 64-bit > (1 row) > > CREATE EXTENSION myextension VERSION '1'; > CREATE EXTENSION > ALTER EXTENSION myextension UPDATE TO '2'; > ERROR: column "col_new" of relation "mytable" does not exist > DROP EXTENSION IF EXISTS myextension; > DROP EXTENSION > psql: similar statements outside extension ==> WORKS > ---------------------------------------------------- > CREATE TABLE mytable (col_old INT); > CREATE TABLE > BEGIN; > BEGIN > ALTER TABLE mytable RENAME col_old TO col_new; > ALTER TABLE > UPDATE mytable SET col_new = 0; > UPDATE 0 > COMMIT; > COMMIT > DROP TABLE IF EXISTS mytable; > DROP TABLE > > As you can see: > > - the error message is "ERROR: column "col_new" of relation "mytable" > does not exist", while the ALTER TABLE statement doesn't return any error, > > - the same statements in a simple psql script works fine, > > - I reproduce this with all supported postgres versions. > > As a workaround, I perform the UPDATE statement before the ALTER TABLE > operation, using of course the old column name. > > I probably do something wrong. But I can't see what. > > Thanks by advance for any piece of advise. > > Best regards. Philippe Beaudoin. > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general