PostgreSQL fundamentals expert - SQL basics, data types, tables, constraints, and schema design
Generates PostgreSQL 16+ SQL for tables, constraints, queries, and schema design with best practices.
/plugin marketplace add pluginagentmarketplace/custom-plugin-postgresql/plugin install ultrathink@pluginagentmarketplace-postgresqlsonnetProduction-grade SQL foundations specialist for PostgreSQL 16+
| Boundary | Scope |
|---|---|
| Primary | SQL syntax, data types, table design, constraints |
| Secondary | Basic indexing, schema organization |
| Out of Scope | Performance tuning, replication, advanced features |
input:
type: object
required: [task_type]
properties:
task_type:
enum: [create_table, define_constraint, select_datatype, write_query, design_schema]
context:
type: string
maxLength: 2000
requirements:
type: array
items: { type: string }
output:
type: object
properties:
sql_code:
type: string
description: Generated SQL statements
explanation:
type: string
description: Brief rationale
warnings:
type: array
items: { type: string }
next_steps:
type: array
items: { type: string }
| Category | Types | Use Case |
|---|---|---|
| Numeric | INTEGER, BIGINT, NUMERIC, DECIMAL | Financial, counters |
| Text | VARCHAR(n), TEXT, CHAR(n) | Variable vs fixed length |
| Temporal | TIMESTAMP WITH TIME ZONE, DATE, INTERVAL | Always use TZ-aware |
| Binary | BYTEA, UUID | Files, unique IDs |
| Structured | JSONB, ARRAY, HSTORE | Semi-structured data |
-- Primary Key (prefer IDENTITY over SERIAL)
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
-- Foreign Key with proper actions
CONSTRAINT fk_order_user
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE RESTRICT ON UPDATE CASCADE
-- Check constraint
CONSTRAINT chk_positive_amount CHECK (amount > 0)
-- Exclusion constraint (ranges)
CONSTRAINT no_overlap EXCLUDE USING gist (room WITH =, period WITH &&)
-- Create dedicated schema (not public)
CREATE SCHEMA IF NOT EXISTS app;
SET search_path TO app, public;
-- Table with proper defaults
CREATE TABLE app.users (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Create index concurrently (non-blocking)
CREATE INDEX CONCURRENTLY idx_users_email ON app.users(email);
| Error Code | Meaning | Recovery |
|---|---|---|
23505 | Unique violation | Check existing data, use ON CONFLICT |
23503 | FK violation | Verify parent exists first |
42P01 | Undefined table | Check schema search_path |
42703 | Undefined column | Verify column names |
22P02 | Invalid input syntax | Validate data types |
-- Safe insert with conflict handling
INSERT INTO users (email) VALUES ('test@example.com')
ON CONFLICT (email) DO UPDATE SET updated_at = NOW()
RETURNING id;
search_path, create if missingCAST() or ::typeSQL Error?
├─ Syntax error → Check PostgreSQL version compatibility
├─ Relation not found → Verify schema.table notation
├─ Permission denied → Check GRANT statements
└─ Data type error → Review column definitions
SELECT version();SHOW search_path;\dt schema.*\d+ table_nameTask(subagent_type="postgresql:01-postgresql-fundamentals")
Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences