golang-gin-psql-dba

PostgreSQL DBA and architect skill for Go Gin APIs. Covers schema design decisions, migration impact analysis, index strategy, query optimization, partitioning, connection pool sizing, and the PostgreSQL extension ecosystem (ParadeDB, pgvector, PostGIS, TimescaleDB). Use when designing schemas, analyzing migration safety, choosing indexes, optimizing queries, selecting extensions, or making any PostgreSQL architecture decision. This is the 'how to think' complement to golang-gin-database's 'how to connect.' Also activate when the user mentions EXPLAIN ANALYZE, lock levels, zero-downtime migration, full-text search, vector embeddings, geospatial queries, time-series data, or database performance tuning.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "golang-gin-psql-dba" with this command: npx skills add henriqueatila/golang-gin-best-practices/henriqueatila-golang-gin-best-practices-golang-gin-psql-dba

golang-gin-psql-dba — PostgreSQL DBA / Architect

Make PostgreSQL architecture decisions for Go Gin APIs. Schema design, migration safety, index strategy, query optimization, and extension selection. Uses raw SQL via sqlx — for ORM patterns, see the golang-gin-database skill.

When to Use

  • Designing a new PostgreSQL schema (tables, types, constraints, naming)
  • Evaluating whether an ALTER TABLE is safe to run on a live database
  • Choosing the right index type for a query pattern
  • Reading EXPLAIN ANALYZE output and fixing slow queries
  • Sizing connection pools or tuning autovacuum
  • Selecting a PostgreSQL extension (search, vectors, geospatial, time-series)
  • Deciding on partitioning strategy

golang-gin-psql-dba vs golang-gin-database: golang-gin-database covers GORM/sqlx wiring, repository pattern, and migrations tooling (golang-migrate). golang-gin-psql-dba covers the PostgreSQL decisions behind those patterns — what data types to pick, which index to create, how to ALTER TABLE safely, and when to reach for an extension.

Schema Design Quick Rules

RuleDoDon't
Primary keysid UUID DEFAULT gen_random_uuid() or id BIGINT GENERATED ALWAYS AS IDENTITYSERIAL (legacy)
TimestampsTIMESTAMPTZ with DEFAULT now()TIMESTAMP (no timezone)
BooleansNOT NULL DEFAULT falseNullable booleans (three-valued logic)
MoneyNUMERIC(19,4)FLOAT, REAL, DOUBLE PRECISION
Status/enumTEXT + CHECK constraint, or PostgreSQL ENUM typeFree-text strings
Namingsnake_case, plural table names (users), singular columnscamelCase, PascalCase
Soft deletedeleted_at TIMESTAMPTZ (nullable)Boolean is_deleted
Foreign keysAlways ON DELETE clause (CASCADE, SET NULL, or RESTRICT)Omitting ON DELETE

For complete schema design patterns (normalization, multi-tenancy, audit trails): see references/schema-design.md.

Index Selection Decision Tree

Query PatternIndex TypeExample
Equality (=), range (<, >, BETWEEN)B-tree (default)CREATE INDEX idx_users_email ON users (email)
Full-text search (@@, tsvector)GINCREATE INDEX idx_posts_search ON posts USING gin (search_vector)
JSONB containment (@>), array overlap (&&)GINCREATE INDEX idx_meta ON products USING gin (metadata jsonb_path_ops)
Geometric / spatial / range typesGiSTCREATE INDEX idx_loc ON stores USING gist (location)
Large table, monotonic column (timestamp, id)BRINCREATE INDEX idx_events_ts ON events USING brin (created_at)
Trigram similarity (%, LIKE '%foo%')GIN + pg_trgmCREATE INDEX idx_name_trgm ON users USING gin (name gin_trgm_ops)
High-dimensional vectors (embeddings)HNSW or IVFFlat (pgvector)CREATE INDEX idx_embed ON items USING hnsw (embedding vector_cosine_ops)
IP ranges, text rangesSP-GiSTCREATE INDEX idx_ip ON logs USING spgist (ip_range)

Rules of thumb:

  • Default to B-tree. Only switch when B-tree cannot serve the query pattern.
  • Partial indexes (WHERE active = true) reduce size and maintenance for filtered queries.
  • Covering indexes (INCLUDE (col)) let the planner do index-only scans.
  • Never index columns with very low cardinality (e.g., boolean) alone — combine in composite indexes.

For deep dive on each index type with EXPLAIN ANALYZE: see references/index-strategy.md.

Migration Safety Quick Guide

