CREATE TABLESPACE
A tablespace is a named location for files: a local directory, a prefix in an S3 bucket or an S3-compatible store, or a prefix in an Azure Blob Storage container. Tables, inverted indexes on views and whole databases can be placed in a tablespace, so that their files live on a faster disk, a larger one, or an object store.
Examples
Create a tablespace on a local disk:
CREATE TABLESPACE doc_fast LOCATION '/mnt/nvme/serenedb';Put a table in it:
CREATE TABLE doc_articles (id INTEGER, body TEXT)TABLESPACE doc_fast;Put an inverted index on a view in it:
CREATE VIEW doc_notes ASSELECT * FROM read_parquet('s3://bucket/notes/*.parquet');
CREATE INDEX doc_notes_body ON doc_notes USING inverted (id, body)TABLESPACE doc_fast;setup error: count 2
Put every table and every inverted index on a view that a session creates in it, unless CREATE names another tablespace:
SET default_tablespace = 'doc_fast';
CREATE TABLE doc_events (id INTEGER, kind TEXT);
RESET default_tablespace;See where each object is placed:
SELECT c.relname, t.spcnameFROM pg_class AS cJOIN pg_tablespace AS t ON t.oid = c.reltablespaceWHERE c.relname LIKE 'doc\_%'ORDER BY c.relname;relname | spcname ----------------+---------- doc_articles | doc_fast doc_events | doc_fast doc_notes_body | doc_fast
Syntax
CREATE TABLESPACE name [ OWNER { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ] LOCATION 'location'
name: the tablespace's name. Names starting withpg_are reserved for the built-in tablespaces.OWNER: the role that owns the tablespace; the role running the statement by default.LOCATION: an absolute path of a local directory, or an object store URL such ass3://bucket/prefixoraz://container/prefix.
Only a superuser can create a tablespace. CREATE TABLESPACE cannot run inside a transaction block. With enable_external_access off, the location must be inside allowed_directories; tablespaces created before keep working.
Locations
A location is kept in normal form, with backslashes read as slashes, repeated and trailing slashes dropped and . and .. resolved: LOCATION 's3://bucket//prefix/' is s3://bucket/prefix and LOCATION '/mnt/nvme\serenedb/' is /mnt/nvme/serenedb. A tablespace keeps its files in <system identifier>/engine_v1 at its location, named by the server's system identifier, so several servers can share a location. Of one server, one tablespace at a time can use a location:
ERROR: tablespace location "/mnt/nvme/serenedb" is already used by tablespace "fast"
The directory is created when it does not exist, on an object store as an empty object named after the location with a trailing slash, and with it <system identifier>/engine_v1/store inside it, the directory that holds the index files of everything placed in the tablespace. A flush into a tablespace whose store directory was removed fails until the directory is there again. The server reads and writes nothing above the location, so a secret whose scope is the location is enough; CREATE TABLESPACE fails when the store cannot be reached or the credentials do not allow reading, listing and writing the location. A location with an engine_v1 directory in its path, such as one inside the files of another tablespace or of the data directory, is refused:
ERROR: tablespace location "/mnt/nvme/serenedb/engine_v1/inside" is inside the files of a tablespace
A memory:// location is refused, since files there are lost when the server stops:
ERROR: tablespace location cannot be in memory
DETAIL: Files under memory:// are lost when the server stops.
An object store URL has to name a bucket or a container:
ERROR: tablespace location must name a bucket or container
The server reaches an object store location with the credentials of a persistent secret whose scope covers its normal form:
CREATE PERSISTENT SECRET archive_store ( TYPE S3, KEY_ID '⟨AKIA...⟩', SECRET '⟨secret⟩', REGION '⟨eu-central-1⟩', SCOPE 's3://⟨bucket⟩/archive');CREATE TABLESPACE doc_archive LOCATION 's3://⟨bucket⟩/archive';See Index Storage for the credentials, the caching and the requests that index files on an object store cost.
On S3, the parts of an upload that the server stops in the middle of stay in the bucket, unlisted and billed, until the upload is aborted: give the bucket a lifecycle rule that aborts incomplete multipart uploads.
A local location has to exist on the server's machine at the same path for as long as the tablespace holds files.
A backup copies only the data directory, so pg_backup_start fails while a tablespace other than pg_default and pg_global exists.
The built-in tablespaces are always there:
| Tablespace | Location |
|---|---|
pg_default | the data directory; where objects go when nothing names another tablespace |
pg_global | reserved; nothing can be placed in it |
pg_tablespace lists the tablespaces and pg_tablespace_location(oid) returns a tablespace's location, an empty string for the built-in ones:
SELECT spcname, pg_tablespace_location(oid) AS locationFROM pg_tablespaceORDER BY spcname;spcname | location ------------+-------------------- doc_fast | /mnt/nvme/serenedb pg_default | (empty) pg_global | (empty)
pg_tablespace_size(name) or pg_tablespace_size(oid) returns the bytes of a tablespace's files, which psql's \db+ shows: for pg_default the files of the tables and indexes in the data directory, for pg_global the catalog that all databases share. On an object store each call costs a listing of the tablespace's directory.
Placing objects
TABLESPACE name places a new object in a tablespace:
| Statement | Placed in the tablespace |
|---|---|
CREATE TABLE ... TABLESPACE name | the files of the table and of its indexes |
CREATE TABLE ... TABLESPACE name AS query | the same, for a table created from a query |
CREATE INDEX ... ON view USING inverted (...) [WITH (...)] TABLESPACE name | the files of an inverted index on a view |
CREATE DATABASE name [WITH] TABLESPACE [=] name | by default, the tables of the database and its inverted indexes on views |
An object created without TABLESPACE goes to the tablespace that the default_tablespace setting names, and when that is empty, to the tablespace of its database. default_tablespace takes the name of an existing tablespace; a tablespace dropped while a session names it is skipped.
An index on a table is stored with the table, so it takes no TABLESPACE clause, and default_tablespace does not apply to it:
ERROR: an index on a table is stored with the table and cannot be placed in a tablespace
A temporary table and its indexes are kept in memory: they take no TABLESPACE clause, and default_tablespace does not apply to them:
ERROR: temporary objects cannot be placed in a tablespace
A table or an index in a database other than a SereneDB database, such as an attached DuckDB file, takes no TABLESPACE clause:
ERROR: TABLESPACE is not supported for tables in a duckdb catalog
Create a database in a tablespace:
CREATE DATABASE doc_analytics TABLESPACE doc_fast;
SELECT d.datname, t.spcnameFROM pg_database AS dJOIN pg_tablespace AS t ON t.oid = d.dattablespaceWHERE d.datname = 'doc_analytics';datname | spcname ---------------+---------- doc_analytics | doc_fast
pg_class.reltablespace is the tablespace of a table or an inverted index, 0 for the tablespace of its database, and an index on a table shows the table's; pg_database.dattablespace is the tablespace of a database. An object stays in its tablespace for its whole life: moving it to another tablespace is not supported.
Privileges
Placing an object in a tablespace other than its database's needs the CREATE privilege on the tablespace, which the owner and superusers hold:
CREATE ROLE doc_ts_writer;
GRANT CREATE ON TABLESPACE doc_fast TO doc_ts_writer;
SELECT has_tablespace_privilege('doc_ts_writer', 'doc_fast', 'CREATE') AS can_create;can_create ------------ t
has_tablespace_privilege checks it. The built-in tablespaces cannot be granted on, renamed or given to another owner; CREATE on pg_default is held by superusers only, and every role can place objects in the tablespace of their database.
ALTER TABLESPACE
ALTER TABLESPACE name RENAME TO new_name
ALTER TABLESPACE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
Renaming a tablespace or changing its owner needs ownership of it. The objects in a tablespace stay where they are when it is renamed:
ALTER TABLESPACE doc_fast RENAME TO doc_nvme;
ALTER TABLESPACE doc_nvme OWNER TO doc_ts_writer;DROP TABLESPACE
DROP TABLESPACE [ IF EXISTS ] name
Only the owner of a tablespace, or a superuser, can drop it, and only when it is empty: no database was created in it and no table or inverted index is placed in it.
Dropping a tablespace removes no files: each object that was in it removed its own files when it was dropped, once the transactions that began before that drop ended. Empty directories stay at the location, on an object store as the empty objects that stand for them, and a new tablespace can use the location at once. DROP TABLESPACE cannot run inside a transaction block.
When one transaction places an object in a tablespace while another drops the tablespace, the one that commits second fails, as with any object that another depends on. A drop fails with tablespace "name" is not empty when the placement committed before the drop began, and with Could not commit DROP of "name" because a dependency was created after the transaction started when it commits later; a placement that commits after the drop fails with Could not commit creation of dependency, subject "name" has been deleted; the files of an object that fails this way are removed.
DROP TABLESPACE doc_nvme;
DROP DATABASE doc_analytics;
DROP TABLE doc_articles;
DROP TABLE doc_events;
DROP INDEX doc_notes_body;
DROP TABLESPACE doc_nvme;db error: ERROR: tablespace "doc_nvme" is not empty
See also
- Index Storage — object store credentials, caching and requests
- CREATE DATABASE
- CREATE INDEX