Commit a36c84c3 authored by Alvaro Herrera's avatar Alvaro Herrera

Fix default_tablespace usage for partitioned tables

In commit 87259588 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
parent d8261595
...@@ -660,8 +660,8 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ...@@ -660,8 +660,8 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
} }
/* /*
* Select tablespace to use. If not specified, use default tablespace * Select tablespace to use: an explicitly indicated one, or (in the case
* (which may in turn default to database's default). * of a partitioned table) the parent's, if it has one.
*/ */
if (stmt->tablespacename) if (stmt->tablespacename)
{ {
...@@ -682,6 +682,10 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, ...@@ -682,6 +682,10 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
tablespaceId = get_rel_tablespace(linitial_oid(inheritOids)); tablespaceId = get_rel_tablespace(linitial_oid(inheritOids));
} }
else else
tablespaceId = InvalidOid;
/* still nothing? use the default */
if (!OidIsValid(tablespaceId))
tablespaceId = GetDefaultTablespace(stmt->relation->relpersistence, tablespaceId = GetDefaultTablespace(stmt->relation->relpersistence,
partitioned); partitioned);
......
...@@ -44,16 +44,38 @@ CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace; ...@@ -44,16 +44,38 @@ CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace;
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
where c.reltablespace = t.oid AND c.relname = 'foo_idx'; where c.reltablespace = t.oid AND c.relname = 'foo_idx';
--
-- partitioned table -- partitioned table
--
CREATE TABLE testschema.part (a int) PARTITION BY LIST (a); CREATE TABLE testschema.part (a int) PARTITION BY LIST (a);
CREATE TABLE testschema.part12 PARTITION OF testschema.part FOR VALUES IN(1,2) PARTITION BY LIST (a) TABLESPACE regress_tblspace; SET default_tablespace TO pg_global;
CREATE TABLE testschema.part12_1 PARTITION OF testschema.part12 FOR VALUES IN (1); CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
ALTER TABLE testschema.part12 SET TABLESPACE pg_default; RESET default_tablespace;
CREATE TABLE testschema.part12_2 PARTITION OF testschema.part12 FOR VALUES IN (2); CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
-- Ensure part12_1 defaulted to regress_tblspace and part12_2 defaulted to pg_default. SET default_tablespace TO regress_tblspace;
CREATE TABLE testschema.part_2 PARTITION OF testschema.part FOR VALUES IN (2);
SET default_tablespace TO pg_global;
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
ALTER TABLE testschema.part SET TABLESPACE regress_tblspace;
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
CREATE TABLE testschema.part_4 PARTITION OF testschema.part FOR VALUES IN (4)
TABLESPACE pg_default;
CREATE TABLE testschema.part_56 PARTITION OF testschema.part FOR VALUES IN (5, 6)
PARTITION BY LIST (a);
ALTER TABLE testschema.part SET TABLESPACE pg_default;
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
PARTITION BY LIST (a);
CREATE TABLE testschema.part_910 PARTITION OF testschema.part FOR VALUES IN (9, 10)
PARTITION BY LIST (a) TABLESPACE regress_tblspace;
RESET default_tablespace;
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
PARTITION BY LIST (a);
SELECT relname, spcname FROM pg_catalog.pg_class c SELECT relname, spcname FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid)
LEFT JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid LEFT JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid
where c.relname LIKE 'part%' order by relname; where c.relname LIKE 'part%' AND n.nspname = 'testschema' order by relname;
RESET default_tablespace;
DROP TABLE testschema.part; DROP TABLE testschema.part;
-- partitioned index -- partitioned index
......
...@@ -61,24 +61,52 @@ SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c ...@@ -61,24 +61,52 @@ SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
foo_idx | regress_tblspace foo_idx | regress_tblspace
(1 row) (1 row)
--
-- partitioned table -- partitioned table
--
CREATE TABLE testschema.part (a int) PARTITION BY LIST (a); CREATE TABLE testschema.part (a int) PARTITION BY LIST (a);
CREATE TABLE testschema.part12 PARTITION OF testschema.part FOR VALUES IN(1,2) PARTITION BY LIST (a) TABLESPACE regress_tblspace; SET default_tablespace TO pg_global;
CREATE TABLE testschema.part12_1 PARTITION OF testschema.part12 FOR VALUES IN (1); CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
ALTER TABLE testschema.part12 SET TABLESPACE pg_default; ERROR: only shared relations can be placed in pg_global tablespace
CREATE TABLE testschema.part12_2 PARTITION OF testschema.part12 FOR VALUES IN (2); RESET default_tablespace;
-- Ensure part12_1 defaulted to regress_tblspace and part12_2 defaulted to pg_default. CREATE TABLE testschema.part_1 PARTITION OF testschema.part FOR VALUES IN (1);
SET default_tablespace TO regress_tblspace;
CREATE TABLE testschema.part_2 PARTITION OF testschema.part FOR VALUES IN (2);
SET default_tablespace TO pg_global;
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
ERROR: only shared relations can be placed in pg_global tablespace
ALTER TABLE testschema.part SET TABLESPACE regress_tblspace;
CREATE TABLE testschema.part_3 PARTITION OF testschema.part FOR VALUES IN (3);
CREATE TABLE testschema.part_4 PARTITION OF testschema.part FOR VALUES IN (4)
TABLESPACE pg_default;
CREATE TABLE testschema.part_56 PARTITION OF testschema.part FOR VALUES IN (5, 6)
PARTITION BY LIST (a);
ALTER TABLE testschema.part SET TABLESPACE pg_default;
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
PARTITION BY LIST (a);
ERROR: only shared relations can be placed in pg_global tablespace
CREATE TABLE testschema.part_910 PARTITION OF testschema.part FOR VALUES IN (9, 10)
PARTITION BY LIST (a) TABLESPACE regress_tblspace;
RESET default_tablespace;
CREATE TABLE testschema.part_78 PARTITION OF testschema.part FOR VALUES IN (7, 8)
PARTITION BY LIST (a);
SELECT relname, spcname FROM pg_catalog.pg_class c SELECT relname, spcname FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid)
LEFT JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid LEFT JOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid
where c.relname LIKE 'part%' order by relname; where c.relname LIKE 'part%' AND n.nspname = 'testschema' order by relname;
relname | spcname relname | spcname
----------+------------------ ----------+------------------
part | part |
part12 | part_1 |
part12_1 | regress_tblspace part_2 | regress_tblspace
part12_2 | part_3 | regress_tblspace
(4 rows) part_4 |
part_56 | regress_tblspace
part_78 |
part_910 | regress_tblspace
(8 rows)
RESET default_tablespace;
DROP TABLE testschema.part; DROP TABLE testschema.part;
-- partitioned index -- partitioned index
CREATE TABLE testschema.part (a int) PARTITION BY LIST (a); CREATE TABLE testschema.part (a int) PARTITION BY LIST (a);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment