Skip to content
Discord Get Started

SQL Reference

DB9 implements a PostgreSQL-compatible SQL engine with pgwire protocol v3, a cost-based optimizer, and TiKV-backed distributed storage. Most PostgreSQL clients, ORMs, and drivers work without changes.

TaskWhere to look
Create tables, indexes, views, typesDDL
Insert, update, delete, upsertDML and Queries
JOINs, CTEs, window functions, subqueriesDML and Queries
Transactions, savepoints, isolation levelsTransactions
Data types and coercion rulesData Types
100+ built-in functionsBuilt-in Functions
PL/pgSQL, triggers, sequences, custom typesAdvanced SQL
Roles, users, grantsAuth and Roles
Session parameters and GUC settingsSession Parameters
Engine and extension limitsLimits
System catalog tablesSystem Catalog
  • Wire protocol — pgwire v3 with Simple Query and Extended Query (Parse/Bind/Describe/Execute)
  • DDLCREATE/ALTER/DROP TABLE, indexes (btree, GIN, GiST, HNSW), views, materialized views, schemas, sequences, functions, triggers, types, collations
  • DMLINSERT, UPDATE, DELETE with RETURNING; INSERT ON CONFLICT (upsert)
  • QueriesJOIN (inner, left, right, full outer, cross, lateral), CTEs (including recursive), window functions, subqueries (correlated, EXISTS, IN/ANY/ALL), set operations (UNION/INTERSECT/EXCEPT)
  • TransactionsBEGIN/COMMIT/ROLLBACK, savepoints, autocommit
  • Data types — boolean, integer, bigint, double precision, numeric, text, varchar, bytea, timestamp/timestamptz, date, time, interval, uuid, json/jsonb, arrays, serial/bigserial, vector, tsvector/tsquery
  • PL/pgSQL — functions, procedures, control flow, exception handling
  • Triggers — row-level and statement-level, BEFORE/AFTER on INSERT/UPDATE/DELETE
  • Indexes — btree (default), GIN, GiST, HNSW (vector), partial indexes, expression indexes, CREATE INDEX CONCURRENTLY
FeatureDB9 behavior
SERIALIZABLE isolationAccepted but downgraded to REPEATABLE READ (TiKV snapshot isolation). A warning is logged.
SMALLINT / INT2Aliased to INTEGER (32-bit).
REAL / FLOAT4Aliased to DOUBLE PRECISION (64-bit).
CHAR(n) fixed-lengthNot supported. Use VARCHAR(n) or TEXT.
Timestamp precisionMillisecond precision. Fractional-second syntax (0) through (6) is accepted but sub-millisecond values are not representable.
GIN index executionThe planner produces GIN scan plans (visible in EXPLAIN), but the runtime currently falls back to table scan.
EncodingUTF-8 only. No other server or client encoding.

These PostgreSQL features are not available in DB9:

  • LISTEN / NOTIFY (async notifications)
  • Logical replication
  • Foreign data wrappers (postgres_fdw, etc.)
  • Tablespaces (all storage is TiKV-managed)
  • Advisory locks

When you send a SQL query to DB9, it follows this pipeline:

  1. Parse — SQL text is parsed into an AST
  2. Route — Statement classified as DDL, DML, transaction control, or settings
  3. Expand views — View references inlined recursively (up to 64 levels)
  4. Analyze — Semantic analysis resolves names, infers types, checks scope
  5. Optimize — Cost-based optimizer (DPccp algorithm) chooses join order, access methods, and physical operators
  6. Execute — Pull-based (Volcano) operator tree streams results row by row
  7. Return — Results sent over pgwire

The optimizer uses table statistics from ANALYZE for cardinality estimation, with predicate pushdown and subquery decorrelation.

LimitValue
Statement timeout60s (default)
Idle transaction timeout60s (default)
View nesting depth64 levels
Pending portals per connection32
Timestamp precisionMilliseconds
EncodingUTF-8 only

See Limits for extension-specific limits (HTTP, fs9, pg_cron).

DB9 uses standard PostgreSQL connection strings:

Terminal
# psql
psql "postgresql://dbname.admin:password@pg.db9.io:5433/postgres"
# Or via the CLI
db9 db sql <db> -q "SELECT now()"

Any PostgreSQL client library works: psycopg2, node-postgres, JDBC, Go pgx, etc. See Connect for full details.

  • Data Types — 23+ supported types with coercion and casting rules
  • DDL — CREATE, ALTER, DROP for tables, indexes, views, schemas, types, triggers
  • DML and Queries — INSERT, UPDATE, DELETE, JOINs, CTEs, window functions, subqueries
  • Transactions — isolation levels, savepoints, COPY
  • Built-in Functions — string, math, date/time, JSON, array, aggregate, window, vector, FTS
  • System Catalog — pg_class, pg_attribute, information_schema, and more
  • Auth and Roles — users, roles, grants, and permissions
  • Advanced SQL — PL/pgSQL, triggers, sequences, custom types, collations
  • Session Parameters — SET/SHOW/RESET and GUC compatibility
  • Limits — engine, extension, and timing constraints
  • Connect — connection strings, TLS, and driver configuration
  • Extensions — fs9, HTTP, vector, pg_cron, embedding, and more
  • Architecture — TiKV storage, pgwire, and query engine internals
  • CLI Referencedb9 db sql for terminal SQL execution
  • Compatibility Matrix — full supported/unsupported feature matrix across SQL, types, indexes, ORMs, and extensions