DEV Community

Cover image for PostgreSQL: The Powerhouse Database
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on • Edited on

PostgreSQL: The Powerhouse Database

PostgreSQL (often called Postgres) is one of the most powerful, reliable, and extensible open-source relational database management systems in the world. It is trusted by companies like Apple, Netflix, Reddit, Instagram, and countless startups for mission-critical workloads.

This article is a deep technical guide that explains PostgreSQL from the inside out—architecture, internals, SQL syntax, advanced features, and real-world usage.


1. What Is PostgreSQL?

PostgreSQL is an object-relational database system (ORDBMS) that supports:

  • Relational data
  • ACID compliance
  • Advanced SQL (beyond the SQL standard)
  • Extensibility (custom types, operators, functions)
  • High concurrency
  • Strong data integrity

Key characteristics:

  • Open source (PostgreSQL License)
  • Cross-platform
  • Highly extensible
  • Production-grade reliability

2. PostgreSQL Architecture (High-Level)

PostgreSQL follows a process-based architecture, not a thread-based one.

Main Components

Client
  |
Postmaster (Parent Process)
  |
  |-- Backend Process (per connection)
  |-- Background Writer
  |-- WAL Writer
  |-- Checkpointer
  |-- Autovacuum
  |-- Logical Replication Launcher
Enter fullscreen mode Exit fullscreen mode

Client–Server Model

  • Each client connection → one backend process
  • Isolation and crash safety
  • Higher memory usage than thread-based DBs, but more robust

3. PostgreSQL Internal Architecture (Deep Dive)

3.1 Backend Process Lifecycle

  1. Client sends connection request
  2. postmaster authenticates user
  3. A backend process is forked
  4. Backend executes SQL commands
  5. Results returned to client

3.2 Memory Architecture

PostgreSQL uses shared memory + local memory.

Shared Memory

  • Shared by all processes

Components:

  • Shared Buffers (cache table/index pages)
  • WAL buffers
  • Lock tables
  • Process arrays

Local Memory (Per Backend)

  • Work memory (work_mem)
  • Maintenance memory (maintenance_work_mem)
  • Sort memory
  • Hash memory

3.3 Storage Architecture

PostgreSQL stores data as files on disk.

Data Directory Structure

base/          -> databases
global/        -> cluster-wide metadata
pg_wal/        -> WAL logs
pg_stat/       -> statistics
pg_tblspc/     -> tablespaces
Enter fullscreen mode Exit fullscreen mode

Tables and Pages

  • Table → File
  • File → 8KB pages (blocks)
  • Page contains:

    • Header
    • Item pointers
    • Tuples (rows)

3.4 MVCC (Multi-Version Concurrency Control)

PostgreSQL uses MVCC to handle concurrency.

  • No read locks
  • Readers never block writers
  • Writers never block readers

Each row has:

  • xmin (transaction ID that created it)
  • xmax (transaction ID that deleted it)

4. WAL (Write-Ahead Logging)

WAL guarantees durability and crash recovery.

Rule:

Changes are written to WAL before being written to data files.

Benefits:

  • Crash recovery
  • Replication
  • Point-in-time recovery (PITR)

5. PostgreSQL SQL Syntax (Complete Guide)


6. Database and Schema Management

Create Database

CREATE DATABASE mydb;
Enter fullscreen mode Exit fullscreen mode

List Databases

\l
Enter fullscreen mode Exit fullscreen mode

Connect

\c mydb
Enter fullscreen mode Exit fullscreen mode

Create Schema

CREATE SCHEMA sales;
Enter fullscreen mode Exit fullscreen mode

Set Schema Search Path

SET search_path TO sales;
Enter fullscreen mode Exit fullscreen mode

7. Data Types (Extensive)

Numeric

INT, BIGINT, SMALLINT
DECIMAL, NUMERIC
REAL, DOUBLE PRECISION
Enter fullscreen mode Exit fullscreen mode

Character

CHAR(n)
VARCHAR(n)
TEXT
Enter fullscreen mode Exit fullscreen mode

Date & Time

DATE
TIME
TIMESTAMP
TIMESTAMPTZ
INTERVAL
Enter fullscreen mode Exit fullscreen mode

Boolean

BOOLEAN
Enter fullscreen mode Exit fullscreen mode

UUID

UUID
Enter fullscreen mode Exit fullscreen mode

JSON

JSON
JSONB
Enter fullscreen mode Exit fullscreen mode

Arrays

INT[]
TEXT[]
Enter fullscreen mode Exit fullscreen mode

Custom Types

CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
Enter fullscreen mode Exit fullscreen mode

8. Table Management

Create Table

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE,
    age INT,
    created_at TIMESTAMPTZ DEFAULT NOW()
);
Enter fullscreen mode Exit fullscreen mode

Alter Table

ALTER TABLE users ADD COLUMN is_active BOOLEAN DEFAULT true;
Enter fullscreen mode Exit fullscreen mode

Drop Table

DROP TABLE users;
Enter fullscreen mode Exit fullscreen mode

9. CRUD Operations

Insert

INSERT INTO users (name, email, age)
VALUES ('Ali', 'ali@example.com', 25);
Enter fullscreen mode Exit fullscreen mode

