postgres-query-expert
БесплатноБез исполняемых скриптовНе проверенA comprehensive guide for interacting with PostgreSQL 16 databases. Use this skill for constructing standard and advanced SQL queries, optimizing performance…
Об этом скилле
PostgreSQL Query Expert
This skill is a definitive reference for PostgreSQL 16, covering query construction, optimization, schema management, and system introspection.
Instructions
1. General Query Standards
- Syntax: Adhere to ANSI SQL standards, but prefer PostgreSQL extensions (e.g.,
DISTINCT ON,RETURNING,LATERAL,FILTERclauses) when they provide cleaner logic or better performance. - Identifiers: Use
snake_casefor all identifiers. Only quote identifiers ("MyTable") if absolutely necessary; prefer lowercase unquoted names. - Safety:
- Parameterization: Always use parameters (
$1,$2, …) for literal values. Never inject user input directly. - Timeouts: For exploratory queries on large databases, prepend
SET LOCAL statement_timeout = '30s';. - Transactions: Use explicit
BEGINandCOMMITblocks for multi-step operations.
- Parameterization: Always use parameters (
2. Performance & Optimization
- Explain plans: Use
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS)to diagnose bottlenecks. - Red flags:
Seq Scanon large tables, highBuffers: shared hit(RAM usage), orDisk: read(I/O). - Indexing: Recommend specific index types based on usage:
- B-tree: Standard equality/range (
=,<,>) queries. - GIN: For composite types like
JSONB(@>) or arrays (&&), and full-text search. - GiST: For geometric data and ranges.
- B-tree: Standard equality/range (
- CTEs: Use Common Table Expressions (
WITH) for readability. In PG16+, these are optimized (inlined) by default unlessMATERIALIZEDis specified.
Introspection (Agent Capabilities)
When exploring a new database, use these queries to understand the schema.
List All Tables
SELECT n.nspname AS schema,
c.relname AS table,
obj_description(c.oid) AS description
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY 1, 2;
Get Table Columns & Types
SELECT a.attname AS column,
format_type(a.atttypid, a.atttypmod) AS type,
a.attnotnull AS not_null,
col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
WHERE a.attrelid = 'public.target_table_name'::regclass
AND a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum;
Reference: Data Querying (DQL)
Advanced Aggregations
- Filter clause:
count(*) FILTER (WHERE status = 'active') - Grouping sets:
GROUP BY GROUPING SETS ((brand), (brand, category), ()) - Any value:
any_value(col)(PG16+) returns an arbitrary value from the group.
Window Functions
Perform calculations across a set of table rows related to the current row.
SELECT dept,
emp_no,
salary,
-- Rank employees by salary within department
dense_rank() OVER (PARTITION BY dept ORDER BY salary DESC) AS rank,
-- Running total of salaries
sum(salary) OVER (
PARTITION BY dept
ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM employees;
Pattern Matching
- LIKE:
col LIKE 'foo%'(simple wildcard). - ILIKE:
col ILIKE 'foo%'(case-insensitive). - SIMILAR TO:
col SIMILAR TO '[a-c]%'(SQL-regex style). - POSIX regex:
- Case-sensitive:
col ~ '^[a-z]+$' - Case-insensitive:
col ~* 'foo'
- Case-sensitive:
Reference: Data Modification (DML)
MERGE (Upsert / Conditional Ops)
Standard SQL method for inserting, updating, or deleting based on join conditions (PG15+).
MERGE INTO wine_stock ws
USING wine_shipments s
ON s.winery_id = ws.winery_id
AND s.year = ws.year
WHEN MATCHED THEN
UPDATE SET stock = ws.stock + s.count
WHEN NOT MATCHED THEN
INSERT (winery_id, year, stock)
VALUES (s.winery_id, s.year, s.count);
INSERT ... ON CONFLICT (Legacy Upsert)
Postgres-specific, often more concise for simple unique-key conflicts.
INSERT INTO kv_store (key, value)
VALUES ('config', '{"a":1}')
ON CONFLICT (key)
DO UPDATE SET value = EXCLUDED.value;
RETURNING Clause
Return data from modified rows immediately.
DELETE FROM archived_logs
WHERE created_at < NOW() - INTERVAL '1 year'
RETURNING id, created_at;
Reference: Special Data Types
JSONB (Binary JSON)
Prefer jsonb over json for storage and indexing.
| Operator | Description | Example |
|---|---|---|
-> / ->> |
Get element (JSON / text) | data->'key' |
@> |
Contains (indexable) | data @> '{"tag": "urgent"}' |
? |
Key exists | data ? 'error' |
#- |
Delete path | data #- '{info, sensitive}' |
SQL/JSON path (PG12+):
-- Find all items with price > 10
SELECT jsonb_path_query(data, '$.items[*] ? (@.price > 10)')
FROM orders;
Arrays
SELECT ARRAY[1,2,3]; -- Creation
SELECT (ARRAY[1,2,3])[1]; -- Access (1-based index)
SELECT 1 = ANY(arr_col); -- Check if value exists in array
SELECT unnest(arr_col) FROM t; -- Ex
Установить postgres-query-expert в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
ReadGrepGlobFAQ
Что делает скилл postgres-query-expert?
A comprehensive guide for interacting with PostgreSQL 16 databases. Use this skill for constructing standard and advanced SQL queries, optimizing performance, debugging errors, managing schema objects, and introspecting database structure.
Как установить скилл postgres-query-expert?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл postgres-query-expert запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
от Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
от Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
от Anthropic