Skip to main content

iron_sql

License main PyPI - Version

iron_sql is a typed SQL code generator and async runtime for PostgreSQL. Write SQL where you use it, run render_sql_module(...).write(), and get a module with typed dataclasses, query helpers, and pooled connections without hand-written boilerplate.

Installation

pip install iron-sql             # runtime only (psycopg + psycopg-pool + pydantic)
pip install iron-sql[codegen]    # + inflection for code generation

The sqlc binary is bundled automatically via the sqlc Python package.

Key Features

  • Query discovery. render_sql_module scans your codebase for calls like <module>_sql("SELECT ..."), runs sqlc for type analysis, and emits a typed module.
  • Strong typing. Generated dataclasses and method signatures flow through your IDE and type checker.
  • Async runtime. Built on psycopg v3 with pooled connections, context-based connection reuse, and transaction helpers.
  • Streaming. query_stream() uses server-side cursors for memory-efficient iteration over large result sets.
  • Safe by default. Helper methods enforce expected row counts instead of returning silent None.

Design Constraints

Every query is a static SQL literal at its call site. That one constraint is where the guarantees come from: sqlc can type the statement ahead of time, generated Literal overloads hand your editor the exact result type, and the SQL that runs is the SQL you read. Nothing assembles a statement at runtime, so every statement text is fixed at generation time and appears verbatim in the generated module, next to the call sites that use it. That makes a hot pg_stat_statements entry easy to trace back -- though the mapping is not one-to-one: PostgreSQL folds statements that differ only in literal constants into a single entry, and splits one statement across users, databases, and search_path.

Non-goals, by construction:

  • SQL fragment composition. No shared WHERE snippets stitched together before execution.
  • Dynamic query assembly. Conditional filters belong in the SQL itself (@status?::task_status IS NULL OR status = @status?), not in Python string building.
  • Lazy relations and object graphs. Nothing loads on attribute access; related rows come from a query you wrote.

A 1+N pattern is therefore never implicit: it is always a loop in your own code around a statement you can read. detect_sql_repeats() reports one when you write it by accident.

Package Layout

  • runtime.py -- async ConnectionPool, row helpers (get_one_row, typed_scalar_row), JSON validation decorators.
  • codegen/generator.py -- query discovery, type resolution, module rendering.
  • codegen/sqlc.py -- wraps the sqlc CLI and models its JSON output.
  • codegen/util.py -- shared codegen utilities (indent_block).

Getting Started

  1. Add a schema file. A Postgres DDL dump, e.g. db/schema.sql.

  2. Write queries where they live. Import the future helper and use SQL literals inline:

    from myapp.db.mydb import mydb_sql
    
    user = await mydb_sql(
        "SELECT id, username, email, created_at FROM users WHERE id = @user_id"
    ).query_single_row(user_id=uid)
    

    Named parameters use @param (required) or @param? (optional) and become keyword-only arguments. Names are case-insensitive, and all uses of one name must agree on ?. Put a space between a parameter and an operator next to it: PostgreSQL reads id=@id with the operator =@ and @n?-1 with the operator ?-. Positional $1 works too, but not in a query with named parameters. sqlc.arg() and sqlc.narg() are rejected.

  3. Generate the client module.

    from pathlib import Path
    
    from iron_sql.codegen import render_sql_module
    
    render_sql_module(
        schema_path=Path("schema.sql"),
        module_full_name="myapp.db.mydb",
        dsn_expr="myapp.config:DSN",
        src_path=Path("."),
    ).write()
    

    src_path is the root of the source scan. The scan does not enter dot-prefixed directories, virtual environments that contain pyvenv.cfg, or directory symlinks. These rules apply only to child directories, so src_path can have any name. All other directories are scanned, including directories without importable Python names.

    This writes myapp/db/mydb.py containing:

    • a connection pool singleton,
    • *_connection() and *_transaction() context managers,
    • *_is_in_transaction() check for an open transaction,
    • *_listen_session(channel) and *_notify(channel, payload="") helpers,
    • dataclasses for multi-column results (deduplicated by table),
    • StrEnum classes for PostgreSQL enums,
    • a query class per statement with typed methods,
    • overloads for the *_sql() helper so editors infer return types.

    To check that a committed module is current without writing it, call diff() instead of write(). It returns a unified diff from the file to the rendered module, or an empty string if the file is current.