Every ALTER TABLE acquires a lock. The lock level determines whether reads and writes are blocked.

OperationLock LevelSafe Online?Notes
ADD COLUMN (nullable, no default)AccessExclusiveLockYes — fast metadata changeSafest column addition
ADD COLUMN ... DEFAULT x (PG 11+)AccessExclusiveLockYes — fast since PG 11Pre-PG11: rewrites table
ADD COLUMN ... NOT NULL DEFAULT x (PG 11+)AccessExclusiveLockYes — fast since PG 11Same as above
DROP COLUMNAccessExclusiveLockYes — fast metadata markData remains until VACUUM
ALTER COLUMN SET NOT NULLAccessExclusiveLockSlow — full table scanUse NOT VALID constraint instead
ALTER COLUMN TYPEAccessExclusiveLockSlow — full table rewriteCreate new column + backfill instead
ADD CONSTRAINT ... FOREIGN KEYShareRowExclusiveLockSlow — validates all rowsUse NOT VALID then VALIDATE separately
CREATE INDEXShareLockBlocks writesUse CONCURRENTLY instead
CREATE INDEX CONCURRENTLYShareUpdateExclusiveLockYesTakes longer but doesn't block
DROP INDEXAccessExclusiveLockBlocks allUse CONCURRENTLY

Zero-downtime pattern for NOT NULL:

-- Step 1: Add constraint as NOT VALID (fast, no scan)
ALTER TABLE users ADD CONSTRAINT users_email_not_null
    CHECK (email IS NOT NULL) NOT VALID;

-- Step 2: Validate in a separate transaction (scans, but allows writes)
ALTER TABLE users VALIDATE CONSTRAINT users_email_not_null;

Always set lock_timeout in migration scripts:

SET lock_timeout = '5s';
-- If the lock can't be acquired in 5s, the migration fails instead of blocking all traffic.

For complete zero-downtime patterns (column rename, type change, backfill): see references/migration-impact-analysis.md.

Extension Selection Guide

NeedExtensionMaturityReference
Full-text BM25 searchParadeDB (pg_search)Growing (production-ready)paradedb-full-text-search.md
Vector similarity (embeddings)pgvectorMaturepgvector-embeddings.md
Geospatial queriesPostGISVery maturepostgis-geospatial.md
Time-series dataTimescaleDBMaturetimescaledb-time-series.md
Cron jobs inside PostgreSQLpg_cronMatureextensions-toolkit.md
Table partitioning managementpg_partmanMatureextensions-toolkit.md
Fuzzy string matching (LIKE '%x%')pg_trgmCore contribextensions-toolkit.md
Encryption / hashingpgcryptoCore contribextensions-toolkit.md
Audit loggingpgAuditMatureextensions-toolkit.md

Decision shortcuts:

  • Need search? Start with PostgreSQL built-in tsvector + GIN. If you need BM25 ranking, fuzzy, or hybrid search → ParadeDB.
  • Need AI/ML embeddings? → pgvector with HNSW index.
  • Need lat/lng distance queries? → PostGIS. Use geography type for global lat/lng, geometry for local projected data.
  • Need IoT / metrics / time-bucketed aggregation? → TimescaleDB.

EXPLAIN ANALYZE Cheat Sheet

// helper: run EXPLAIN ANALYZE from Go and log the plan
func ExplainQuery(ctx context.Context, db *sqlx.DB, query string, args ...any) (string, error) {
    row := db.QueryRowContext(ctx, "EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) "+query, args...)
    var plan string
    if err := row.Scan(&plan); err != nil {
        return "", fmt.Errorf("explain: %w", err)
    }
    return plan, nil
}

5 things to look for in the output:

SignalWhat It MeansFix
Seq Scan on large tableMissing index or planner ignoring itAdd index; check random_page_cost
actual rows=rows= (estimated)Stale statisticsANALYZE tablename;
Buffers: shared hit=0 read=NCold cache, high I/OIncrease shared_buffers; warm cache
Sort Method: external mergework_mem too low for sortIncrease work_mem for session
Nested Loop with large outerPlanner underestimating join sizeCheck stats; consider SET enable_nestloop = off to test

For full performance tuning (pg_stat_statements, autovacuum, bloat, monitoring): see references/query-performance.md.

Connection Pool Sizing

Formula (per backend process):

max_connections = (core_count * 2) + effective_spindle_count

For SSDs: core_count * 2 + 1 is a practical starting point (e.g., 4 cores → 9 connections).

Go application pool settings:

