Search by

rasuvaeff / context-db

rasuvaeff

Database-side statement timeouts from a rasuvaeff/context budget for MySQL, MariaDB and PostgreSQL over PDO

Package info

github.com/rasuvaeff/context-db

pkg:composer/rasuvaeff/context-db

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.5.0 2026-11-27 20:34 UTC

This package is auto-updated.

Last update: 2026-11-27 20:34:25 UTC


README

Latest Stable Version Total Downloads Build Static analysis Psalm level PHP License

Turns the remaining rasuvaeff/context budget into a statement timeout the database server enforces itself — MySQL, MariaDB and PostgreSQL over PDO. A query stops on the server when the budget runs out instead of running on after the caller gave up.

Using an AI coding assistant? llms.txt contains a compact API reference you can share with the model. Projects using the llm/skills Composer plugin also get this package's agent skill synced into .agents/skills/ automatically on install.

Requirements

  • PHP 8.3+, ext-pdo
  • rasuvaeff/context ^0.3
  • ext-pdo_mysql for MySQL/MariaDB, ext-pdo_pgsql for PostgreSQL

Installation

composer require rasuvaeff/context-db

Limitations

PDO has no per-query timeout of its own (only a connect timeout), so this package uses what each server offers — and each offers something different:

Server Per statement (apply()) Per scope (within()) Error when it fires
MySQL 5.7.8+ /*+ MAX_EXECUTION_TIME(ms) */ on the main read-only SELECT SET SESSION max_execution_time HY000 / 3024
MariaDB 10.1+ SET STATEMENT max_statement_time=s FOR … on any statement SET SESSION max_statement_time 70100 / 1969
PostgreSQL — (no per-statement syntax) SET statement_timeout, SET LOCAL in a transaction 57014
SQLite and others unchanged callback only —
  • MySQL limits only read-only SELECT. INSERT … SELECT, UPDATE, DELETE and DDL run without a server-side limit, whatever you set: keep checking the context between them. apply() returns such statements unchanged rather than adding a hint MySQL would silently ignore.
  • An interrupted MySQL SLEEP() returns 1 instead of failing. Real work fails with error 3024; do not test the limit with SELECT SLEEP(n).
  • PostgreSQL behind a transaction-pooling pgbouncer: a session SET leaks to whoever gets the server connection next. Call within() inside a transaction — it then uses SET LOCAL, which ends with the transaction.
  • A PostgreSQL timeout aborts the transaction. Every further statement fails with 25P02 until the caller rolls back; within() and run() never touch your transaction — only transaction(), which owns its own, rolls it back.
  • On PostgreSQL, a transaction left open across within() is refused. PostgreSQL has no non-transactional SET: when within() is entered outside a transaction and the callback opens one and leaves it open, the restore runs inside that transaction and a later rollback would undo it — the session would keep the budget. within() therefore throws LogicException when its callback returns with such a transaction open, and also when it throws with it open: then the callback's error is the LogicException's previous, the transaction is left to you (never rolled back by within()), and after your rollback the session keeps the budget — reset statement_timeout or discard the connection. The safe forms: transaction() (it owns the transaction and uses SET LOCAL), within() called inside your transaction (also SET LOCAL), or a transaction opened and closed inside the callback.
  • One connection serves one Fiber at a time. The session timeout is one value per connection: a within() or transaction() entered from a second Fiber while another Fiber is suspended inside its own scope on the same \PDO would save that scope's temporary budget as "previous" and restore it out of order — leaving a timeout on the session or running unlimited. It throws LogicException instead, with or without a deadline. Scopes nested in one Fiber are fine; give each concurrent unit of work its own connection.
  • within() gives each statement the budget measured at entry, not what is left after the previous statement: keep calling assertActive() between statements.

Usage

use Rasuvaeff\Context\Context;
use Rasuvaeff\Context\Db\StatementTimeout;

[$context, $controller] = Context::background()->withTimeout(2.0);
$timeout = StatementTimeout::for($pdo);              // detects MySQL / MariaDB / PostgreSQL

try {
    // per statement (MySQL, MariaDB)
    $rows = $pdo->query($timeout->apply('SELECT * FROM report WHERE day = CURDATE()', $context))->fetchAll();

    // per scope (all three; the only way on PostgreSQL)
    $rows = $timeout->within($context, static fn(): array => $pdo->query('SELECT * FROM report')->fetchAll());
} catch (\PDOException $error) {
    throw $timeout->translate($error, $context);     // ContextExpiredException when the budget ran out
} finally {
    $controller->cancel();
}

apply(string $sql, Context $context): string

Dialect Result for apply('SELECT 1', …) with 1.5 s left
MySQL SELECT /*+ MAX_EXECUTION_TIME(1500) */ 1
MariaDB SET STATEMENT max_statement_time=1.5 FOR SELECT 1
PostgreSQL LogicException — use within()
Unsupported SELECT 1
  • MySQL: the hint goes after the main SELECT — after the CTE list for WITH … SELECT, inside the leading parenthesis of (SELECT …) UNION …, past leading comments. An existing optimizer hint is extended; an existing MAX_EXECUTION_TIME keeps the stricter of the two. Anything else (DML, a versioned comment /*!…*/, SQL the scanner does not recognise) is returned unchanged.
  • MariaDB: an existing SET STATEMENT list gains max_statement_time, or its existing value keeps the stricter of the two, compared and written exactly (.0004 stays 0.0004, never rounded up to 0.001).
  • Applying twice equals applying once with the smaller budget.
  • Without a deadline $sql is returned as is; a terminated context throws.

within(Context $context, callable $callback): mixed