Customization

  • Type overrides. type_overrides={"float8": "decimal.Decimal"} maps database type names to Python type strings. For built-in types the key is the PostgreSQL internal name (float8, varchar, timestamptz), not the SQL-standard spelling (double precision, character varying); for a user-defined enum, domain or extension type it is the type name as declared (custom_int, citext). A key that no query column or parameter uses raises SQLGenerationError listing the type names actually in use.
  • JSON model overrides. json_model_overrides={"users.metadata": "myapp.models:UserMeta"} types a json, jsonb, text or varchar column of a table and validates it with Pydantic on read; the same option on a *_sql(...) call types a result column of that query. See JSON Columns.
  • Naming conventions. Supply to_pascal_fn and to_snake_fn callables to control generated names.
  • Connection settings. dsn_expr and pool_options_expr are written verbatim into the generated module; point them at config variables, env var lookups, or function calls. The generator also imports the module and evaluates the expression: dsn_expr must evaluate to str, pool_options_expr to dict. An exception raised by that import or evaluation propagates with its own type and traceback; a note names the setting and the expression.
  • Session settings and timeouts. PoolOptions["kwargs"] reaches every pooled connection, so libpq options carries server-side settings: {"kwargs": {"options": "-c statement_timeout=5000 -c lock_timeout=1000"}}. statement_timeout bounds a single statement, not a query method: a query_stream() that iterates for longer than the timeout is unaffected, because DECLARE and each FETCH count separately. Consider idle_in_transaction_session_timeout alongside them to bound transactions that stay open while the application is busy elsewhere.
  • Debug artifacts. Pass debug_path to save sqlc inputs and outputs for inspection. The generator also writes each refused directory and its reason to skipped_dirs.json.

JSON Columns

A json, jsonb, text or varchar column can carry a Python type, written module:Name: a Pydantic model, a type alias such as TaskList = list[Task] or type TaskList = list[Task], or a builtin such as builtins:int. The field annotation and the validation on read use that one type. json and jsonb values arrive decoded, so a JSON string reads as str; text and varchar values are parsed as JSON text.

from uuid import UUID

from pydantic import BaseModel


class ProjectSettings(BaseModel):
    archived: bool = False


class Task(BaseModel):
    id: UUID
    title: str


type TaskList = list[Task]

render_sql_module(json_model_overrides={"table.column": "module:Name"}) types a column of a table wherever a query returns it. A computed column -- json_agg, to_jsonb, json_build_object, a subquery with them -- belongs to no table, so the call that runs the query types it: the key is the result column name as the SQL spells it, after AS.

projects = await mydb_sql(
    """
    SELECT projects.id, projects.settings, coalesce(
        (SELECT json_agg(tasks ORDER BY tasks.title)
        FROM tasks WHERE tasks.project_id = projects.id),
        '[]'::json
    ) AS tasks
    FROM projects
    """,
    json_model_overrides={"tasks": "myapp.models:TaskList"},
).query_all_rows()
render_sql_module(
    schema_path=Path("schema.sql"),
    module_full_name="myapp.db.mydb",
    dsn_expr="myapp.config:DSN",
    json_model_overrides={"projects.settings": "myapp.models:ProjectSettings"},
).write()
  • The setting on the call wins over the table setting for that result column. Every call of the same SQL must pass the same json_model_overrides.
  • Nullability follows sqlc. A bare (SELECT json_agg(...)) is typed non-null, yet PostgreSQL returns NULL for an empty set, and reading that NULL raises. Wrap it in coalesce(..., '[]'::json): sqlc then types the column as nullable, and the query never returns NULL.
  • Without a DSN sqlc may not infer the type of an expression such as coalesce(...); the generator rejects an override on such a column. Cast the expression (::json, ::jsonb) or generate with a DSN.

Runtime Highlights

  • ConnectionPool opens lazily and reopens after close(), with ContextVar-based connection reuse for nested contexts.
  • *_is_in_transaction() reports whether the context connection has an open transaction. It reads the transaction status of that connection, not the presence of a connection in the ContextVar, so a bare *_connection() gives False. When another task runs a statement on the same connection, the check waits for that statement to finish. query_stream() opens its own transaction for the read, so the check gives True while a stream is iterated.
  • *_listen_session() uses a dedicated pooled connection and doesn't reuse ContextVar transaction connections.
  • *_notify() sends the channel and payload as parameters of pg_notify(), so neither appears in the query text that pg_stat_activity or client tracing shows; PostgreSQL logs that record parameters still include them. A channel longer than 63 bytes in UTF-8 raises psycopg.errors.InvalidParameterValue rather than being truncated; *_listen_session() still truncates such a name to 63 bytes.
  • query_single_row() raises NoRowsError; query_optional_row() returns None. Both raise TooManyRowsError on 2+ rows.
  • query_stream() returns an async context manager yielding an AsyncGenerator; uses server-side cursors with automatic transaction management.
  • JSONB params are sent with psycopg.types.json.Jsonb; JSON with psycopg.types.json.Json. Scalar row factories validate types at runtime.
  • json_validated decorator validates JSON fields of a result dataclass on construction; typed_json_scalar_row validates a single JSON result column.
  • detect_sql_repeats() reports accidental 1+N loops; see below.

Detecting Accidental 1+N

