BUG #18167: cannot create partitioned tables when default_tablespace is set

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: mariusraicu(at)laposte(dot)net
Subject: BUG #18167: cannot create partitioned tables when default_tablespace is set
Date: 2023-10-24 09:42:28
Message-ID: 18167-fcc9f9f33cddf04d@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

The following bug has been logged on the website:

Bug reference: 18167
Logged by: Marius Raicu
Email address: mariusraicu(at)laposte(dot)net
PostgreSQL version: 16.0
Operating system: RedHat 8
Description:

Hello all,

I am encountering some problems when creating partitioned tables when
default_tablespace parameter is set.

I am not sure if it is a bug or maybe I don't understand the documentation
correctly. In the doc, it is stated:
https://www.postgresql.org/docs/16/sql-createtable.html
TABLESPACE tablespace_name
The tablespace_name is the name of the tablespace in which the new table is
to be created. If not specified, default_tablespace is consulted, or
temp_tablespaces if the table is temporary. For partitioned tables, since no
storage is required for the table itself, the tablespace specified overrides
default_tablespace as the default tablespace to use for any newly created
partitions when no other tablespace is explicitly specified.

USING INDEX TABLESPACE tablespace_name
This clause allows selection of the tablespace in which the index associated
with a UNIQUE, PRIMARY KEY, or EXCLUDE constraint will be created. If not
specified, default_tablespace is consulted, or temp_tablespaces if the table
is temporary.

https://www.postgresql.org/docs/16/runtime-config-client.html#GUC-DEFAULT-TABLESPACE
default_tablespace (string)
This variable specifies the default tablespace in which to create objects
(tables and indexes) when a CREATE command does not explicitly specify a
tablespace.

The value is either the name of a tablespace, or an empty string to specify
using the default tablespace of the current database. If the value does not
match the name of any existing tablespace, PostgreSQL will automatically use
the default tablespace of the current database. If a nondefault tablespace
is specified, the user must have CREATE privilege for it, or creation
attempts will fail.

This variable is not used for temporary tables; for them, temp_tablespaces
is consulted instead.

This variable is also not used when creating databases. By default, a new
database inherits its tablespace setting from the template database it is
copied from.

If this parameter is set to a value other than the empty string when a
partitioned table is created, the partitioned table's tablespace will be set
to that value, which will be used as the default tablespace for partitions
created in the future, even if default_tablespace has changed since then.

See the sequence below:

[marius(at)mylaptop ~]$ psql
psql (17devel)
Type "help" for help.

marius(at)[local]:5434/postgres=# show default_tablespace;
default_tablespace
--------------------

(1 row)

marius(at)[local]:5434/postgres=# create table toto(id numeric) partition by
list(id);
CREATE TABLE
marius(at)[local]:5434/postgres=# drop table toto;
DROP TABLE
marius(at)[local]:5434/postgres=# \! mkdir /home/marius/pgcode/tblspc1
marius(at)[local]:5434/postgres=# \! ls /home/marius/pgcode
bin pgdata postgresql tblspc1
marius(at)[local]:5434/postgres=# \q
[marius(at)mylaptop ~]$ vi $PGDATA/postgresql.conf
[marius(at)mylaptop ~]$
[marius(at)mylaptop ~]$ pg_ctl restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2023-10-24 11:14:21.636 CEST [5800] LOG:
redirecting log output to logging collector process
2023-10-24 11:14:21.636 CEST [5800] HINT: Future log output will appear in
directory "log".
done
server started
[marius(at)mylaptop ~]$ psql
psql (17devel)
Type "help" for help.

marius(at)[local]:5434/postgres=# show default_tablespace;
default_tablespace
--------------------
tblspc1
(1 row)

marius(at)[local]:5434/postgres=# create tablespace tblspc1 location
'/home/marius/pgcode/tblspc1';
CREATE TABLESPACE
marius(at)[local]:5434/postgres=# create database test tablespace tblspc1;
CREATE DATABASE
marius(at)[local]:5434/postgres=# \c test
You are now connected to database "test" as user "marius".
marius(at)[local]:5434/test=# create table toto(id numeric) partition by
list(id);
ERROR: cannot specify default tablespace for partitioned relations
marius(at)[local]:5434/test=# create table toto(id numeric, constraint pk_id
primary key(id) using index tablespace tblspc1) partition by list(id);
ERROR: cannot specify default tablespace for partitioned relations

marius(at)[local]:5434/postgres=# \c test
You are now connected to database "test" as user "marius".
marius(at)[local]:5434/test=# create table toto2(id numeric, constraint pk_id
primary key(id) using index tablespace tblspc1) partition by list(id);
ERROR: cannot specify default tablespace for partitioned relations
marius(at)[local]:5434/test=# create table toto(id numeric) partition by
list(id) tablespace tblspc1;
ERROR: cannot specify default tablespace for partitioned relations
marius(at)[local]:5434/test=# create table toto(id numeric) partition by
list(id);
ERROR: cannot specify default tablespace for partitioned relations
marius(at)[local]:5434/test=# create table toto2(id numeric, constraint pk_id
primary key(id)) partition by list(id);
ERROR: cannot specify default tablespace for partitioned relations

However, in another database, 'postgres' by example, which was created in
the default tablespace '' (no tablespace at all), it works:

marius(at)[local]:5434/postgres=# create table toto(id numeric) partition by
list(id) tablespace tblspc1;
CREATE TABLE
marius(at)[local]:5434/postgres=# create table toto2(id numeric, constraint
pk_id primary key(id) using index tablespace tblspc1) partition by
list(id);
CREATE TABLE

I was able to reproduce this behavior on all versions starting to PG12.
So, when the default _tablespace is set, you have to specify the tablespace
clause to CREATE TABLE, despite the fact that the database where you try to
put the table is created into a tablespace.

Thanks,
Marius Raicu

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Amit Langote 2023-10-24 09:48:06 Re: AW: AW: BUG #18147: ERROR: invalid perminfoindex 0 in RTE with relid xxxxx
Previous Message Hans Buschmann 2023-10-24 06:16:13 AW: AW: AW: BUG #18147: ERROR: invalid perminfoindex 0 in RTE with relid xxxxx

Browse pgsql-hackers by date

  From Date Subject
Next Message Alena Rybakina 2023-10-24 10:00:13 Re: Simplify create_merge_append_path a bit for clarity
Previous Message Laurenz Albe 2023-10-24 08:35:32 Bug: RLS policy FOR SELECT is used to check new rows