Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X89qa-00005a-0U for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Jul 2014 15:13:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X89qY-00058l-UR for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Jul 2014 15:13:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X89qX-00058e-PP for pgsql-bugs@postgresql.org; Fri, 18 Jul 2014 15:13:17 +0000 Received: from mx01.clickware.de ([78.35.13.85]) by makus.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X89qT-0007aR-F7 for pgsql-bugs@postgresql.org; Fri, 18 Jul 2014 15:13:16 +0000 Received: (qmail 25643 invoked by uid 1010); 18 Jul 2014 15:13:07 -0000 Received: from localhost (marc@localhost) by mx01 (envelope-from , uid 1002) with qmail-scanner-2.06st (sophie: //. spamassassin: 3.2.3. Clear:RC:0(127.0.0.1):SA:0(-4.4/5.0):. Processed in 0.229532 secs); 18 Jul 2014 15:13:07 -0000 X-Spam-Status: No, hits=-4.4 required=5.0 Received: from localhost (HELO ?192.168.1.205?) (marc@127.0.0.1) by mx01.clickware.de with SMTP; 18 Jul 2014 15:13:07 -0000 Message-ID: <53C93986.80904@clickware.de> Date: Fri, 18 Jul 2014 17:13:10 +0200 From: Marc Schablewski User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: PG 9.3.x: Dropping a table column breaks restore of dump if table is used as return type of a function used in a view X-Enigmail-Version: 1.6 Content-Type: multipart/alternative; boundary="------------060903040907030302070409" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------060903040907030302070409 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Hi, we think we might have discovered a problem in the way dropped columns are handled in PostgreSQL 9.3 when using a table as a return type in a function that itself is used in a from clause of a view and dumping the database containing this view. As you can see in the example below, we created a function that returns a set of rows from a table. This function is used in a view that selects each row the function returns. PostgreSQL seems to generate additional column information for the result type of our function when parsing the view definition, which you can see when doing a \d on it. test=> \d+ testv ... View definition: SELECT t.sss1 FROM testt() t(sss1, sss2); Now, if you drop a column in the table and do a describe on the view again, the dropped column gets replaced by a dummy column "<>". test=> \d+ testv ... View definition: SELECT t.sss1 FROM testt() t(sss1, "<>"); This also occurs when you drop the column _before_ creating the function and view! You also cannot remove this dummy column by dropping and recreating the function or view. Up to this point, it's no big deal. You can still use the view as normal. The trouble starts, when you take a dump of that database. As expected, the structure of the table is dumped without the dropped column, but the view definition still contains the dummy column and will produce an error while restoring the dump, because the column count does not match. This happens in both the SQL and the custom dump format of pg_dump. I can't tell if this is a problem with dropped columns still being visible in certain circumstances or a problem in pg_dump which should ignore those columns, but doesn't. So far, the only solution we can think of is to drop and recreate the table without the dropped columns. We tested this on various versions of PostgreSQL (9.3.4, 9.3.2, 9.1.13 and 9.2.6), both on Windows 7 (64bit) and Linux (SuSE and Ubuntu), but only 9.3.x seems to be affected. The Linux versions where compiled from source. On Windows we used the one-click installer. In PostgreSQL 9.2 and older, the dummy column didn't appear in the generated column list to cause any trouble. Kind regards, Marc Schablewski Example script to reproduce the behaviour: create table test (sss1 varchar, sss2 varchar); alter table test drop column sss2; drop function if exists testt(); CREATE OR REPLACE FUNCTION testt() RETURNS setof test AS $body$ declare rec1 test%ROWTYPE; begin for rec1 in (select * from test) loop return next rec1; end loop; return; end; $body$ LANGUAGE 'plpgsql'; drop view if exists testv; create view testv as select t.* from testt() t; \d+ testv --------------060903040907030302070409 Content-Type: text/html; charset=ISO-8859-15 Content-Transfer-Encoding: 8bit Hi,

we think we might have discovered a problem in the way dropped columns are handled in PostgreSQL 9.3 when using a table as a return type in a function that itself is used in a from clause of a view and dumping the database containing this view.

As you can see in the example below, we created a function that returns a set of rows from a table. This function is used in a view that selects each row the function returns. PostgreSQL seems to generate additional column information for the result type of our function when parsing the view definition, which you can see when doing a \d on it.

test=> \d+ testv
...
View definition:
 SELECT t.sss1
   FROM testt() t(sss1, sss2);

Now, if you drop a column in the table and do a describe on the view again, the dropped column gets replaced by a dummy column "<>".

test=> \d+ testv
...
View definition:
 SELECT t.sss1
   FROM testt() t(sss1, "<>");

This also occurs when you drop the column _before_ creating the function and view! You also cannot remove this dummy column by dropping and recreating the function or view.

Up to this point, it's no big deal. You can still use the view as normal. The trouble starts, when you take a dump of that database. As expected, the structure of the table is dumped without the dropped column, but the view definition still contains the dummy column and will produce an error while restoring the dump, because the column count does not match. This happens in both the SQL and the custom dump format of pg_dump. I can't tell if this is a problem with dropped columns still being visible in certain circumstances or a problem in pg_dump which should ignore those columns, but doesn't.

So far, the only solution we can think of is to drop and recreate the table without the dropped columns.

We tested this on various versions of PostgreSQL (9.3.4, 9.3.2, 9.1.13 and 9.2.6), both on Windows 7 (64bit) and Linux (SuSE and Ubuntu), but only 9.3.x seems to be affected. The Linux versions where compiled from source. On Windows we used the one-click installer. In PostgreSQL 9.2 and older, the dummy column didn't appear in the generated column list to cause any trouble.

Kind regards,
    Marc Schablewski

Example script to reproduce the behaviour:

create table test (sss1 varchar, sss2 varchar);
alter table test drop column sss2;

drop function if exists testt();
CREATE OR REPLACE FUNCTION testt() RETURNS setof test AS
$body$
declare
    rec1 test%ROWTYPE;
begin
    for rec1 in (select * from test)
    loop
        return next rec1;
    end loop;

    return;
end;
$body$
LANGUAGE 'plpgsql';

drop view if exists testv;
create view testv as
select t.*
from testt() t;

\d+ testv



--------------060903040907030302070409--