PostgreSQL Overview¶
PostgreSQL (commonly known as PG) is an open-source Object-Relational Database Management System (ORDBMS), renowned for its feature completeness, standards compliance, and strong extensibility — often called "the world's most advanced open-source database".
Key Features¶
- MVCC (Multi-Version Concurrency Control): reads never block writes, ideal for high-concurrency workloads
- Rich Data Types: native support for JSON/JSONB, arrays, range types, geometric types, and custom composite types
- Strong Extensibility: extended via plugins such as PostGIS (GIS) and TimescaleDB (time-series)
- Strict SQL Standard: leading compliance with the SQL:2016 standard among most databases
- Multiple Index Types: B-Tree, Hash, GiST, SP-GiST, GIN, BRIN
- Transactions & Isolation: full ACID support, including Serializable isolation (SSI)
PostgreSQL vs MySQL¶
| Aspect | PostgreSQL | MySQL (InnoDB) |
|---|---|---|
| Positioning | Object-relational ORDBMS | Pure relational RDBMS |
| MVCC Implementation | Tuple versioning (xmin/xmax), old versions cleaned by VACUUM | Undo Log rollback segments |
| JSON Support | JSONB binary storage, indexable via GIN | JSON type only, limited indexing |
| Complex Queries | CTEs, window functions, parallel query — earlier and more mature | Gradually added in 8.0+ |
| Replication | Physical + logical replication | Binlog master-slave replication |
| Storage Engines | Single engine (tables can attach extensions) | Multiple engines (InnoDB/MyISAM...) |
| Connection Model | One process per connection, heavier | One thread per connection, lightweight |
| Use Cases | Complex queries, GIS, analytics, mixed workloads | Simple high-concurrency reads/writes, web applications |
Core Concepts¶
- Instance: a postgres process cluster + shared memory, managing one Data Directory
- Database: a physically isolated unit within the data directory; must be specified at connection
- Schema: a named namespace within a database (default
public), used to organize tables, views, and functions - Tablespace: specifies the filesystem location for storing data and indexes
- WAL (Write-Ahead Log): log written before data changes — the foundation for crash recovery and replication
Process model:
[Client] --connection--> [postmaster] --fork--> [backend process]
|
[WAL Writer] [Bgwriter] [Autovacuum] [Checkpointer]
Common Operations¶
-- Connection & basics (psql meta-commands)
\c dbname -- switch database
\dt -- list tables
\d tablename -- describe table structure
\df -- list functions
\l -- list all databases
-- Creation examples
CREATE TABLE t (
id bigserial PRIMARY KEY,
tags text[], -- array type
attrs jsonb, -- JSONB
price numeric CHECK (price >= 0)
);
CREATE INDEX idx_attrs ON t USING gin(attrs);
-- JSONB query
SELECT * FROM t WHERE attrs @> '{"type": "vip"}';
-- index query
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'tablename';
Useful Tools¶
| Tool | Purpose |
|---|---|
psql |
Official command-line client |
pg_dump / pg_restore |
Logical backup and restore |
pg_basebackup |
Physical online backup |
pgAdmin / DBX |
GUI management tools |
explain / explain analyze |
Query plan analysis |
pg_stat_statements |
SQL execution statistics (performance tuning) |