Skip to content

Commit a36c84c

Browse files
committed
Fix default_tablespace usage for partitioned tables
In commit 8725958 I (Álvaro) tried to rationalize the determination of tablespace to use for partitioned tables, but failed to handle the default_tablespace case. Repair and add proper tests. Author: Amit Langote, Rushabh Lathia Reported-by: Rushabh Lathia Reviewed-by: Amit Langote, Álvaro Herrera Discussion: https://postgr.es/m/CAGPqQf0cYjm1=rjxk_6gU0SjUS70=yFUAdCJLwWzh9bhNJnyVg@mail.gmail.com
1 parent d826159 commit a36c84c

File tree

3 files changed

+73
-19
lines changed

3 files changed

+73
-19
lines changed

src/backend/commands/tablecmds.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
660660
}
661661

662662
/*
663-
* Select tablespace to use. If not specified, use default tablespace
664-
* (which may in turn default to database's default).
663+
* Select tablespace to use: an explicitly indicated one, or (in the case
664+
* of a partitioned table) the parent's, if it has one.
665665
*/
666666
if (stmt->tablespacename)
667667
{
@@ -682,6 +682,10 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
682682
tablespaceId = get_rel_tablespace(linitial_oid(inheritOids));
683683
}
684684
else
685+
tablespaceId = InvalidOid;
686+
687+
/* still nothing? use the default */
688+
if (!OidIsValid(tablespaceId))
685689
tablespaceId = GetDefaultTablespace(stmt->relation->relpersistence,
686690
partitioned);
687691

src/test/regress/input/tablespace.source

+28-6
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,38 @@ CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace;
4444
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
4545
where c.reltablespace = t.oid AND c.relname = 'foo_idx';
4646

47+
--
4748
-- partitioned table
49+
--
4850
CREATE TABLE testschema.part (a int) PARTITION BY LIST (a);
49-
CREATE TABLE testschema.part12 PARTITION OF testschema.part FOR VALUES IN(1,2) PARTITION BY LIST (a) TABLESPACE regress_tblspace;
50-
CREATE TABLE testschema.part12_1 PARTITION OF testschema.part12 FOR VALUES IN (1);
51-
ALTER TABLE testschema.part12 SET TABLESPACE pg_default;
52-
CREATE TABLE testschema.part12_2 PARTITION OF testschema.part12 FOR VALUES IN (2);
53-
-- Ensure part12_1 defaulted to regress_tblspace and part12_2 defaulted to pg_default.
51+
SET default_tablespace TO pg_global;
52+
CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
53+
RESET default_tablespace;
54+
CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
55+
SET default_tablespace TO regress_tblspace;
56+
CREATE TABLE testschema.part_2 PARTITION OF testschema.part FOR VALUES IN (2);
57+
SET default_tablespace TO pg_global;
58+
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
59+
ALTER TABLE testschema.part SET TABLESPACE regress_tblspace;
60+
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
61+
CREATE TABLE testschema.part_4 PARTITION OF testschema.part FOR VALUES IN (4)
62+
TABLESPACE pg_default;
63+
CREATE TABLE testschema.part_56 PARTITION OF testschema.part FOR VALUES IN (5, 6)
64+
PARTITION BY LIST (a);
65+
ALTER TABLE testschema.part SET TABLESPACE pg_default;
66+
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
67+
PARTITION BY LIST (a);
68+
CREATE TABLE testschema.part_910 PARTITION OF testschema.part FOR VALUES IN (9, 10)
69+
PARTITION BY LIST (a) TABLESPACE regress_tblspace;
70+
RESET default_tablespace;
71+
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
72+
PARTITION BY LIST (a);
73+
5474
SELECT relname, spcname FROM pg_catalog.pg_class c
75+
JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid)
5576
LEFT JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid
56-
where c.relname LIKE 'part%' order by relname;
77+
where c.relname LIKE 'part%' AND n.nspname = 'testschema' order by relname;
78+
RESET default_tablespace;
5779
DROP TABLE testschema.part;
5880

5981
-- partitioned index

src/test/regress/output/tablespace.source

+39-11
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,52 @@ SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
6161
foo_idx | regress_tblspace
6262
(1 row)
6363

64+
--
6465
-- partitioned table
66+
--
6567
CREATE TABLE testschema.part (a int) PARTITION BY LIST (a);
66-
CREATE TABLE testschema.part12 PARTITION OF testschema.part FOR VALUES IN(1,2) PARTITION BY LIST (a) TABLESPACE regress_tblspace;
67-
CREATE TABLE testschema.part12_1 PARTITION OF testschema.part12 FOR VALUES IN (1);
68-
ALTER TABLE testschema.part12 SET TABLESPACE pg_default;
69-
CREATE TABLE testschema.part12_2 PARTITION OF testschema.part12 FOR VALUES IN (2);
70-
-- Ensure part12_1 defaulted to regress_tblspace and part12_2 defaulted to pg_default.
68+
SET default_tablespace TO pg_global;
69+
CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
70+
ERROR: only shared relations can be placed in pg_global tablespace
71+
RESET default_tablespace;
72+
CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
73+
SET default_tablespace TO regress_tblspace;
74+
CREATE TABLE testschema.part_2 PARTITION OF testschema.part FOR VALUES IN (2);
75+
SET default_tablespace TO pg_global;
76+
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
77+
ERROR: only shared relations can be placed in pg_global tablespace
78+
ALTER TABLE testschema.part SET TABLESPACE regress_tblspace;
79+
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
80+
CREATE TABLE testschema.part_4 PARTITION OF testschema.part FOR VALUES IN (4)
81+
TABLESPACE pg_default;
82+
CREATE TABLE testschema.part_56 PARTITION OF testschema.part FOR VALUES IN (5, 6)
83+
PARTITION BY LIST (a);
84+
ALTER TABLE testschema.part SET TABLESPACE pg_default;
85+
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
86+
PARTITION BY LIST (a);
87+
ERROR: only shared relations can be placed in pg_global tablespace
88+
CREATE TABLE testschema.part_910 PARTITION OF testschema.part FOR VALUES IN (9, 10)
89+
PARTITION BY LIST (a) TABLESPACE regress_tblspace;
90+
RESET default_tablespace;
91+
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
92+
PARTITION BY LIST (a);
7193
SELECT relname, spcname FROM pg_catalog.pg_class c
94+
JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid)
7295
LEFT JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid
73-
where c.relname LIKE 'part%' order by relname;
96+
where c.relname LIKE 'part%' AND n.nspname = 'testschema' order by relname;
7497
relname | spcname
7598
----------+------------------
7699
part |
77-
part12 |
78-
part12_1 | regress_tblspace
79-
part12_2 |
80-
(4 rows)
81-
100+
part_1 |
101+
part_2 | regress_tblspace
102+
part_3 | regress_tblspace
103+
part_4 |
104+
part_56 | regress_tblspace
105+
part_78 |
106+
part_910 | regress_tblspace
107+
(8 rows)
108+
109+
RESET default_tablespace;
82110
DROP TABLE testschema.part;
83111
-- partitioned index
84112
CREATE TABLE testschema.part (a int) PARTITION BY LIST (a);

0 commit comments

Comments
 (0)