nimbuscode.dev/technologies/postgresql
C:\> cat TECHNOLOGIES/POSTGRESQL.md
Loading PostgreSQL documentation...

PostgreSQL

Object-Relational Database Management System

1. Introduction

PostgreSQL (often called "Postgres") is a powerful, open-source object-relational database system with over 30 years of active development. It has earned a strong reputation for reliability, feature robustness, and performance.

Known for its SQL compliance, extensibility, and advanced features, PostgreSQL goes beyond traditional relational database functionality to provide robust support for complex data types, full-text search, geographic objects, and more. It can handle workloads ranging from small single-machine applications to large web services with many concurrent users.

2. Syntax Examples

Basic SQL Operations
-- Create a table with PostgreSQL data types
CREATE TABLE products (
    product_id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    price NUMERIC(10, 2) NOT NULL,
    description TEXT,
    weight REAL,
    in_stock BOOLEAN DEFAULT true,
    created_at TIMESTAMPTZ DEFAULT now(),
    tags TEXT[]
);

-- Insert data with array
INSERT INTO products (name, price, description, weight, tags)
VALUES 
  ('Ergonomic Keyboard', 129.99, 'Comfortable typing experience', 0.9, 
   ARRAY['electronics', 'office', 'ergonomic']),
  ('Wireless Mouse', 45.50, 'High precision wireless mouse', 0.1, 
   ARRAY['electronics', 'computer accessories']);

-- Query with array operations
SELECT * FROM products
WHERE 'electronics' = ANY(tags)
ORDER BY price DESC;
PostgreSQL-Specific Features
-- JSON data type and operations
CREATE TABLE user_profiles (
    user_id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    preferences JSONB
);

INSERT INTO user_profiles (name, preferences)
VALUES 
  ('Alice', '{"theme": "dark", "notifications": true, "language": "en"}'),
  ('Bob', '{"theme": "light", "notifications": false, "language": "fr"}');

-- Query JSON data
SELECT name, preferences->'theme' AS theme
FROM user_profiles
WHERE preferences->>'language' = 'en';

-- Update JSON data
UPDATE user_profiles
SET preferences = preferences || '{"fontSize": "large"}'::jsonb
WHERE name = 'Alice';

-- Common Table Expressions (CTE)
WITH expensive_products AS (
    SELECT * FROM products
    WHERE price > 100
)
SELECT * FROM expensive_products
WHERE 'office' = ANY(tags);
Advanced Features: Inheritance, Functions, Extensions
-- Table inheritance
CREATE TABLE vehicles (
    vehicle_id SERIAL PRIMARY KEY,
    make VARCHAR(50),
    model VARCHAR(50),
    year INTEGER
);

CREATE TABLE cars (INHERITS (vehicles)) (
    doors INTEGER,
    body_type VARCHAR(20)
);

-- Custom function
CREATE OR REPLACE FUNCTION get_product_info(p_id INTEGER)
RETURNS TABLE(name VARCHAR, price NUMERIC, tag_count INTEGER) AS $$
BEGIN
    RETURN QUERY
    SELECT 
        p.name, 
        p.price, 
        array_length(p.tags, 1) AS tag_count
    FROM products p
    WHERE p.product_id = p_id;
END;
$$ LANGUAGE plpgsql;

-- Using PostGIS extension for geographic data
CREATE EXTENSION postgis;

CREATE TABLE stores (
    store_id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    location GEOGRAPHY(POINT)
);

INSERT INTO stores (name, location)
VALUES ('Downtown Store', ST_MakePoint(-122.4194, 37.7749)::geography);

-- Find stores within 10km
SELECT name, ST_Distance(location, ST_MakePoint(-122.4, 37.8)::geography) AS distance
FROM stores
WHERE ST_DWithin(location, ST_MakePoint(-122.4, 37.8)::geography, 10000);

3. Main Uses

PostgreSQL is used in various contexts:

  • Web applications requiring robust data integrity
  • Geographic Information Systems (GIS) with PostGIS extension
  • Data warehousing and business intelligence
  • Financial services and banking applications
  • Scientific and research databases
  • Enterprise applications with complex data models
  • Government and public sector systems
  • Applications requiring complex queries and analytics

4. Pros and Cons

Advantages

  • Strong standards compliance (SQL standard)
  • Advanced data types (arrays, JSON, hstore, geometric)
  • Robust transactional support with full ACID compliance
  • Extensibility with custom functions, operators, and types
  • Mature replication and high availability options
  • Excellent data integrity and constraint features
  • Active community and well-documented

Limitations

  • More resource-intensive than some alternatives
  • Steeper learning curve for advanced features
  • More complex to set up and administer
  • Less "out-of-the-box" tooling than some commercial DBs
  • Table locks for some operations can impact concurrency
  • Replication configuration can be complex
  • Can be slower for simple read-heavy workloads

5. PostgreSQL Evolution

PostgreSQL has a rich development history:

  • 1986-1994 - Initial development at UC Berkeley as "Postgres"
  • 1996 - Postgres95 renamed to PostgreSQL (version 6.0)
  • 2005 - PostgreSQL 8.0 with native Windows support
  • 2010 - PostgreSQL 9.0 with built-in replication
  • 2014 - PostgreSQL 9.4 with JSONB data type
  • 2016 - PostgreSQL 9.6 with parallel query execution
  • 2017 - PostgreSQL 10 with logical replication, improved partitioning
  • 2018 - PostgreSQL 11 with increased partitioning capabilities
  • 2019 - PostgreSQL 12 with performance improvements for indexes
  • 2020 - PostgreSQL 13 with improved indexing and sorting
  • 2021 - PostgreSQL 14 with performance improvements and MVCC enhancements
  • 2022-2025 - PostgreSQL 15-18 with continued performance and security enhancements

PostgreSQL continues its development with annual major releases that focus on performance, features, and security while maintaining high backward compatibility.

6. Learning Resources

Here are some excellent resources for learning PostgreSQL:

7. Related Technologies

Technologies related to PostgreSQL:

C:\> cd ../