Select

SELECT * FROM users;
Enter fullscreen mode Exit fullscreen mode

Update

UPDATE users
SET age = 26
WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

Delete

DELETE FROM users WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

10. Constraints

PRIMARY KEY
FOREIGN KEY
UNIQUE
NOT NULL
CHECK
Enter fullscreen mode Exit fullscreen mode

Example:

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id INT REFERENCES users(id),
    total NUMERIC CHECK (total > 0)
);
Enter fullscreen mode Exit fullscreen mode

11. Indexing (Critical for Performance)

B-Tree (Default)

CREATE INDEX idx_users_email ON users(email);
Enter fullscreen mode Exit fullscreen mode

Hash

CREATE INDEX idx_hash ON users USING HASH(email);
Enter fullscreen mode Exit fullscreen mode

GIN (JSON, Arrays)

CREATE INDEX idx_json ON products USING GIN(details);
Enter fullscreen mode Exit fullscreen mode

GiST (Geospatial)

CREATE INDEX idx_geo ON locations USING GIST(point);
Enter fullscreen mode Exit fullscreen mode

12. Joins

SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id;
Enter fullscreen mode Exit fullscreen mode

Types:

  • INNER
  • LEFT
  • RIGHT
  • FULL

13. Aggregations & Grouping

SELECT user_id, SUM(total)
FROM orders
GROUP BY user_id
HAVING SUM(total) > 100;
Enter fullscreen mode Exit fullscreen mode

14. Subqueries & CTEs

Subquery

SELECT * FROM users
WHERE id IN (SELECT user_id FROM orders);
Enter fullscreen mode Exit fullscreen mode

CTE

WITH total_orders AS (
  SELECT user_id, SUM(total) AS total
  FROM orders
  GROUP BY user_id
)
SELECT * FROM total_orders WHERE total > 100;
Enter fullscreen mode Exit fullscreen mode

15. Transactions

BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;
Enter fullscreen mode Exit fullscreen mode

Rollback:

ROLLBACK;
Enter fullscreen mode Exit fullscreen mode

16. Functions & Stored Procedures

Function

CREATE FUNCTION add(a INT, b INT)
RETURNS INT AS $$
BEGIN
  RETURN a + b;
END;
$$ LANGUAGE plpgsql;
Enter fullscreen mode Exit fullscreen mode

Call

SELECT add(5, 3);
Enter fullscreen mode Exit fullscreen mode

17. Triggers

CREATE OR REPLACE FUNCTION log_update()
RETURNS TRIGGER AS $$
BEGIN
  INSERT INTO logs VALUES (NEW.id, NOW());
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Enter fullscreen mode Exit fullscreen mode
CREATE TRIGGER after_update
AFTER UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION log_update();
Enter fullscreen mode Exit fullscreen mode

18. Views & Materialized Views

View

CREATE VIEW active_users AS
SELECT * FROM users WHERE is_active = true;
Enter fullscreen mode Exit fullscreen mode

Materialized View

CREATE MATERIALIZED VIEW stats AS
SELECT COUNT(*) FROM users;
Enter fullscreen mode Exit fullscreen mode

Refresh:

REFRESH MATERIALIZED VIEW stats;
Enter fullscreen mode Exit fullscreen mode

19. JSON & JSONB Operations

SELECT data->>'name' FROM products;
Enter fullscreen mode Exit fullscreen mode
SELECT data @> '{"price":100}' FROM products;
Enter fullscreen mode Exit fullscreen mode

20. Full-Text Search

SELECT *
FROM articles
WHERE to_tsvector(content) @@ to_tsquery('database');
Enter fullscreen mode Exit fullscreen mode

21. Security & Roles

Create Role

CREATE ROLE admin LOGIN PASSWORD 'secret';
Enter fullscreen mode Exit fullscreen mode

Grant

GRANT ALL PRIVILEGES ON DATABASE mydb TO admin;
Enter fullscreen mode Exit fullscreen mode

22. Replication & High Availability

  • Streaming Replication
  • Logical Replication
  • Hot Standby
  • Failover tools (Patroni, PgBouncer)

23. Extensions

CREATE EXTENSION pg_stat_statements;
CREATE EXTENSION postgis;
Enter fullscreen mode Exit fullscreen mode

24. Performance Tuning

Key settings:

  • shared_buffers
  • work_mem
  • maintenance_work_mem
  • effective_cache_size

Analyze query:

EXPLAIN ANALYZE SELECT * FROM users;
Enter fullscreen mode Exit fullscreen mode

25. Backup & Restore

Backup

pg_dump mydb > backup.sql
Enter fullscreen mode Exit fullscreen mode

Restore

psql mydb < backup.sql
Enter fullscreen mode Exit fullscreen mode

26. Why PostgreSQL Is Special

  • Advanced SQL compliance
  • True ACID transactions
  • MVCC without locks
  • JSON + relational hybrid
  • Industrial-grade reliability
  • Infinite extensibility

Final Thoughts

PostgreSQL is not just a database—it is a complete data platform.
If you master PostgreSQL internals, SQL syntax, and architecture, you can build banking systems, social networks, analytics engines, and distributed systems with confidence.

Top comments (0)