Understanding Tablespaces in PostgreSQL: pg_default and pg_global
In PostgreSQL, tablespaces are locations on the file system where database objects (like tables, indexes, and data) are stored. They provide flexibility in managing the physical storage of data.
1. What is a Tablespace?
A tablespace is a directory on the server's file system used to store database files.
By default, PostgreSQL creates two tablespaces:
pg_default
pg_global
These tablespaces ensure that the system and user-defined objects are organized and stored correctly.
2. The Default Tablespaces
a. pg_default
Purpose:
- It is the default tablespace for all user-defined objects, such as tables and indexes, unless another tablespace is explicitly specified.
Characteristics:
Associated with the base directory of your PostgreSQL data directory.
Stores most database objects (e.g., tables, indexes) unless directed to another tablespace.
Example Use:
- If you create a table and do not specify a tablespace, the table will be stored in
pg_default
.
- If you create a table and do not specify a tablespace, the table will be stored in
CREATE TABLE my_table (
id SERIAL PRIMARY KEY,
name TEXT
);
- The table
my_table
will be stored in thepg_default
tablespace.
b. pg_global
Purpose:
- Stores shared system-wide objects that are not tied to a specific database.
Examples of Stored Objects:
The
pg_authid
table, which contains user and role information.Other global metadata shared across all databases in the PostgreSQL instance.
Characteristics:
- System-level tablespace for objects that are global and accessed by all databases in the cluster.
Example Use:
- When managing users and roles, the data is stored in
pg_global
.
- When managing users and roles, the data is stored in
SELECT * FROM pg_roles; -- Data fetched from pg_global
3. Why Use Tablespaces?
Performance Optimization:
- Tablespaces can be created on different disks or storage devices to balance I/O load.
Scalability:
- Store large tables on dedicated high-performance storage while keeping smaller tables on general-purpose storage.
Data Organization:
- Separate tablespaces for different databases, schemas, or object types for better data management.
4. How to Create and Use a Tablespace?
Creating a Tablespace
Use the
CREATE TABLESPACE
command to define a new tablespace.Specify the directory location on the server file system.
CREATE TABLESPACE fast_storage LOCATION '/mnt/fast_drive';
Using a Tablespace
Assign a tablespace to a table or index during creation.
CREATE TABLE large_table ( id SERIAL PRIMARY KEY, data TEXT ) TABLESPACE fast_storage;
Changing a Table's Tablespace
Use the
ALTER TABLE
command to move a table to a different tablespace.ALTER TABLE my_table SET TABLESPACE fast_storage;
5. Differences Between pg_default and pg_global
Feature | pg_default | pg_global |
Purpose | Stores database-specific objects | Stores global objects shared across all databases |
Default Usage | Tables, indexes, and other user objects | Metadata like roles and global settings |
Location | Inside the PostgreSQL data directory (base/ ) | Inside the PostgreSQL data directory (global/ ) |
Accessibility | Accessible only within the specific database | Accessible across all databases in the PostgreSQL instance |
6. Checking Tablespace Usage
List Available Tablespaces
Use the \db
command in psql
or query the pg_tablespace
system catalog.
SELECT * FROM pg_tablespace;
Check Tablespace for a Table
To check which tablespace a specific table belongs to, use:
SELECT relname, tablespace
FROM pg_class c
LEFT JOIN pg_tablespace t ON c.reltablespace = t.oid
WHERE relname = 'my_table';
- If
tablespace
is NULL, the table resides inpg_default
.
7. Key Considerations for pg_default and pg_global
pg_default:
Best for general-purpose storage.
Automatically used unless a different tablespace is specified.
Use it for typical database objects unless specific storage needs arise.
pg_global:
Primarily for system-level data (e.g., roles, shared catalogs).
Not meant for user-defined objects.
Automatically managed by PostgreSQL; no user intervention required.
8. Practical Scenarios
When to Use Custom Tablespaces:
Large-scale applications with high storage demands.
Databases requiring separation of hot (frequently accessed) and cold (archived) data.
Workloads involving intensive I/O operations.
When to Stick with pg_default:
Small or medium-sized applications without complex storage needs.
If no performance or storage optimization is required.
When to Care About pg_global:
If you're managing users, roles, or system-wide settings.
Primarily for database administrators, as regular users won't interact with
pg_global
directly.
Summary
pg_default: The standard tablespace for most objects.
pg_global: Stores global metadata and system objects.
Custom Tablespaces: Provide flexibility and performance optimization for advanced use cases.