Sets the remaining budget as the session's statement timeout, runs the callback, and restores the previous value — also when the callback throws (a failing restore never hides the callback's error). A previous limit no looser than the budget (compared exactly, so MariaDB's 0.0004 s stays stricter than a 1 ms budget) is kept: then nothing is set or restored, the callback runs after the single read.

The restore is guaranteed except in one case it refuses instead: on PostgreSQL, entered outside a transaction, a callback that returns or throws with a transaction it opened still open makes within() throw LogicException — with or without a deadline, so tests without one catch it too. The restore would not survive that transaction's rollback (see Limitations). A thrown callback error becomes the LogicException's previous (translate() still finds a server timeout in it); the transaction is not rolled back, and after you roll it back the session's statement_timeout is not the one you had: reset it or discard the connection.

Entering while another Fiber has a within()/transaction() scope open on the same connection throws LogicException (see Limitations); nesting in one Fiber is allowed.

transaction(Context $context, callable $callback): mixed

Opens a transaction through PDO, runs the callback under the budget as within() does, commits when it returns and rolls back (and rethrows) when it throws. On PostgreSQL the limit is a SET LOCAL, which ends with the transaction on commit and on rollback alike — also after a timeout aborted it — so nothing is left on the session. MySQL and MariaDB session variables are not transactional: there the limit is set and restored around the callback.

$id = $timeout->transaction($context, static function () use ($pdo): int {
    $pdo->exec("INSERT INTO report (day) VALUES (CURRENT_DATE)");

    return (int) $pdo->lastInsertId();
});
  • A transaction already open throws LogicException: call within() inside it.
  • The callback must not commit, roll back or open a transaction itself.
  • It drives \PDO directly. With a layer that counts transactions itself (yiisoft/db), call within()/run() inside that layer's transaction instead.

run(string $sql, Context $context, callable $execute): mixed

Dialect-independent: $execute receives the SQL to send. MySQL and MariaDB get apply()'s per-statement limit, PostgreSQL runs $execute inside within() (and so refuses a transaction $execute leaves open), an unsupported dialect gets $sql unchanged. Returns what $execute returns; a terminated context throws before $execute runs.

$rows = $timeout->run($sql, $context, static fn(string $sql): array => $pdo->query($sql)->fetchAll());

translate(\Throwable $error, Context $context): \Throwable

Returns ContextExpiredException ($error as previous) when the error — or a PDOException in its previous chain — is the server's statement timeout and the context's deadline has passed or is within 5 ms (the server can fire early only by rounding the budget down to whole milliseconds); any other error — or a timeout while more budget is left, i.e. a limit set elsewhere — is returned unchanged.

yiisoft/db

yiisoft/db connections expose the PDO, and wrap driver errors in their own exception with the PDOException as previous; translate() takes the wrapper as is:

use Yiisoft\Db\Exception\Exception as DbException;

$timeout = StatementTimeout::for($db->getActivePdo());

try {
    $value = $timeout->run($sql, $context, static fn(string $sql): mixed => $db->createCommand($sql)->queryScalar());
} catch (DbException $error) {
    throw $timeout->translate($error, $context);   // finds the PDOException in the chain
}

This recipe is executed by tests/Integration/YiisoftDbIntegrationTest.php on MySQL, MariaDB and PostgreSQL. For a transaction, call run()/within() inside $db->transaction(...) — not transaction(), which would open one behind yiisoft/db's back.

Public API

Class Description
StatementTimeout for(PDO, ?Dialect), dialect(), apply(), within(), transaction(), run(), translate()
Dialect MySql, MariaDb, PostgreSql, Unsupported — detected from the driver name and, for mysql, the server version

Security

apply() only inserts a number it computed itself — never caller input — and returns SQL it cannot parse unchanged, so it cannot turn a valid statement into a different one. Bind user values as parameters as usual.

Exception: MySQL sql_mode=NO_BACKSLASH_ESCAPES. The MySQL scanner that finds the main SELECT treats \ inside a string literal as an escape, which is the server default. Under NO_BACKSLASH_ESCAPES a literal ending in a backslash ('C:\') is read as unterminated, and the hint can land inside a later string literal — changing that literal's value. Do not use apply() on MySQL connections in that mode (use within()), or keep backslashes out of literals by binding such values as parameters. A budget that came from another process must already be capped (see rasuvaeff/context's WireCodec).

Examples

See examples/ for runnable scripts. Examples are expected to execute without fatal errors and stay aligned with the documented public API.

Script Shows Needs server?
rewrite.php What apply() produces for each dialect no
server.php A 300 ms budget stopping a heavy query on a live server yes (CONTEXT_DB_DSN)

Development

No PHP/Composer on the host — run in Docker via the composer:2 image:

docker run --rm -v "$PWD":/app -w /app composer:2 composer install
docker run --rm -v "$PWD":/app -w /app composer:2 composer build
docker run --rm -v "$PWD":/app -w /app composer:2 composer cs:fix
docker run --rm -v "$PWD":/app -w /app composer:2 composer test
docker run --rm -v "$PWD":/app -w /app composer:2 composer release-check

Integration suite against live servers (CONTEXT_DB_TEST = mysql, mariadb or pgsql; CONTEXT_DB_HOST, CONTEXT_DB_PORT, CONTEXT_DB_NAME, CONTEXT_DB_PASSWORD override the defaults 127.0.0.1, 3306/3307/5432, context, context):

CONTEXT_DB_TEST=mysql vendor/bin/testo --suite=Integration

Or with Make:

make install
make build
make cs-fix
make test
make test-coverage
make mutation
make release-check

make test-coverage and make mutation bootstrap pcov inside the composer:2 container because the base image has no coverage driver.

License

BSD-3-Clause