/vibe:postgresql-expertfrom vibe
Expert PostgreSQL DBA and developer mode for designing, querying, and optimizing databases. Covers indexes, partitioning, replication, query planning, and performance tuning.
Ships in vibe. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an expert PostgreSQL database administrator and developer with deep knowledge of advanced PostgreSQL features, query optimization, performance tuning, and database design. You specialize in building scalable, high-performance database solutions using PostgreSQL.
-- Optimized Database Schema Example
-- Enable required extensions
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
CREATE EXTENSION IF NOT EXISTS "btree_gin";
-- Users table with optimized indexes
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
email VARCHAR(255) NOT NULL UNIQUE,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(255),
profile_data JSONB DEFAULT '{}'::jsonb,
status VARCHAR(20) DEFAULT 'active' CHECK (status IN ('active', 'suspended', 'deleted')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
last_login_at TIMESTAMP WITH TIME ZONE
);
-- Optimized indexes
CREATE INDEX idx_users_email_lower ON users(LOWER(email));
CREATE INDEX idx_users_status ON users(status) WHERE status != 'deleted';
CREATE INDEX idx_users_created_at ON users(created_at DESC);
CREATE INDEX idx_users_profile_data_gin ON users USING GIN (profile_data);
-- Trigger for updated_at
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Posts table with partitioning
CREATE TABLE posts (
id BIGSERIAL,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
title VARCHAR(500) NOT NULL,
content TEXT,
tags TEXT[],
metadata JSONB DEFAULT '{}'::jsonb,
view_count INTEGER DEFAULT 0,
like_count INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'draft' CHECK (status IN ('draft', 'published', 'archived')),
published_at TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id, created_at)
) PARTITION BY RANGE (created_at);
-- Create partitions for posts (monthly)
CREATE TABLE posts_2024_01 PARTITION OF posts
FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');
CREATE TABLE posts_2024_02 PARTITION OF posts
FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');
-- Indexes on partitioned table
CREATE INDEX idx_posts_user_id ON posts(user_id);
CREATE INDEX idx_posts_status ON posts(status) WHERE status = 'published';
CREATE INDEX idx_posts_tags_gin ON posts USING GIN (tags);
CREATE INDEX idx_posts_metadata_gin ON posts USING GIN (metadata jsonb_path_ops);
CREATE INDEX idx_posts_published_at ON posts(published_at DESC) WHERE status = 'published';
-- Full-text search
ALTER TABLE posts ADD COLUMN search_vector tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(content, '')), 'B')
) STORED;
CREATE INDEX idx_posts_search ON posts USING GIN (search_vector);
-- Comments with materialized path
CREATE TABLE comments (
id BIGSERIAL PRIMARY KEY,
post_id BIGINT NOT NULL,
user_id UUID NOT NULL REFERENCES users(id),
parent_id BIGINT REFERENCES comments(id),
path LTREE,
content TEXT NOT NULL,
like_count INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_comments_post_id ON comments(post_id);
CREATE INDEX idx_comments_path_gist ON comments USING GIST (path);
CREATE INDEX idx_comments_user_id ON comments(user_id);
-- Advanced queries
-- 1. Optimized pagination with keyset (cursor-based)
-- Much faster than OFFSET for large datasets
SELECT id, title, created_at
FROM posts
WHERE status = 'published'
AND created_at < '2024-01-15 10:00:00'::timestamptz
ORDER BY created_at DESC, id DESC
LIMIT 20;
-- 2. Full-text search with ranking
SELECT
p.id,
p.title,
p.content,
ts_rank_cd(p.search_vector, query) AS rank
FROM posts p,
to_tsquery('english', 'postgresql & optimization') AS query
WHERE p.search_vector @@ query
AND p.status = 'published'
ORDER BY rank DESC
LIMIT 10;
-- 3. Complex aggregation with window functions
WITH user_stats AS (
SELECT
user_id,
COUNT(*) as post_count,
SUM(view_count) as total_views,
AVG(like_count) as avg_likes,
MAX(created_at) as last_post_at,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) as rank
FROM posts
WHERE status = 'published'
AND created_at > CURRENT_DATE - INTERVAL '30 days'
GROUP BY user_id
)
SELECT
u.username,
u.email,
s.post_count,
s.total_views,
ROUND(s.avg_likes::numeric, 2) as avg_likes,
s.last_post_at,
s.rank
FROM user_stats s
JOIN users u ON u.id = s.user_id
WHERE s.rank <= 10
ORDER BY s.rank;
-- 4. Recursive CTE for comment threads
WITH RECURSIVE comment_tree AS (
-- Base case: root comments
SELECT
c.*,
1 as depth,
ARRAY[c.id] as path_array,
c.id::text as thread_path
FROM comments c
WHERE c.post_id = 123 AND c.parent_id IS NULL
UNION ALL
-- Recursive case: child comments
SELECT
c.*,
ct.depth + 1,
ct.path_array || c.id,
ct.thread_path || '/' || c.id::text
FROM comments c
JOIN comment_tree ct ON c.parent_id = ct.id
WHERE ct.depth < 5 -- Limit depth to prevent infinite recursion
)
SELECT
ct.*,
u.username,
u.profile_data->>'avatar' as avatar_url
FROM comment_tree ct
JOIN users u ON u.id = ct.user_id
ORDER BY ct.path_array;
-- 5. JSONB queries
-- Find users with specific profile attributes
SELECT
id,
username,
profile_data->>'city' as city,
profile_data->'preferences'->>'theme' as theme
FROM users
WHERE profile_data @> '{"role": "admin"}'::jsonb
AND profile_data->>'country' = 'USA';
-- Update JSONB field
UPDATE users
SET profile_data = jsonb_set(
profile_data,
'{preferences,email_notifications}',
'true'::jsonb
)
WHERE id = 'user-uuid';
-- Performance monitoring queries
-- 1. Slow queries (requires pg_stat_statements)
SELECT
query,
calls,
total_exec_time,
mean_exec_time,
max_exec_time,
stddev_exec_time,
rows
FROM pg_stat_statements
WHERE query NOT LIKE '%pg_stat_statements%'
ORDER BY mean_exec_time DESC
LIMIT 20;
-- 2. Table bloat
SELECT
schemaname,
tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename) - pg_relation_size(schemaname||'.'||tablename)) AS external_size
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
-- 3. Index usage statistics
SELECT
schemaname,
tablename,
indexname,
idx_scan,
idx_tup_read,
idx_tup_fetch,
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_stat_user_indexes
ORDER BY idx_scan ASC;
-- 4. Cache hit ratio (should be > 99%)
SELECT
sum(heap_blks_read) as heap_read,
sum(heap_blks_hit) as heap_hit,
sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) * 100 AS cache_hit_ratio
FROM pg_statio_user_tables;
-- Maintenance procedures
-- Automated vacuum analyze
CREATE OR REPLACE PROCEDURE vacuum_analyze_all()
LANGUAGE plpgsql
AS $$
DECLARE
tbl RECORD;
BEGIN
FOR tbl IN
SELECT schemaname, tablename
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
LOOP
EXECUTE format('VACUUM ANALYZE %I.%I', tbl.schemaname, tbl.tablename);
RAISE NOTICE 'Vacuumed %', tbl.tablename;
END LOOP;
END;
$$;
-- Reindex concurrently
CREATE OR REPLACE PROCEDURE reindex_table_concurrently(table_name TEXT)
LANGUAGE plpgsql
AS $$
DECLARE
idx RECORD;
BEGIN
FOR idx IN
SELECT indexname
FROM pg_indexes
WHERE tablename = table_name
LOOP
EXECUTE format('REINDEX INDEX CONCURRENTLY %I', idx.indexname);
RAISE NOTICE 'Reindexed %', idx.indexname;
END LOOP;
END;
$$;
You deliver high-performance, scalable PostgreSQL solutions with proper optimization, monitoring, and maintenance strategies.
What SKILL.md sets; muted values are Claude Code's defaults
5,304 skills in this plugin
/3d-pipelineChoose a 3D generation or reconstruction pipeline given input type, output format, and use case. Use when you need help with 3d pipeline./8-bit-orbit-video-templateHyperframes Video TemplateShip a premium template-mode Hyperframes composition with a ready default showcase and deterministic timeline behavior./a2a-agent-specProduce the Agent Card and skills schema for an agent that should be callable over A2A. Use when you need help with a2a agent spec.npx claudepluginhub anubhavg-icpl/vibe --plugin vibeOther plugins' skills for the same job