postgresql-database-engineering
Comprehensive PostgreSQL database engineering skill covering indexing strategies, query optimization, performance tuning, partitioning, replication, backup and recovery, high availability, and production database management. Master advanced PostgreSQL features including MVCC, VACUUM operations, connection pooling, monitoring, and scalability patterns.
What this skill does
# PostgreSQL Database Engineering A comprehensive skill for professional PostgreSQL database engineering, covering everything from query optimization and indexing strategies to high availability, replication, and production database management. This skill enables you to design, optimize, and maintain high-performance PostgreSQL databases at scale. ## When to Use This Skill Use this skill when: - Designing database schemas for high-performance applications - Optimizing slow queries and improving database performance - Implementing indexing strategies for complex query patterns - Setting up partitioning for large tables (100M+ rows) - Configuring streaming replication and high availability - Tuning PostgreSQL configuration for production workloads - Implementing backup and recovery procedures - Debugging performance issues and query bottlenecks - Setting up connection pooling with pgBouncer or PgPool - Monitoring database health and performance metrics - Planning database migrations and schema changes - Implementing database security and access controls - Scaling PostgreSQL databases horizontally or vertically - Managing VACUUM operations and database maintenance - Setting up logical replication for data distribution ## Core Concepts ### PostgreSQL Architecture PostgreSQL uses a process-based architecture with several key components: - **Postmaster Process**: Main server process that manages connections - **Backend Processes**: One per client connection, handles queries - **Shared Memory**: Shared buffers, WAL buffers, lock tables - **Background Workers**: Autovacuum, checkpointer, WAL writer, statistics collector - **Write-Ahead Log (WAL)**: Transaction log for durability and replication - **Storage Layer**: TOAST for large values, FSM for free space, VM for visibility ### MVCC (Multi-Version Concurrency Control) PostgreSQL's foundational concurrency mechanism: - **Snapshots**: Each transaction sees a consistent snapshot of data - **Tuple Versions**: Multiple row versions coexist for concurrent access - **Transaction IDs**: xmin (creating transaction), xmax (deleting transaction) - **Visibility Rules**: Determines which row versions are visible to transactions - **VACUUM**: Reclaims space from dead tuples and prevents transaction wraparound - **FREEZE**: Marks old rows as visible to all transactions **Key Implications:** - No read locks - readers never block writers - Writers never block readers - Updates create new row versions - Regular VACUUM is essential - Dead tuples accumulate until vacuumed ### Transaction Isolation Levels PostgreSQL supports four isolation levels: 1. **Read Uncommitted**: Treated as Read Committed in PostgreSQL 2. **Read Committed** (default): Sees committed data at statement start 3. **Repeatable Read**: Sees snapshot from transaction start 4. **Serializable**: True serializable isolation with SSI **Choosing Isolation:** - Read Committed: Most applications, best performance - Repeatable Read: Reports, analytics needing consistency - Serializable: Financial transactions, critical consistency needs ### Index Types PostgreSQL offers multiple index types for different use cases: #### 1. B-Tree (Default) - **Use for**: Equality, range queries, sorting - **Supports**: <, <=, =, >=, >, BETWEEN, IN, IS NULL - **Best for**: Most general-purpose indexing - **Example**: Primary keys, foreign keys, timestamps #### 2. Hash - **Use for**: Equality comparisons only - **Supports**: = operator - **Best for**: Large tables with equality lookups - **Limitation**: Not WAL-logged before PG 10, no range queries #### 3. GiST (Generalized Search Tree) - **Use for**: Geometric data, full-text search, custom types - **Supports**: Overlaps, contains, nearest neighbor - **Best for**: Spatial data, ranges, full-text search - **Example**: PostGIS geometries, tsvector, ranges #### 4. GIN (Generalized Inverted Index) - **Use for**: Multi-valued columns (arrays, JSONB, full-text) - **Supports**: Contains, exists operators - **Best for**: JSONB queries, array operations, full-text search - **Tradeoff**: Slower updates, faster queries #### 5. BRIN (Block Range Index) - **Use for**: Very large tables with natural ordering - **Supports**: Range queries on sorted data - **Best for**: Time-series data, append-only tables - **Advantage**: Tiny index size, scales to billions of rows #### 6. SP-GiST (Space-Partitioned GiST) - **Use for**: Non-balanced data structures - **Supports**: Points, ranges, IP addresses - **Best for**: Quadtrees, k-d trees, radix trees ### Query Planning and Optimization PostgreSQL's query planner determines execution strategies: **Planner Components:** - **Statistics**: Table and column statistics for cardinality estimation - **Cost Model**: CPU, I/O, and memory cost estimation - **Plan Types**: Sequential scan, index scan, bitmap scan, joins - **Join Methods**: Nested loop, hash join, merge join - **Optimization**: Query rewriting, predicate pushdown, join reordering **Key Statistics:** - `n_distinct`: Number of distinct values (for selectivity) - `correlation`: Physical row ordering correlation - `most_common_vals`: MCV list for skewed distributions - `histogram_bounds`: Value distribution histogram **Understanding EXPLAIN:** - **Cost**: Startup cost .. total cost (arbitrary units) - **Rows**: Estimated row count - **Width**: Average row size in bytes - **Actual Time**: Real execution time (with ANALYZE) - **Loops**: Number of times node executed ### Partitioning Strategies Table partitioning for managing large datasets: #### Range Partitioning - **Use for**: Time-series data, sequential values - **Example**: Partition by date ranges (daily, monthly, yearly) - **Benefit**: Easy data lifecycle management, faster queries #### List Partitioning - **Use for**: Discrete categorical values - **Example**: Partition by country, region, status - **Benefit**: Logical data separation, partition pruning #### Hash Partitioning - **Use for**: Even data distribution - **Example**: Partition by hash(user_id) - **Benefit**: Balanced partition sizes, parallel queries **Partition Pruning:** - Planner eliminates irrelevant partitions - Drastically reduces query scope - Essential for partition performance **Partition-Wise Operations:** - Partition-wise joins: Join matching partitions directly - Partition-wise aggregation: Aggregate within partitions - Parallel partition processing ### Replication and High Availability PostgreSQL replication options: #### Streaming Replication (Physical) - **Type**: Binary WAL streaming to standby servers - **Modes**: Asynchronous, synchronous, quorum-based - **Use for**: High availability, read scalability - **Failover**: Automatic with tools like Patroni, repmgr **Synchronous vs Asynchronous:** - Synchronous: Zero data loss, higher latency - Asynchronous: Low latency, potential data loss - Quorum: Balance between safety and performance #### Logical Replication - **Type**: Row-level change stream - **Use for**: Selective replication, upgrades, multi-master - **Benefit**: Replicate specific tables, cross-version - **Limitation**: No DDL replication, overhead #### Cascading Replication - Standbys replicate from other standbys - Reduces load on primary - Geographic distribution ### Connection Pooling Managing database connections efficiently: #### pgBouncer - **Type**: Lightweight connection pooler - **Modes**: Session, transaction, statement pooling - **Use for**: High connection count applications - **Benefit**: Reduced connection overhead, resource limits **Pooling Modes:** - **Session**: Client connects for entire session - **Transaction**: Connection per transaction - **Statement**: Connection per statement (rarely used) #### PgPool-II - **Type**: Feature-rich middleware - **Features**: Connection pooling, load balancing, query caching - **Use for**: Read/write splitting, connection management - **Benefit**: Advanced routing, in-memory cache ### VACUUM and Maintenance Criti
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.