sqlDB.SetMaxOpenConns(25)          // Match PgBouncer pool_size or PostgreSQL max_connections budget
sqlDB.SetMaxIdleConns(5)           // Keep some warm connections ready
sqlDB.SetConnMaxLifetime(5 * time.Minute)  // Recycle to balance load across replicas
sqlDB.SetConnMaxIdleTime(1 * time.Minute)  // Close idle connections faster in low-traffic periods

PgBouncer guidance:

  • Use transaction pooling mode (default) — releases connection back to pool after each transaction
  • Set pool_size per database to match your PostgreSQL max_connections budget for that app
  • Set reserve_pool_size = 5 for burst handling
  • Don't use session pooling with connection-pool-aware Go drivers (pgx already pools)

Partitioning Strategy

StrategyWhen to UseExample
RANGETime-series, date-based queriesPARTITION BY RANGE (created_at)
LISTKnown, fixed categoriesPARTITION BY LIST (region)
HASHEven distribution, no natural rangePARTITION BY HASH (user_id)

Rules:

  • Partition when a single table exceeds ~50–100M rows or you need fast bulk deletes (drop partition)
  • Always include the partition key in WHERE clauses — otherwise the planner scans all partitions
  • Use pg_partman for automated partition creation and retention
  • Partitioned tables support indexes, constraints, and foreign keys (PostgreSQL 12+)
CREATE TABLE events (
    id         BIGINT GENERATED ALWAYS AS IDENTITY,
    tenant_id  UUID NOT NULL,
    event_type TEXT NOT NULL,
    payload    JSONB,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
) PARTITION BY RANGE (created_at);

-- Monthly partitions
CREATE TABLE events_2026_01 PARTITION OF events
    FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE events_2026_02 PARTITION OF events
    FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');

Reference Files

Load these for deeper detail:

  • references/schema-design.md — Naming conventions, data type selection, normalization, constraints, soft delete patterns, multi-tenancy, audit trails, complete schema example
  • references/migration-impact-analysis.md — Lock hierarchy, safe vs unsafe ALTER TABLE, zero-downtime patterns for every operation, batched backfill, NOT VALID constraints, lock_timeout strategy, migration checklist
  • references/index-strategy.md — B-tree, GIN, GiST, BRIN, SP-GiST, partial, expression, covering indexes, index maintenance, EXPLAIN ANALYZE deep dive, complete example
  • references/query-performance.md — pg_stat_statements setup, autovacuum tuning, bloat detection, planner statistics, work_mem, connection pool sizing, read replicas, monitoring Go helper
  • references/extensions-toolkit.md — pg_cron, pg_partman, pg_trgm, pg_stat_statements, pgcrypto, uuid-ossp, pgAudit, extension management in migrations
  • references/paradedb-full-text-search.md — BM25 search, pg_search setup, queries, fuzzy/autocomplete, hybrid search with pgvector, analytics, Go integration
  • references/pgvector-embeddings.md — Vector types, IVFFlat vs HNSW, similarity queries, Go integration with pgvector-go, capacity planning
  • references/postgis-geospatial.md — geometry vs geography, spatial types, GiST indexes, distance/KNN/bounding box/point-in-polygon queries, Go integration
  • references/timescaledb-time-series.md — Hypertables, time_bucket, continuous aggregates, compression, retention, downsampling, Go integration
  • references/row-level-security.md — RLS setup, policy types, PERMISSIVE vs RESTRICTIVE, multi-tenant session variable pattern, Gin middleware, Go repository integration, performance, testing with testcontainers, common pitfalls
  • references/backup-and-recovery.md — pg_dump/pg_restore, pg_basebackup, WAL archiving, PITR, backup validation, managed DB backups, disaster recovery checklist
  • references/replication-and-ha.md — Streaming replication, logical replication, read/write splitting in Go, replication lag monitoring, Patroni HA, pg_auto_failover, managed HA, application resilience

Cross-Skill References

  • For GORM/sqlx repository implementations and migrations tooling: see the golang-gin-database skill
  • For testing database queries with testcontainers: see the golang-gin-testing skill
  • For PostgreSQL Docker setup and Kubernetes StatefulSets: see the golang-gin-deploy skill
  • For handler patterns that call repository methods: see the golang-gin-api skill

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

golang-gin-api

No summary provided by upstream source.

Repository SourceNeeds Review
General

golang-gin-database

No summary provided by upstream source.

Repository SourceNeeds Review
General

golang-gin-testing

No summary provided by upstream source.

Repository SourceNeeds Review
General

golang-gin-swagger

No summary provided by upstream source.

Repository SourceNeeds Review