All skills
planetscale avatar

/postgres

@2b8cda6 official

PostgreSQL best practices, query optimization, connection troubleshooting, and performance improvement. Load when working with Postgres databases.

Use this Skill: https://skilld.dev/gh/planetscale/database-skills/postgres

This session only. Nothing lands on disk.

referencesmemory-management-ops.md

≈696 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Memory Architecture and OOM Prevention

Memory Areas

  • Shared memory: shared_buffers — main data cache, all processes, requires restart to change.
  • Private per backend: work_mem (sorts/hashes/joins, per-operation); maintenance_work_mem (VACUUM, CREATE INDEX, ALTER TABLE ADD FOREIGN KEY); temp_buffers (8MB default).
  • Planner hint only: effective_cache_size is NOT allocated — set to ~50–75% of total RAM.
  • Hash multiplier: hash_mem_multiplier (default 2.0) means hash ops use up to 2× work_mem.

Memory Multiplication Danger

Maximum potential: work_mem × operations_per_query × (parallel_workers + 1) × connections (leader participates by default via parallel_leader_participation = on; hash operations use up to hash_mem_multiplier × work_mem, default 2.0). Example: 128MB work_mem, 3 ops (2 sorts + 1 hash join), 2 parallel workers, 100 connections → 2 sorts at 128MB = 256MB, 1 hash join at 128MB × 2.0 = 256MB, per process = 512MB, × 3 processes (2 workers + leader) = 1536MB/query, × 100 connections = ~150GB worst case. This case is rare. Not all queries hit limits at once, but high concurrency + large datasets approach it. This is a common cause of OOM in containerized/Kubernetes deployments. Plan capacity with a 1.5–2× safety margin.

OS Page Cache (Double Buffering)

Data exists in both shared_buffers and OS page cache. A miss in shared_buffers can still hit OS cache (avoiding disk I/O). Extremely large shared_buffers can hurt performance: less OS cache, slower startup, heavier checkpoints. Optimal split depends on workload (OLTP vs OLAP).

OOM Prevention

  • Implement connection pooling to reduce total backend count.
  • Reduce work_mem globally; use per-session overrides for heavy queries only.
  • Lower max_parallel_workers_per_gather in high-concurrency systems.
  • Set statement_timeout to kill runaway queries.
  • Monitor: dmesg -T | grep "killed process" and temp_blks_written in pg_stat_statements.

Operational Rules

  • Tune per-session first, global last.
  • Suspect OOM when memory spikes during high concurrency, dashboards, or large batch jobs.
  • Increase memory only after confirming spill behavior (temp_blks_written > 0).
  • maintenance_work_mem can be set much higher (1–2GB) — fewer processes use it. Cap autovacuum with autovacuum_work_mem to avoid autovacuum_max_workers × maintenance_work_mem memory spikes.
  • shared_buffers change requires full restart; work_mem is per-session changeable.

Source: SKILL.md on GitHub

1 alert17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides comprehensive documentation, best practices, and diagnostic SQL queries for PostgreSQL and PlanetScale-specific features. No security issues were identified.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    18/23 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 2b8cda6. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Activeupdated 7 months ago
metadata
{
  "author": "planetscale",
  "version": "1.0.0"
}
  • Database
  • postgres
  • schema-design
  • indexing
  • query-optimization
  • replication
  • monitoring
  • backup-recovery
  • planetscale

README badge

README badge for planetscale/database-skills/postgres

Provides Postgres best practices, query optimization guidance, indexing strategies, and schema design patterns. Includes PlanetScale-specific connection pooling, monitoring, and CLI reference for hosted Postgres deployments.

Generated from the current SKILL.md.

Does this skill cover PlanetScale-specific features like connection pooling?
Yes. The skill includes references for PlanetScale connection pooling, PgBouncer configuration, the pscale CLI, and PlanetScale Insights for slow query analysis.
What topics does this skill cover?
Schema design, indexing, query optimization, partitioning, MVCC and transactions, replication, WAL operations, monitoring, backup/recovery, and PlanetScale-specific connection and deployment workflows.
Does this skill help with query performance troubleshooting?
Yes. It includes guidance on query patterns, index optimization, MVCC transaction isolation, and monitoring via pg_stat views and pg_stat_statements.
Can I use this skill if I self-host Postgres instead of PlanetScale?
Yes. The skill covers generic Postgres best practices and operations. PlanetScale-specific references are optional and only used if you choose PlanetScale for hosting.

Generated from the current SKILL.md. These answers refresh after source changes.