From: Tom Lane <tgl@sss.pgh.pa.us>
To: Peter J. Holzer <hjp-pgsql@hjp.at>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Tablespace column value null on select * from pg_tables
Date: Sun, 28 Jul 2019 16:43:53 -0400
Message-ID: <22007.1564346633@sss.pgh.pa.us> (raw)
In-Reply-To: <20190728202439.GB25242@hjp.at>
References: <HF3MoPAEJHO5WGoMH1JsvhqzV3P48GQ8vOwKhad1xCLbC5-SOgGYDSmEOJFtFNARBPreyEGiuWKaI3pU2j8n7NG8kMDS41elKfD_qxB-AxY=@protonmail.com>
<a61ac18f-df1b-1bb5-ca3c-346bc8faf464@aklaver.com>
<KTdw5h9qQtntMgkqVtOrCx78JzBxXVMj0to5vT5B_yYZawT3I4vJ5dorf_gyFfreEps-6VspOhn-__EwrV_dD5-3yhhe0kIC5EtiWtknuXw=@protonmail.com>
<6b86387c-1076-8c0e-564b-cd76d34c3132@aklaver.com>
<98-GCV7Md7B0jENT-tVKpjarGkevVihM4OUhRpEbGUov3f2BKUDUpQR1725CegOBxE3oVEahOf1Ty0DR6anUAqbvIfI2FavO1LIRfpzyReg=@protonmail.com>
<20190728202439.GB25242@hjp.at>
"Peter J. Holzer" <hjp-pgsql@hjp.at> writes:
> On 2019-07-16 19:20:24 +0000, Alex Williams wrote:
>> 1. postgres=# ALTER DATABASE mydatabase SET TABLESPACE pg_default;
>> ERROR: some relations of database "mydatabase" are already in tablespace "pg_default"
>> HINT: You must move them back to the database's default tablespace before using this command.
> What's the reason for this error? Wouldn't it be simpler to leave
> relations alone which are already in the correct tablespace?
It'd be simpler, it'd also be wrong.
The difficulty here is that ALTER DATABASE executes from a different
database, so it can't examine or change pg_class.reltablespace, so
it can't fix values that it would make inconsistent.
So imagine that we have a database in tablespace X, containing one
table T1 in tablespace Y. All the other entries in that database's
reltablespace column are zero signifying "it's in the database's default
tablespace". The one reference to tablespace X is in the shared catalog
pg_database, where we can see and change it during ALTER DATABASE.
Now let's do ALTER DATABASE ... SET TABLESPACE Y, and suppose that the
error check is not there. We move everything in X into Y, and we update
pg_database.dattablespace, but we don't change the reltablespace column.
Things are not actually broken at this point; T1's reltablespace says
Y and indeed that's where it is.
Now let's do ALTER DATABASE ... SET TABLESPACE Z. From outside the
database, all we can see is that all its files are physically in Y,
so we move them all to Z. Including T1. But its reltablespace entry
still says Y, so now things *are* broken.
(Another small issue here is that table relfilenodes are only
guaranteed unique per tablespace per database, so that there is
a hazard of OID collision between T1 and some other table being
moved into tablespace Y. We could fix that, but only if we
can change pg_class.relfilenode...)
So this can't be fixed without having a way to access and change
catalogs from a different database, which is a large can of worms
we don't want to open for such a small feature.
regards, tom lane
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: tgl@sss.pgh.pa.us, hjp-pgsql@hjp.at
Subject: Re: Tablespace column value null on select * from pg_tables
In-Reply-To: <22007.1564346633@sss.pgh.pa.us>
References: <HF3MoPAEJHO5WGoMH1JsvhqzV3P48GQ8vOwKhad1xCLbC5-SOgGYDSmEOJFtFNARBPreyEGiuWKaI3pU2j8n7NG8kMDS41elKfD_qxB-AxY=@protonmail.com> <a61ac18f-df1b-1bb5-ca3c-346bc8faf464@aklaver.com> <KTdw5h9qQtntMgkqVtOrCx78JzBxXVMj0to5vT5B_yYZawT3I4vJ5dorf_gyFfreEps-6VspOhn-__EwrV_dD5-3yhhe0kIC5EtiWtknuXw=@protonmail.com> <6b86387c-1076-8c0e-564b-cd76d34c3132@aklaver.com> <98-GCV7Md7B0jENT-tVKpjarGkevVihM4OUhRpEbGUov3f2BKUDUpQR1725CegOBxE3oVEahOf1Ty0DR6anUAqbvIfI2FavO1LIRfpzyReg=@protonmail.com> <20190728202439.GB25242@hjp.at> <22007.1564346633@sss.pgh.pa.us>
* 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