Manage Apple Reminders via the `remindctl` CLI on macOS (list, add, edit, complete, delete)....
npx skills add itechmeat/llm-code --skill "postgresql"
Install specific skill from multi-skill repository
# Description
PostgreSQL best practices: multi-tenancy with RLS, schema design, Alembic migrations, async SQLAlchemy, and query optimization.
# SKILL.md
name: postgresql
description: "PostgreSQL best practices: multi-tenancy with RLS, schema design, Alembic migrations, async SQLAlchemy, and query optimization."
version: "18.1"
release_date: "2025-11-13"
PostgreSQL
When to use
- Designing or changing multi-tenant tables with Row-Level Security (RLS)
- Debugging tenant isolation issues
- Adding/changing Alembic migrations for schema, RLS policies, or indexes
- Writing tests that validate RLS isolation
- Configuring PostgreSQL authentication, replication, or tuning
RLS Multi-tenancy Pattern
Non-negotiables
- RLS context is mandatory for any tenant-scoped query
- Context must be set inside the same transaction as the queries
- No fallbacks for tenant ID (fail fast if missing)
- Async-only DB access when using async frameworks
Setting RLS Context
RLS works only if the current transaction has the context set:
SET LOCAL app.current_tenant_id = '<tenant_uuid>';
Must run before the first tenant-scoped query in that transaction.
Common Failure Modes
- Setting
SET LOCAL ...after the firstselect() - Setting the context in one session, then querying in another
- Running queries outside the expected transaction scope
Typical RLS Policy
ALTER TABLE some_table ENABLE ROW LEVEL SECURITY;
CREATE POLICY some_table_tenant_isolation
ON some_table
USING (tenant_id = current_setting('app.current_tenant_id', true)::uuid);
Multi-tenant Table Checklist
- Tenant ID column is UUID
- FK to tenants table with
ON DELETE CASCADE - Indexes aligned with access patterns (usually tenant_id first)
- PostgreSQL does not auto-index FK columns β add explicit indexes
- UNIQUE allows multiple NULLs unless using
NULLS NOT DISTINCT(PG15+) - RLS is enabled and policies exist
- Application code sets RLS context at transaction start
Alembic Migrations Checklist
- Add/modify schema (columns, constraints, FKs)
- Create/update indexes
- Enable RLS and create/adjust policies
- Add verification (tests) for isolation
- Provide a real downgrade (no stubs)
RLS Isolation Testing Recipe
Goal:
- Data for tenant A is visible to tenant A
- Data for tenant A is NOT visible to tenant B
Canonical flow:
- Setup data through an admin session (RLS bypass) for tenant A and B
- Assert via an RLS session:
- set context to tenant A β sees only tenant A data
- set context to tenant B β does not see tenant A data
Destructive Operations Safety
Hard rules:
- Never run
DELETEwithout a narrowWHEREtargeting specific data - Never run
TRUNCATE/DROPwithout explicit confirmation
Pre-flight before destructive actions:
- Confirm exact target (tables / IDs / date range)
- Run a
SELECT/row count first and show results - Ask for final confirmation, then execute
References
Schema & Design
- table-design.md β Data types, constraints, indexing, partitioning, JSONB, safe schema evolution
- charset-encoding.md β Character sets, encoding, collation, ICU, locale settings
Authentication
- authentication.md β pg_hba.conf, SCRAM-SHA-256, md5, peer, cert, LDAP, GSSAPI
- authentication-oauth.md β OAuth 2.0 (PostgreSQL 18+), SASL OAUTHBEARER, validators
- user-management.md β CREATE/ALTER/DROP ROLE, membership, GRANT/REVOKE, predefined roles
Runtime Configuration
- connection-settings.md β listen_addresses, max_connections, SSL, TCP keepalives
- query-tuning.md β Planner settings, work_mem, parallel query, cost constants
- replication.md β Streaming replication, WAL, synchronous commit, logical replication
- vacuum.md β Autovacuum, vacuum cost model, freeze ages, per-table tuning
- error-handling.md β exit_on_error, restart_after_crash, data_sync_retry
Internals
- internals.md β Query processing pipeline, parser/rewriter/planner/executor, system catalogs, wire protocol, access methods
- protocol.md β Wire protocol v3.2: message format, startup, auth, query, COPY, replication
See also
- sql-expert β Query patterns, EXPLAIN workflow, optimization
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.