Skip to content

Prevent long table names from aliasing cached writes - #1

Draft
kafaichoi wants to merge 2 commits into
masterfrom
codex/bounded-write-statement-names
Draft

kafaichoi wants to merge 2 commits into
masterfrom
codex/bounded-write-statement-names

Conversation

@kafaichoi

@kafaichoi kafaichoi commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Description

Ecto's operation prefix and suffix can make generated statement names exceed PostgreSQL's default 63-byte limit, even for valid table names. Postgrex caches the full name, so different client keys can alias one server statement on a connection, causing parameter errors or silent wrong-table writes.

  • Bound oversized names using an ASCII source hint where available plus a hash. Names that fit remain unchanged.
  • Assert destination rows for ordinary and bulk inserts, with Unicode, byte-boundary, and explicit cache-option coverage.

Based on the diagnosis and fix in elixir-ecto/ecto_sql#751 by aiboupeter. Explicit overlong cache_statement overrides remain outside scope.

Security Implications

Prevents unintended-table writes from generated-name aliasing; database permissions are unchanged.

How to Test

Local results with locked dependencies: Elixir 1.20.5, OTP 29, PostgreSQL 18.6, using an isolated database.

  • ECTO_ADAPTER=pg mix test integration_test/pg/prepare_test.exs --seed 0: 5 passed; the ordinary and bulk destination assertions fail on the base.
  • mix test --seed 0: 707 passed.
  • ECTO_ADAPTER=pg mix test --seed 0: 499 passed, 8 excluded.

Compiler warnings and storage-fixture stderr were emitted. Older runtime/database combinations and MySQL/MSSQL integration were not run locally. GitHub CI currently reports no checks.

Long source names can alias on PostgreSQL while remaining distinct in
Postgrex, allowing cached writes to target the wrong table.
Keep generated keys within the limit; see elixir-ecto#751.
Hash-only names hide the table in statement-name-only diagnostics.
Retain an ASCII hint without reintroducing server-side truncation.
Keep caller-supplied names and names already within the limit unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant