Skip to content

🐛 Bug Report: Postgres adapter sends ROLLBACK before every BEGIN #14080

Open
utopia-php/database
#1006

Description

@JoshiJoshiJoshi

In short: Before every write transaction the adapter sends a ROLLBACK, even when nothing is open. Postgres answers each one with a warning in its log.

Impact:

  • One wasted round trip per write.
  • Under write load 99.7 % of the Postgres log is this warning, and with Docker's log rotation the log only covers a few minutes: real errors get lost.

👟 Reproduction steps

  1. Run Appwrite 2.3.0 with _APP_DB_ADAPTER=postgresql.

  2. Enable statement logging: ALTER SYSTEM SET log_statement = 'all'; SELECT pg_reload_conf();

  3. Create one row: POST /v1/tablesdb/{db}/tables/{table}/rows.

  4. docker logs appwrite-postgresql:

    LOG:  statement: ROLLBACK
    WARNING:  there is no transaction in progress
    LOG:  statement: BEGIN
    LOG:  statement: SET LOCAL statement_timeout = '15000ms'
    LOG:  statement: INSERT INTO "appwrite"."_1_database_1_collection_2" …
    LOG:  statement: COMMIT
    

The WARNING line is logged with default settings (no log_statement needed) on every write.

👍 Expected behavior

No ROLLBACK is sent when no transaction is open, and normal writes do not produce a server warning.

👎 Actual Behavior

Summary

SQL::startTransaction() cleans up the pooled connection before BEGIN:

  • If PDO reports an open transaction, it calls rollBack().
  • Otherwise it still executes a plain ROLLBACK (SQL.php#L75-L105, the prepare('ROLLBACK') at L84).

In the normal case no transaction is open, so every write transaction pays one extra round trip. PostgreSQL also answers every such ROLLBACK with:

WARNING:  there is no transaction in progress

Suggested fix

  • Trust the connection state: send ROLLBACK only when the driver or connection reports an open transaction (PDO::inTransaction()), or when the adapter's own counter is out of sync.
  • If a desynced proxy state must be covered (the reason for fix(transaction): tolerate desynced cleanup rollback on startTransaction utopia-php/database#895), do the cleanup once when a connection is taken from or returned to the pool, or after a reconnect, not before every BEGIN.
  • If PDO's own state is not trustworthy after a reconnect, reset the adapter's transaction counter in the reconnect path instead.

Impact on throughput is small (one short round trip per write). The main problem is the log flood.

Since when / regression?

Version numbers refer to utopia-php/database unless an Appwrite version is named.

  • The cleanup ROLLBACK has been in startTransaction() since 0.53.13. MariaDB ignores a ROLLBACK outside a transaction without a warning.
  • With PostgreSQL the warning appears in every Appwrite release that uses the Postgres adapter (2.0.0 onwards). It is not a regression.

Risks / things to consider for the fix

  • The desync case from fix(transaction): tolerate desynced cleanup rollback on startTransaction utopia-php/database#895 must stay covered: a connection that reconnected mid-transaction can report a transaction it no longer has, or the other way round. Moving the cleanup to the reconnect or pool-return path must not leave a connection inside an aborted transaction.
  • PDO::inTransaction() reflects the driver's view. If a proxy layer keeps its own counter, that counter must be reset on reconnect.

Related: utopia-php/database#895, #14075 (same test setup).

Related issues from the same analysis (Appwrite 2.3.0 with Postgres, same test setup):

🎲 Appwrite version

Version 2.3.0 (self-hosted)

💻 Operating system

Linux (Debian 12, Docker 29.8)

🧱 Your Environment

Self-hosted Appwrite 2.3.0 with _APP_DB_ADAPTER=postgresql, PostgreSQL 18.3 (appwrite/postgres:0.1.0), utopia-php/database 7.3.11. Test setup: Postgres, Redis and ClickHouse on one 4 vCPU node, the API on two separate 4 vCPU nodes, load from oha. The code in question lives in utopia-php/database; filed here because it ships with Appwrite and affects every Appwrite install that uses Postgres. Related test setup: #14075.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

Activity

  1. added
    product / databases
    product / self-hosted
    product / storage
    product / vcs
    on Oct 3, 2026
  2. subhu339 commented on Oct 4, 2026

    @subhu339

    Hey @JoshiJoshiJoshi 👋

    Opened a PR to address this in utopia-php/database#989.

    It drops the unconditional ROLLBACK fallback in SQL::startTransaction(), which gets rid of the extra round trip and Postgres log warnings while keeping the #895 desync handling intact.

  3. changed the title [-]🐛 Bug Report: Postgres adapter sends `ROLLBACK` before every `BEGIN` – one extra round trip and one server WARNING per write, flooding the Postgres log[/-] [+]🐛 Bug Report: Postgres adapter sends `ROLLBACK` before every `BEGIN`[/+] on Oct 4, 2026
  4. removed
    product / storage
    product / self-hosted
    product / vcs
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databases

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions