-- relative tablespace locations are not allowed CREATE TABLESPACE regress_tblspace LOCATION 'relative'; -- fail
-- empty tablespace locations are not usually allowed CREATE TABLESPACE regress_tblspace LOCATION ''; -- fail
-- as a special developer-only option to allow us to use tablespaces -- with streaming replication on the same server, an empty location -- can be allowed as a way to say that the tablespace should be created -- as a directory in pg_tblspc, rather than being a symlink SET allow_in_place_tablespaces = true;
-- create a tablespace using WITH clause CREATE TABLESPACE regress_tblspacewith LOCATION ''WITH (some_nonexistent_parameter = true); -- fail CREATE TABLESPACE regress_tblspacewith LOCATION ''WITH (random_page_cost = 3.0); -- ok
-- check to see the parameter was used SELECT spcoptions FROM pg_tablespace WHERE spcname = 'regress_tblspacewith';
-- drop the tablespace so we can re-use the location DROP TABLESPACE regress_tblspacewith;
-- This returns a relative path as of an effect of allow_in_place_tablespaces, -- masking the tablespace OID used in the path name. SELECT regexp_replace(pg_tablespace_location(oid), '(pg_tblspc)/(\d+)', '\1/NNN') FROM pg_tablespace WHERE spcname = 'regress_tblspace';
-- try setting and resetting some properties for the new tablespace ALTER TABLESPACE regress_tblspace SET (random_page_cost = 1.0, seq_page_cost = 1.1); ALTER TABLESPACE regress_tblspace SET (some_nonexistent_parameter = true); -- fail ALTER TABLESPACE regress_tblspace RESET (random_page_cost = 2.0); -- fail ALTER TABLESPACE regress_tblspace RESET (random_page_cost, effective_io_concurrency); -- ok
-- table with toast relation CREATETABLE regress_tblspace_test_tbl (num1 bigint, num2 doubleprecision, t text); INSERTINTO regress_tblspace_test_tbl (num1, num2, t) SELECT round(random()*100), random(), 'text' FROM generate_series(1, 10) s(i); CREATEINDEX regress_tblspace_test_tbl_idx ON regress_tblspace_test_tbl (num1); -- move to global tablespace, fail
REINDEX (TABLESPACE pg_global) INDEX regress_tblspace_test_tbl_idx;
REINDEX (TABLESPACE pg_global) INDEX CONCURRENTLY regress_tblspace_test_tbl_idx;
-- check transactional behavior of REINDEX (TABLESPACE)
BEGIN;
REINDEX (TABLESPACE regress_tblspace) INDEX regress_tblspace_test_tbl_idx;
REINDEX (TABLESPACE regress_tblspace) TABLE regress_tblspace_test_tbl;
ROLLBACK; -- no relation moved to the new tablespace SELECT c.relname FROM pg_class c, pg_tablespace s WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace';
-- check that all indexes are moved to a new tablespace with different -- relfilenode. -- Save first the existing relfilenode for the toast and main relations. SELECT relfilenode as main_filenode FROM pg_class WHERE relname = 'regress_tblspace_test_tbl_idx' \gset SELECT relfilenode as toast_filenode FROM pg_class WHERE oid =
(SELECT i.indexrelid FROM pg_class c,
pg_index i WHERE i.indrelid = c.reltoastrelid AND
c.relname = 'regress_tblspace_test_tbl') \gset
REINDEX (TABLESPACE regress_tblspace) TABLE regress_tblspace_test_tbl; SELECT c.relname FROM pg_class c, pg_tablespace s WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace' ORDERBY c.relname; ALTERTABLE regress_tblspace_test_tbl SET TABLESPACE regress_tblspace; ALTERTABLE regress_tblspace_test_tbl SET TABLESPACE pg_default; SELECT c.relname FROM pg_class c, pg_tablespace s WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace' ORDERBY c.relname; -- Move back to the default tablespace. ALTERINDEX regress_tblspace_test_tbl_idx SET TABLESPACE pg_default; SELECT c.relname FROM pg_class c, pg_tablespace s WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace' ORDERBY c.relname;
REINDEX (TABLESPACE regress_tblspace, CONCURRENTLY) TABLE regress_tblspace_test_tbl; SELECT c.relname FROM pg_class c, pg_tablespace s WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace' ORDERBY c.relname; SELECT relfilenode = :main_filenode AS main_same FROM pg_class WHERE relname = 'regress_tblspace_test_tbl_idx'; SELECT relfilenode = :toast_filenode as toast_same FROM pg_class WHERE oid =
(SELECT i.indexrelid FROM pg_class c,
pg_index i WHERE i.indrelid = c.reltoastrelid AND
c.relname = 'regress_tblspace_test_tbl'); DROPTABLE regress_tblspace_test_tbl;
-- REINDEX (TABLESPACE) with partitions -- Create a partition tree and check the set of relations reindexed -- with their new tablespace. CREATETABLE tbspace_reindex_part (c1 int, c2 int) PARTITION BY RANGE (c1); CREATETABLE tbspace_reindex_part_0 PARTITION OF tbspace_reindex_part FORVALUESFROM (0) TO (10) PARTITION BY list (c2); CREATETABLE tbspace_reindex_part_0_1 PARTITION OF tbspace_reindex_part_0 FORVALUESIN (1); CREATETABLE tbspace_reindex_part_0_2 PARTITION OF tbspace_reindex_part_0 FORVALUESIN (2); -- This partitioned table will have no partitions. CREATETABLE tbspace_reindex_part_10 PARTITION OF tbspace_reindex_part FORVALUESFROM (10) TO (20) PARTITION BY list (c2); -- Create some partitioned indexes CREATEINDEX tbspace_reindex_part_index ON ONLY tbspace_reindex_part (c1); CREATEINDEX tbspace_reindex_part_index_0 ON ONLY tbspace_reindex_part_0 (c1); ALTERINDEX tbspace_reindex_part_index ATTACH PARTITION tbspace_reindex_part_index_0; -- This partitioned index will have no partitions. CREATEINDEX tbspace_reindex_part_index_10 ON ONLY tbspace_reindex_part_10 (c1); ALTERINDEX tbspace_reindex_part_index ATTACH PARTITION tbspace_reindex_part_index_10; CREATEINDEX tbspace_reindex_part_index_0_1 ON ONLY tbspace_reindex_part_0_1 (c1); ALTERINDEX tbspace_reindex_part_index_0 ATTACH PARTITION tbspace_reindex_part_index_0_1; CREATEINDEX tbspace_reindex_part_index_0_2 ON ONLY tbspace_reindex_part_0_2 (c1); ALTERINDEX tbspace_reindex_part_index_0 ATTACH PARTITION tbspace_reindex_part_index_0_2; SELECT relid, parentrelid, level FROM pg_partition_tree('tbspace_reindex_part_index') ORDERBY relid, level; -- Track the original tablespace, relfilenode and OID of each index -- in the tree. CREATE TEMP TABLE reindex_temp_before AS SELECT oid, relname, relfilenode, reltablespace FROM pg_class WHERE relname ~ 'tbspace_reindex_part_index';
REINDEX (TABLESPACE regress_tblspace, CONCURRENTLY) TABLE tbspace_reindex_part; -- REINDEX CONCURRENTLY changes the OID of the old relation, hence a check -- based on the relation name below. SELECT b.relname, CASEWHEN a.relfilenode = b.relfilenode THEN'relfilenode is unchanged' ELSE'relfilenode has changed' END AS filenode, CASEWHEN a.reltablespace = b.reltablespace THEN'reltablespace is unchanged' ELSE'reltablespace has changed' END AS tbspace FROM reindex_temp_before b JOIN pg_class a ON b.relname = a.relname ORDERBY1; DROPTABLE tbspace_reindex_part;
-- create a schema we can use CREATESCHEMA testschema;
-- try a table CREATETABLE testschema.foo (i int) TABLESPACE regress_tblspace; SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c where c.reltablespace = t.oid AND c.relname = 'foo';
-- tables from dynamic sources CREATETABLE testschema.asselect TABLESPACE regress_tblspace ASSELECT1; SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c where c.reltablespace = t.oid AND c.relname = 'asselect';
PREPARE selectsource(int) ASSELECT $1; CREATETABLE testschema.asexecute TABLESPACE regress_tblspace AS EXECUTE selectsource(2); SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c where c.reltablespace = t.oid AND c.relname = 'asexecute';
-- index CREATEINDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace; SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c where c.reltablespace = t.oid AND c.relname = 'foo_idx';
-- -- partitioned table -- CREATETABLE testschema.part (a int) PARTITION BY LIST (a); SET default_tablespace TO pg_global; CREATETABLE testschema.part_1 PARTITION OF testschema.part FORVALUESIN (1);
RESET default_tablespace; CREATETABLE testschema.part_1 PARTITION OF testschema.part FORVALUESIN (1); SET default_tablespace TO regress_tblspace; CREATETABLE testschema.part_2 PARTITION OF testschema.part FORVALUESIN (2); SET default_tablespace TO pg_global; CREATETABLE testschema.part_3 PARTITION OF testschema.part FORVALUESIN (3); ALTERTABLE testschema.part SET TABLESPACE regress_tblspace; CREATETABLE testschema.part_3 PARTITION OF testschema.part FORVALUESIN (3); CREATETABLE testschema.part_4 PARTITION OF testschema.part FORVALUESIN (4)
TABLESPACE pg_default; CREATETABLE testschema.part_56 PARTITION OF testschema.part FORVALUESIN (5, 6)
PARTITION BY LIST (a); ALTERTABLE testschema.part SET TABLESPACE pg_default; CREATETABLE testschema.part_78 PARTITION OF testschema.part FORVALUESIN (7, 8)
PARTITION BY LIST (a); CREATETABLE testschema.part_910 PARTITION OF testschema.part FORVALUESIN (9, 10)
PARTITION BY LIST (a) TABLESPACE regress_tblspace;
RESET default_tablespace; CREATETABLE testschema.part_78 PARTITION OF testschema.part FORVALUESIN (7, 8)
PARTITION BY LIST (a);
SELECT relname, spcname FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (c.relnamespace = n.oid) LEFTJOIN pg_catalog.pg_tablespace t ON c.reltablespace = t.oid where c.relname LIKE'part%'AND n.nspname = 'testschema'orderby relname;
RESET default_tablespace; DROPTABLE testschema.part;
-- partitioned index CREATETABLE testschema.part (a int) PARTITION BY LIST (a); CREATETABLE testschema.part1 PARTITION OF testschema.part FORVALUESIN (1); CREATEINDEX part_a_idx ON testschema.part (a) TABLESPACE regress_tblspace; CREATETABLE testschema.part2 PARTITION OF testschema.part FORVALUESIN (2); SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c where c.reltablespace = t.oid AND c.relname LIKE'part%_idx'ORDERBY relname;
\d testschema.part
\d+ testschema.part
\d testschema.part1
\d+ testschema.part1
\d testschema.part_a_idx
\d+ testschema.part_a_idx
-- partitioned rels cannot specify the default tablespace. These fail: CREATETABLE testschema.dflt (a intPRIMARYKEY) PARTITION BY LIST (a) TABLESPACE pg_default; CREATETABLE testschema.dflt (a intPRIMARYKEYUSINGINDEX TABLESPACE pg_default) PARTITION BY LIST (a); SET default_tablespace TO'pg_default'; CREATETABLE testschema.dflt (a intPRIMARYKEY) PARTITION BY LIST (a) TABLESPACE regress_tblspace; CREATETABLE testschema.dflt (a intPRIMARYKEYUSINGINDEX TABLESPACE regress_tblspace) PARTITION BY LIST (a); -- but these work: CREATETABLE testschema.dflt (a intPRIMARYKEYUSINGINDEX TABLESPACE regress_tblspace) PARTITION BY LIST (a) TABLESPACE regress_tblspace; SET default_tablespace TO''; CREATETABLE testschema.dflt2 (a intPRIMARYKEY) PARTITION BY LIST (a); DROPTABLE testschema.dflt, testschema.dflt2;
-- check that default_tablespace doesn't affect ALTER TABLE index rebuilds CREATETABLE testschema.test_default_tab(id bigint) TABLESPACE regress_tblspace; INSERTINTO testschema.test_default_tab VALUES (1); CREATEINDEX test_index1 on testschema.test_default_tab (id); CREATEINDEX test_index2 on testschema.test_default_tab (id) TABLESPACE regress_tblspace; ALTERTABLE testschema.test_default_tab ADDCONSTRAINT test_index3 PRIMARYKEY (id); ALTERTABLE testschema.test_default_tab ADDCONSTRAINT test_index4 UNIQUE (id) USINGINDEX TABLESPACE regress_tblspace;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 -- use a custom tablespace for default_tablespace SET default_tablespace TO regress_tblspace; -- tablespace should not change if no rewrite ALTERTABLE testschema.test_default_tab ALTER id TYPE bigint;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 SELECT * FROM testschema.test_default_tab; -- tablespace should not change even if there is an index rewrite ALTERTABLE testschema.test_default_tab ALTER id TYPE int;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 SELECT * FROM testschema.test_default_tab; -- now use the default tablespace for default_tablespace SET default_tablespace TO''; -- tablespace should not change if no rewrite ALTERTABLE testschema.test_default_tab ALTER id TYPE int;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 -- tablespace should not change even if there is an index rewrite ALTERTABLE testschema.test_default_tab ALTER id TYPE bigint;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 DROPTABLE testschema.test_default_tab;
-- check that default_tablespace doesn't affect ALTER TABLE index rebuilds -- (this time with a partitioned table) CREATETABLE testschema.test_default_tab_p(id bigint, val bigint)
PARTITION BY LIST (id) TABLESPACE regress_tblspace; CREATETABLE testschema.test_default_tab_p1 PARTITION OF testschema.test_default_tab_p FORVALUESIN (1); INSERTINTO testschema.test_default_tab_p VALUES (1); CREATEINDEX test_index1 on testschema.test_default_tab_p (val); CREATEINDEX test_index2 on testschema.test_default_tab_p (val) TABLESPACE regress_tblspace; ALTERTABLE testschema.test_default_tab_p ADDCONSTRAINT test_index3 PRIMARYKEY (id); ALTERTABLE testschema.test_default_tab_p ADDCONSTRAINT test_index4 UNIQUE (id) USINGINDEX TABLESPACE regress_tblspace;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 -- use a custom tablespace for default_tablespace SET default_tablespace TO regress_tblspace; -- tablespace should not change if no rewrite ALTERTABLE testschema.test_default_tab_p ALTER val TYPE bigint;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 SELECT * FROM testschema.test_default_tab_p; -- tablespace should not change even if there is an index rewrite ALTERTABLE testschema.test_default_tab_p ALTER val TYPE int;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 SELECT * FROM testschema.test_default_tab_p; -- now use the default tablespace for default_tablespace SET default_tablespace TO''; -- tablespace should not change if no rewrite ALTERTABLE testschema.test_default_tab_p ALTER val TYPE int;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 -- tablespace should not change even if there is an index rewrite ALTERTABLE testschema.test_default_tab_p ALTER val TYPE bigint;
\d testschema.test_index1
\d testschema.test_index2
\d testschema.test_index3
\d testschema.test_index4 DROPTABLE testschema.test_default_tab_p;
-- check that default_tablespace affects index additions in ALTER TABLE CREATETABLE testschema.test_tab(id int) TABLESPACE regress_tblspace; INSERTINTO testschema.test_tab VALUES (1); SET default_tablespace TO regress_tblspace; ALTERTABLE testschema.test_tab ADDCONSTRAINT test_tab_unique UNIQUE (id); SET default_tablespace TO''; ALTERTABLE testschema.test_tab ADDCONSTRAINT test_tab_pkey PRIMARYKEY (id);
\d testschema.test_tab_unique
\d testschema.test_tab_pkey SELECT * FROM testschema.test_tab; DROPTABLE testschema.test_tab;
-- check that default_tablespace is handled correctly by multi-command -- ALTER TABLE that includes a tablespace-preserving rewrite CREATETABLE testschema.test_tab(a int, b int, c int); SET default_tablespace TO regress_tblspace; ALTERTABLE testschema.test_tab ADDCONSTRAINT test_tab_unique UNIQUE (a); CREATEINDEX test_tab_a_idx ON testschema.test_tab (a); SET default_tablespace TO''; CREATEINDEX test_tab_b_idx ON testschema.test_tab (b);
\d testschema.test_tab_unique
\d testschema.test_tab_a_idx
\d testschema.test_tab_b_idx ALTERTABLE testschema.test_tab ALTER b TYPE bigint, ADDUNIQUE (c);
\d testschema.test_tab_unique
\d testschema.test_tab_a_idx
\d testschema.test_tab_b_idx DROPTABLE testschema.test_tab;
-- let's try moving a table from one place to another CREATETABLE testschema.atable ASVALUES (1), (2); CREATEUNIQUEINDEX anindex ON testschema.atable(column1);
ALTERTABLE testschema.atable SET TABLESPACE regress_tblspace; ALTERINDEX testschema.anindex SET TABLESPACE regress_tblspace; ALTERINDEX testschema.part_a_idx SET TABLESPACE pg_global; ALTERINDEX testschema.part_a_idx SET TABLESPACE pg_default; ALTERINDEX testschema.part_a_idx SET TABLESPACE regress_tblspace;
INSERTINTO testschema.atable VALUES(3); -- ok INSERTINTO testschema.atable VALUES(1); -- fail (checks index) SELECT COUNT(*) FROM testschema.atable; -- checks heap
-- let's try moving a materialized view from one place to another CREATE MATERIALIZED VIEW testschema.amv ASSELECT * FROM testschema.atable; ALTER MATERIALIZED VIEW testschema.amv SET TABLESPACE regress_tblspace;
REFRESH MATERIALIZED VIEW testschema.amv; SELECT COUNT(*) FROM testschema.amv;
-- Will fail with bad path CREATE TABLESPACE regress_badspace LOCATION '/no/such/location';
-- No such tablespace CREATETABLE bar (i int) TABLESPACE regress_nosuchspace;
-- Fail, in use for some partitioned object DROP TABLESPACE regress_tblspace; ALTERINDEX testschema.part_a_idx SET TABLESPACE pg_default; -- Fail, not empty DROP TABLESPACE regress_tblspace;
-- Adequate cache initialization before GRANT
\c -
BEGIN; GRANTALLON TABLESPACE regress_tblspace TO PUBLIC;
ROLLBACK;
CREATE ROLE regress_tablespace_user1 login; CREATE ROLE regress_tablespace_user2 login; GRANTUSAGEONSCHEMA testschema TO regress_tablespace_user2;
ALTER TABLESPACE regress_tblspace OWNER TO regress_tablespace_user1;
CREATETABLE testschema.tablespace_acl (c int); -- new owner lacks permission to create this index from scratch CREATEINDEX k ON testschema.tablespace_acl (c) TABLESPACE regress_tblspace; ALTERTABLE testschema.tablespace_acl OWNER TO regress_tablespace_user2;
SET SESSION ROLE regress_tablespace_user2; CREATETABLE tablespace_table (i int) TABLESPACE regress_tblspace; -- fail ALTERTABLE testschema.tablespace_acl ALTER c TYPE bigint;
REINDEX (TABLESPACE regress_tblspace) TABLE tablespace_table; -- fail
REINDEX (TABLESPACE regress_tblspace, CONCURRENTLY) TABLE tablespace_table; -- fail
RESET ROLE;
ALTER TABLESPACE regress_tblspace RENAMETO regress_tblspace_renamed;
ALTERTABLEALLIN TABLESPACE regress_tblspace_renamed SET TABLESPACE pg_default; ALTERINDEXALLIN TABLESPACE regress_tblspace_renamed SET TABLESPACE pg_default; ALTER MATERIALIZED VIEW ALLIN TABLESPACE regress_tblspace_renamed SET TABLESPACE pg_default;
-- Should show notice that nothing was done ALTERTABLEALLIN TABLESPACE regress_tblspace_renamed SET TABLESPACE pg_default; ALTER MATERIALIZED VIEW ALLIN TABLESPACE regress_tblspace_renamed SET TABLESPACE pg_default;
-- Should succeed DROP TABLESPACE regress_tblspace_renamed;
DROPSCHEMA testschema CASCADE;
DROP ROLE regress_tablespace_user1; DROP ROLE regress_tablespace_user2;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.