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.

referencespartitioning.md

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

Table Partitioning

Plan partitioning upfront for tables expected to grow large. Retrofitting later requires a migration.

When to Partition

Partitioning benefits maintenance (vacuum, index builds) and data retention more than pure query speed.

Table Type Size Threshold Row Threshold
General tables >100 GB (or >RAM) >20M rows
Time-series / logs >50 GB >10M rows

Use the lower thresholds for append-heavy, time-ordered data with retention needs (logs, events, audit trails, metrics).

Range Partitioning (Most Common)

-- EXAMPLE
CREATE TABLE event (
  id BIGINT GENERATED ALWAYS AS IDENTITY,
  event_type TEXT NOT NULL,
  payload JSONB,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  PRIMARY KEY (id, created_at) -- Partition key MUST be part of PK
) PARTITION BY RANGE (created_at);

CREATE TABLE event_2026_01 PARTITION OF event
  FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');

CREATE TABLE event_2026_02 PARTITION OF event
  FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');

List Partitioning

Useful for partitioning by region, tenant, or category:

-- EXAMPLE
CREATE TABLE order (
  id BIGINT GENERATED ALWAYS AS IDENTITY,
  region TEXT NOT NULL,
  total NUMERIC(10,2),
  PRIMARY KEY (id, region) -- Partition key MUST be part of PK
) PARTITION BY LIST (region);

CREATE TABLE order_us PARTITION OF order FOR VALUES IN ('us');
CREATE TABLE order_eu PARTITION OF order FOR VALUES IN ('eu');
CREATE TABLE order_default PARTITION OF order DEFAULT;  -- catches unmatched values

Partition Management

  • Use pg_partman (extension) to automate partition creation and cleanup.
  • Use DETACH PARTITION to remove a partition while retaining it as a standalone table (e.g., for archiving).
  • Use DETACH PARTITION ... CONCURRENTLY (PG 14+) to avoid ACCESS EXCLUSIVE locks on the parent table.
  • Drop old partitions for data retention instead of DELETE to avoid vacuum overhead and bloat.
  • Create future partitions ahead of time to avoid insert failures.
  • Always confirm with a human before detaching or dropping partitions. These are destructive actions — detaching removes data from the partitioned table, and dropping permanently deletes the data.
-- DESTRUCTIVE: confirm with a human before executing
ALTER TABLE event DETACH PARTITION event_2025_01 CONCURRENTLY;
DROP TABLE event_2025_01;

Guidelines & Limitations

  • Primary Keys: Partition key columns MUST be included in the PRIMARY KEY and any UNIQUE constraints.
  • Global Uniqueness: Global unique constraints on non-partition columns are NOT supported.
  • Indexes: Indexes defined on the parent are automatically created on all partitions (and future ones).
  • Pruning: Ensure queries filter by the partition key to enable "partition pruning" (skipping unrelated partitions).

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.