detect_sql_repeats() watches for one statement executing many times in quick succession inside a single asyncio task -- the shape of a query sitting in a loop. Wrap the part of the process you want watched:

from contextlib import asynccontextmanager

from iron_sql import detect_sql_repeats

@asynccontextmanager                       # e.g. an ASGI lifespan in development
async def lifespan(app):
    with detect_sql_repeats(executions=10, within_seconds=1.0):
        yield
@pytest.fixture(autouse=True)              # or a fixture that fails the run in CI
def no_repeated_queries():
    with detect_sql_repeats(executions=5, within_seconds=10.0, strict=True):
        yield

executions is how many runs of the same statement already look like a loop; within_seconds is the sliding window they must fall into. Both default to a deliberately quiet executions=10, within_seconds=1.0. Tune them to your code rather than to these numbers: set executions just above the largest batch you legitimately run in a loop (inserting a handful of rows one statement at a time is a normal pattern this cannot tell apart), and set within_seconds to the rough duration of one logical operation -- an HTTP request, one worker job. A window that is too wide merges neighbouring operations into a false report; one that is too narrow misses a slow loop whose every query waits on the network.

  • Each repeated statement is reported once per task, as a logging warning naming the call sites and the statement itself, collapsed to one line and truncated. strict=True raises RepeatedQueryError instead, which is what you want in CI.
  • Counting is per asyncio task, so a hundred concurrent handlers running the same query once each never trip it -- only a loop inside one of them does.
  • Detection is off unless a block is active, and the block cannot be nested: entering a second one raises RuntimeError.

Example

The example/ directory contains a complete working setup: a PostgreSQL schema, generation script with testcontainers, and sample query definitions. See example/generate.py for the codegen call and example/main.py for query usage.

Validation and Troubleshooting

  • render_sql_module raises iron_sql.codegen.SQLGenerationError when it rejects its input: SQL, the schema file, the scanned sources and their *_sql(...) calls, settings and overrides, generated names, row_type, or an unsupported construct such as a jsonb[] query parameter. The message names the place (file:line:column in the sources, or the setting and its key; both sides of a conflict), the violated requirement, and the fix or the accepted form. It never contains the evaluated DSN.
  • A failure that sqlc itself reports is also a SQLGenerationError, with sqlc's output mapped to the source lines of the statements.
  • Other exceptions keep their types: an exception from importing or evaluating dsn_expr or pool_options_expr (for example ModuleNotFoundError or NameError), filesystem errors other than a missing schema file, a failure to launch sqlc or to parse its JSON output, and errors from RenderedModule.write(). A wrong call to render_sql_module itself is an ordinary Python TypeError. The note naming the setting is part of the traceback (__notes__), not of str(exc).
  • SQLGenerationError subclasses ValueError, so except ValueError still catches every rejection. Earlier versions raised some rejections as TypeError (*_sql(...) call arguments, jsonb[] parameters, the type of an evaluated setting) or SyntaxError (a source file that does not parse); except TypeError and except SyntaxError no longer catch them.
  • Unknown SQL types map to object and emit UnknownSQLTypeWarning (promotable to error with warnings.filterwarnings).
  • Statements with the same SQL but conflicting row_type values are rejected at generation time.
  • A user-defined type may reuse a standard SQL spelling of a built-in (integer, real). Enums and composite types are recognised as user-defined either way. A domain or extension type is recognised only where the column references it schema-qualified (v public."integer"), because an unqualified reference reaches iron_sql spelled exactly like the built-in; such a column resolves to the built-in, and type_overrides cannot name it.

Metadata

Release files for iron-sql 1.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iron-sql 1.7.1
File Size Uploaded Provenance Actions
iron_sql-1.7.1.tar.gz 40.4 kB Without provenance Details for iron_sql-1.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for iron-sql 1.7.1
File Interpreter ABI Platform Actions
iron_sql-1.7.1-py3-none-any.whl Python 3 none any Details for iron_sql-1.7.1-py3-none-any.whl

Total release size: 81.4 kB

Release files / iron_sql-1.7.1.tar.gz

Download URL iron_sql-1.7.1.tar.gz
Size 40.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3d3c80a87e5e61c5fa0dc13c8866eb3bcd3dc63ff3f9fa16e94abdb036d4162f
BLAKE2b-256 checksum
How to use checksums
3eba2ac1c635839da0d152d2ad33c239ac3fe640a6122f0a31ad34e34450b929
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / iron_sql-1.7.1-py3-none-any.whl

Download URL iron_sql-1.7.1-py3-none-any.whl
Size 41.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e81f159f3693ea046ed8966eb4218fc362e271771aed20b39ff3f2b5f84dfffa
BLAKE2b-256 checksum
How to use checksums
714a3d36b28b8758826d5f4fac66b48ed0104d6607296348457debcfbd5188bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for iron-sql 1.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page