9f8064bb创建于 2023年3月3日历史提交
-- create a tablespace we can use
CREATE TABLESPACE testspace LOCATION '@testtablespace@';
-- try setting and resetting some properties for the new tablespace
ALTER TABLESPACE testspace SET (random_page_cost = 1.0);
ALTER TABLESPACE testspace SET (some_nonexistent_parameter = true);  -- fail
ERROR:  unrecognized parameter "some_nonexistent_parameter"
ALTER TABLESPACE testspace RESET (random_page_cost = 2.0); -- fail
ERROR:  RESET must not include values for parameters
ALTER TABLESPACE testspace RESET (random_page_cost, seq_page_cost); -- ok
-- create a schema we can use
CREATE SCHEMA testschema;
-- try a table
CREATE TABLE testschema.foo (i int) TABLESPACE testspace;
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
    where c.reltablespace = t.oid AND c.relname = 'foo';
 relname |  spcname  
---------+-----------
 foo     | testspace
(1 row)

INSERT INTO testschema.foo VALUES(1);
INSERT INTO testschema.foo VALUES(2);
-- tables from dynamic sources
CREATE TABLE testschema.asselect TABLESPACE testspace AS SELECT 1;
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
    where c.reltablespace = t.oid AND c.relname = 'asselect';
 relname  |  spcname  
----------+-----------
 asselect | testspace
(1 row)

PREPARE selectsource(int) AS SELECT $1;
CREATE TABLE testschema.asexecute TABLESPACE testspace
    AS EXECUTE selectsource(2);
ERROR:  CREATE TABLE AS EXECUTE not yet supported
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
    where c.reltablespace = t.oid AND c.relname = 'asexecute';
 relname | spcname 
---------+---------
(0 rows)

-- index
CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE testspace;
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
    where c.reltablespace = t.oid AND c.relname = 'foo_idx';
 relname |  spcname  
---------+-----------
 foo_idx | testspace
(1 row)

-- check that default_tablespace doesn't affect ALTER TABLE index rebuilds
CREATE TABLE testschema.test_default_tab(id bigint) TABLESPACE testspace;
INSERT INTO testschema.test_default_tab VALUES (1);
CREATE INDEX test_index1 on testschema.test_default_tab (id);
CREATE INDEX test_index2 on testschema.test_default_tab (id) TABLESPACE testspace;
\d testschema.test_index1
Index "testschema.test_index1"
 Column |  Type  | Definition 
--------+--------+------------
 id     | bigint | id
btree, for table "testschema.test_default_tab"

\d testschema.test_index2
Index "testschema.test_index2"
 Column |  Type  | Definition 
--------+--------+------------
 id     | bigint | id
btree, for table "testschema.test_default_tab"
Tablespace: "testspace"

-- use a custom tablespace for default_tablespace
SET default_tablespace TO testspace;
-- tablespace should not change if no rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint;
\d testschema.test_index1
Index "testschema.test_index1"
 Column |  Type  | Definition 
--------+--------+------------
 id     | bigint | id
btree, for table "testschema.test_default_tab"

\d testschema.test_index2
Index "testschema.test_index2"
 Column |  Type  | Definition 
--------+--------+------------
 id     | bigint | id
btree, for table "testschema.test_default_tab"
Tablespace: "testspace"

SELECT * FROM testschema.test_default_tab;
 id 
----
  1
(1 row)

-- tablespace should not change even if there is an index rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE int;
\d testschema.test_index1
Index "testschema.test_index1"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
btree, for table "testschema.test_default_tab"

\d testschema.test_index2
Index "testschema.test_index2"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
btree, for table "testschema.test_default_tab"
Tablespace: "testspace"

SELECT * FROM testschema.test_default_tab;
 id 
----
  1
(1 row)

-- now use the default tablespace for default_tablespace
SET default_tablespace TO '';
-- tablespace should not change if no rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE int;
\d testschema.test_index1
Index "testschema.test_index1"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
btree, for table "testschema.test_default_tab"

\d testschema.test_index2
Index "testschema.test_index2"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
btree, for table "testschema.test_default_tab"
Tablespace: "testspace"

-- tablespace should not change even if there is an index rewrite
ALTER TABLE testschema.test_default_tab ALTER id TYPE bigint;
\d testschema.test_index1
Index "testschema.test_index1"
 Column |  Type  | Definition 
--------+--------+------------
 id     | bigint | id
btree, for table "testschema.test_default_tab"

\d testschema.test_index2
Index "testschema.test_index2"
 Column |  Type  | Definition 
--------+--------+------------
 id     | bigint | id
btree, for table "testschema.test_default_tab"
Tablespace: "testspace"

DROP TABLE testschema.test_default_tab;
-- let's try moving a table from one place to another
CREATE TABLE testschema.atable AS VALUES (1), (2);
CREATE UNIQUE INDEX anindex ON testschema.atable(column1);
ALTER TABLE testschema.atable SET TABLESPACE testspace;
ALTER INDEX testschema.anindex SET TABLESPACE testspace;
INSERT INTO testschema.atable VALUES(3);	-- ok
INSERT INTO testschema.atable VALUES(1);	-- fail (checks index)
ERROR:  duplicate key value violates unique constraint "anindex"
DETAIL:  Key (column1)=(1) already exists.
SELECT COUNT(*) FROM testschema.atable;		-- checks heap
 count 
-------
     3
(1 row)

-- Will fail with bad path
CREATE TABLESPACE badspace LOCATION '/no/such/location';
ERROR:  could not create tablespace directory "/no/such/location": Permission denied
-- No such tablespace
CREATE TABLE bar (i int) TABLESPACE nosuchspace;
ERROR:  tablespace "nosuchspace" does not exist
-- Fail, not empty
DROP TABLESPACE testspace;
ERROR:  tablespace "testspace" is not empty
CREATE USER tablespace_user password 'gauss@123';
alter tablespace testspace owner to tablespace_user;
DROP SCHEMA testschema CASCADE;
NOTICE:  drop cascades to 3 other objects
DETAIL:  drop cascades to table testschema.foo
drop cascades to table testschema.asselect
drop cascades to table testschema.atable
-- Should succeed
DROP TABLESPACE testspace;
drop user tablespace_user;