pg_upgrade incorrectly equates pg_default and database tablespace

Lists: pgsql-hackers
From: Ants Aasma <ants(at)cybertec(dot)at>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: pg_upgrade incorrectly equates pg_default and database tablespace
Date: 2012-03-22 12:55:32
Message-ID: CA+CSw_vDb2nmPhumR4QzOre=ai+ktrJ8KwaZQv7M0nU9L5A9sw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

while working on a support case I stumbled upon a bug in pg_upgrade.
Upgrade fails with "No such file or directory" when a database is
moved to a non-default tablespace and contains a table that is moved
to pg_default. The cause seems to be that the following test
incorrectly equates empty spclocation with database tablespace:

tblspace = PQgetvalue(res, relnum, i_spclocation);
/* if no table tablespace, use the database tablespace */
if (strlen(tblspace) == 0)
tblspace = dbinfo->db_tblspace;

Patch to fix this is attached.

Regards,
Ants Aasma
--
Cybertec Schönig & Schönig GmbH
Gröhrmühlgasse 26
A-2700 Wiener Neustadt
Web: http://www.postgresql-support.de

Attachment Content-Type Size
pgupgrade-default-tblspc.patch text/x-patch 2.0 KB

From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Ants Aasma <ants(at)cybertec(dot)at>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Bruce Momjian <bruce(at)momjian(dot)us>
Subject: Re: pg_upgrade incorrectly equates pg_default and database tablespace
Date: 2012-03-30 20:11:47
Message-ID: 1333138307.28198.14.camel@sussancws0025
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, 2012-03-22 at 14:55 +0200, Ants Aasma wrote:
> Hi,
>
> while working on a support case I stumbled upon a bug in pg_upgrade.
> Upgrade fails with "No such file or directory" when a database is
> moved to a non-default tablespace and contains a table that is moved
> to pg_default. The cause seems to be that the following test
> incorrectly equates empty spclocation with database tablespace:
>
> tblspace = PQgetvalue(res, relnum, i_spclocation);
> /* if no table tablespace, use the database tablespace */
> if (strlen(tblspace) == 0)
> tblspace = dbinfo->db_tblspace;
>
> Patch to fix this is attached.

I confirmed this bug upgrading 9.1 to master, and that this patch fixes
it. Thank you for the report!

Patch looks good to me as well, with one very minor nitpick: the added
comment is missing an apostrophe.

Bruce, can you take a look at this?

Regards,
Jeff Davis


From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Ants Aasma <ants(at)cybertec(dot)at>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Bruce Momjian <bruce(at)momjian(dot)us>
Subject: Re: pg_upgrade incorrectly equates pg_default and database tablespace
Date: 2012-04-07 03:26:30
Message-ID: 1333769190.10414.25.camel@jdavis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, 2012-03-30 at 13:11 -0700, Jeff Davis wrote:
> I confirmed this bug upgrading 9.1 to master, and that this patch fixes
> it. Thank you for the report!
>
> Patch looks good to me as well, with one very minor nitpick: the added
> comment is missing an apostrophe.
>
> Bruce, can you take a look at this?

Adding this to the next commitfest, just so it doesn't get forgotten.

Regards,
Jeff Davis


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Ants Aasma <ants(at)cybertec(dot)at>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: pg_upgrade incorrectly equates pg_default and database tablespace
Date: 2012-04-11 00:05:22
Message-ID: 20120411000522.GJ3379@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Mar 22, 2012 at 02:55:32PM +0200, Ants Aasma wrote:
> Hi,
>
> while working on a support case I stumbled upon a bug in pg_upgrade.
> Upgrade fails with "No such file or directory" when a database is
> moved to a non-default tablespace and contains a table that is moved
> to pg_default. The cause seems to be that the following test
> incorrectly equates empty spclocation with database tablespace:
>
> tblspace = PQgetvalue(res, relnum, i_spclocation);
> /* if no table tablespace, use the database tablespace */
> if (strlen(tblspace) == 0)
> tblspace = dbinfo->db_tblspace;
>
> Patch to fix this is attached.

Thank you for the fine bug report, and patch (and the bug confirmation
from Jeff Davis). Sorry for the delay in replying.

You have certainly found a bug, and one that exists all the way back to
pg_upgrade 9.0. I was able to reproduce the bug with this SQL:

-- test database in different tablespace with table in cluster
-- default tablespace
CREATE DATABASE tbltest TABLESPACE tt;
\connect tbltest
CREATE TABLE t1 (x int);
CREATE TABLE t2 (x int) TABLESPACE pg_default;

It is exactly as you described --- the database is in a user-defined
tablespace, but the table (t2) is in the cluster default location. Not
sure how no one else reported this failure before.

The crux of the confusion is that pg_class.reltablespace == 0 means the
database default tablespace, while a join to pg_tablespace that returns
a zero-length string means it is in the cluster data directory. The new
code properly looks at reltablespace rather than testing the tablespace
location, which was your fix as well.

I have applied three different patches very similar to your helpful
suggestion, attached.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

Attachment Content-Type Size
9.0 text/plain 3.9 KB
9.1 text/plain 3.1 KB
9.2 text/plain 3.1 KB