The idea · Lab 3.4.5Every table and index PostgreSQL creates lives in some tablespace — by default, the cluster's single pg_default tablespace, wherever PGDATA itself lives on disk. A tablespace is just a named pointer to a directory PostgreSQL has permission to write to; creating one, and then placing specific tables or indexes in it, is how a busy table's heavy write traffic can be physically separated from its own indexes' equally heavy write traffic, or how cold, rarely-touched data can be moved onto cheaper, slower storage without touching the schema, the queries, or the application at all.
This lab builds two real tablespaces on two separate directories, places a table and its index directly into one, moves an existing table into the other, and confirms a specific, easy-to-miss fact directly: tablespaces belong to the whole PostgreSQL cluster, not to any one database — a tablespace created while connected to one database is immediately usable from every other database in the same cluster.
Two Real Tablespaces
CREATE TABLESPACE fast_ssd LOCATION '/tmp/fast';
CREATE TABLESPACE archive_hdd LOCATION '/tmp/slow';
Each points at a real, existing, PostgreSQL-writable directory — no special filesystem required, just a location the server process can read and write.
Placing New Objects Directly
CREATE TABLE sql_products_ts (id int PRIMARY KEY, name text) TABLESPACE fast_ssd;
CREATE INDEX idx_products_ts_name ON sql_products_ts(name) TABLESPACE fast_ssd;
A table and one of its indexes, placed directly in fast_ssd at creation time — no separate move step needed when the placement is known upfront.
Moving an Existing Table
CREATE TABLE sql_event_archive (id int PRIMARY KEY, data text); -- created in the default tablespace
ALTER TABLE sql_event_archive SET TABLESPACE archive_hdd;
SELECT tablename, tablespace FROM pg_tables WHERE tablename IN ('sql_products_ts','sql_event_archive');
tablename | tablespace
-------------------+-------------
sql_products_ts | fast_ssd
sql_event_archive | archive_hdd
Both placements confirmed directly from pg_tables, not assumed from the commands having run without error.
Cluster-Global, Confirmed Directly
-- fast_ssd was created while connected to beer_db. Switch to a completely different database:
\c postgres
SELECT spcname FROM pg_tablespace WHERE spcname = 'fast_ssd'; -- still visible
CREATE TABLE other_db_table (id int) TABLESPACE fast_ssd; -- works immediately
A tablespace created in one database is immediately visible and usable from every other database in the same cluster — it is not scoped to the database it happened to be created from.
A Real Symlink on Disk
$ ls -la $PGDATA/pg_tblspc/
lrwxrwxrwx 1 postgres postgres 9 19011 -> /tmp/fast
Every non-default tablespace shows up in PGDATA's pg_tblspc directory as a real symbolic link, named by the tablespace's OID, pointing at the actual location — the mechanism made visible directly, not just described.
Key concepts
Tablespace
A named pointer to a directory on disk where PostgreSQL stores the actual files for tables, indexes, or an entire database. Every cluster starts with pg_default (where user objects live unless told otherwise) and pg_global (shared catalog data); additional tablespaces are created explicitly with CREATE TABLESPACE, each represented in PGDATA's pg_tblspc directory as a symbolic link to the real location.
Cluster-global object
An object that belongs to the whole PostgreSQL server instance (the cluster) rather than to any single database within it — roles and tablespaces are the two most common examples. A tablespace created while connected to one database is immediately visible and usable from every other database on the same server, with no per-database setup required.
What to remember
- A tablespace is a named pointer to a directory PostgreSQL can read and write — creating one requires no special filesystem, just a real writable location
- CREATE TABLE/INDEX ... TABLESPACE places a new object directly; ALTER TABLE/INDEX ... SET TABLESPACE moves an existing one, both confirmable directly via pg_tables/pg_indexes
- Tablespaces are cluster-global objects — created once, usable immediately from every database on the same server, with no per-database registration step
- Every non-default tablespace appears in PGDATA's pg_tblspc directory as a real symbolic link named by its OID, pointing at the actual location — a directly verifiable mechanism, not an abstraction
- Separating a busy table from its own indexes onto different tablespaces (different physical disks) splits real I/O contention at the storage layer, with no schema, query, or application changes required