Skip to main content

Command Palette

Search for a command to run...

Is PostgesSQL table space different from database schema?

Published
4 min read
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

Tablespaces and Database Schemas are different concepts in PostgreSQL. Both are used to organize and manage database objects, but they serve entirely different purposes. Here's a detailed comparison:


1. What is a Tablespace?

A tablespace in PostgreSQL refers to a location on the filesystem where the physical files of database objects (e.g., tables, indexes) are stored.

Key Characteristics:

  • Physical Storage: Tablespaces determine where the actual data is stored on disk.

  • File System Level: They map to specific directories on the server.

  • Purpose: Used for storage management and performance optimization.

  • Scope: Tablespaces are cluster-wide; multiple databases within the same PostgreSQL cluster can share a tablespace.

Usage:

  • Distribute data across different disks for load balancing.

  • Store large objects (e.g., indexes or tables) on high-performance storage devices.

  • Separate different types of data for better organization or security.

Example:

  1. Create a tablespace on a specific location:

     CREATE TABLESPACE fast_storage LOCATION '/mnt/fast_drive';
    
  2. Use the tablespace when creating a table:

     CREATE TABLE large_table (
         id SERIAL PRIMARY KEY,
         data TEXT
     ) TABLESPACE fast_storage;
    

2. What is a Database Schema?

A schema in PostgreSQL is a logical namespace within a database that organizes and groups related database objects like tables, views, indexes, and functions.

Key Characteristics:

  • Logical Organization: Schemas group objects logically within a database.

  • Namespace Management: Objects in different schemas can have the same name without conflicts.

  • Purpose: Used to separate and organize objects within a single database.

  • Scope: A schema exists only within a specific database; it cannot span across multiple databases.

Usage:

  • Organize objects based on functionality (e.g., sales, hr schemas).

  • Provide multi-tenancy by creating separate schemas for each tenant.

  • Control permissions at the schema level.

Example:

  1. Create a schema:

     CREATE SCHEMA sales;
    
  2. Create a table within the schema:

     CREATE TABLE sales.orders (
         order_id SERIAL PRIMARY KEY,
         customer_id INT,
         total_amount NUMERIC
     );
    
  3. Access the table using its schema-qualified name:

     SELECT * FROM sales.orders;
    

Key Differences Between Tablespace and Schema

AspectTablespaceSchema
PurposeDefines the physical location of data on the filesystem.Organizes objects logically within a database.
ScopeCluster-wide (shared by multiple databases).Database-specific (exists only within one database).
Type of OrganizationPhysical storage management.Logical grouping of database objects.
Used ForOptimizing disk usage and storage distribution.Grouping objects (e.g., tables, views, functions) by namespace or functionality.
Objects StoredData files for tables, indexes, etc.Tables, views, indexes, functions, and other database objects.
Access LevelManaged by the database administrator (DBA).Accessible and manageable by database users and developers.
Example CommandCREATE TABLESPACE fast_storage LOCATION '/mnt/fast_drive';CREATE SCHEMA sales;

3. How Do Tablespaces and Schemas Work Together?

  • Tablespaces control where the physical files for database objects are stored.

  • Schemas control how database objects are logically grouped and organized within a database.

Example:

  1. Create a tablespace for high-performance storage:

     CREATE TABLESPACE fast_storage LOCATION '/mnt/ssd_drive';
    
  2. Create a schema for the sales department:

     CREATE SCHEMA sales;
    
  3. Create a table in the sales schema that uses the fast_storage tablespace:

     CREATE TABLE sales.orders (
         order_id SERIAL PRIMARY KEY,
         total_amount NUMERIC
     ) TABLESPACE fast_storage;
    

4. When to Use Tablespaces vs. Schemas?

Use Tablespaces When:

  • You want to optimize disk performance by distributing data across different storage devices.

  • You need to store specific objects (e.g., large tables or indexes) on high-speed storage.

  • You’re managing storage quotas or isolating data for performance reasons.

Use Schemas When:

  • You need to logically group related objects for better organization.

  • You’re implementing multi-tenancy (e.g., each tenant gets its own schema).

  • You want to control user permissions at a namespace level.


Summary

  • Tablespaces: Deal with the physical storage of data on disk and are cluster-wide.

  • Schemas: Deal with the logical organization of database objects within a database.

  • Both concepts can be used together to manage and optimize your PostgreSQL database effectively.

12 views
Is PostgesSQL table space different from database schema?