Skip to content

fix(postgres): rewrite executeRead + compat test fixes#48

Open
tamnd wants to merge 4 commits into
mainfrom
fix/postgres-compat-read
Open

fix(postgres): rewrite executeRead + compat test fixes#48
tamnd wants to merge 4 commits into
mainfrom
fix/postgres-compat-read

Conversation

@tamnd

@tamnd tamnd commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • Root cause fix: executeRead used pgx.SendBatch with QueryExecModeCacheDescribe. pgx sends Parse messages for ALL batch items before executing any, so PostgreSQL parsed the SELECT as the authenticator role before SET LOCAL ROLE ran, returning 42501 permission denied → HTTP 401 for every anonymous GET.

    Fix: open a BeginTx, call applySession (switches role + sets GUCs), then Query. The SELECT is parsed after the role is already set. Same sequential pattern as executeCallRead.

  • Backend driver registry: each backend package now calls backend.Register in its init() function; cmd/dbrest imports them as blank imports. Removes the switch in openBackend.

  • Mongo cleanup: remove unused project parameter from findDocs, simplify nil guard in executeUpdate/executeDelete.

  • Compat test fixes: add Carol (person 3) to the seed so the to-many embed test covers a parent row with an empty related collection. Update resetTestDB to preserve all 3 seed persons.

Commits

Commit What
87c2e16 fix(postgres): rewrite executeRead to avoid permission error on anonymous reads
747530c refactor: backend driver registry with self-registration via init()
aea1e99 fix(mongo): remove unused project param from findDocs, simplify nil guard
41016a2 fix(compat): add Carol to seed and update resetTestDB to preserve 3 persons

Test plan

  • go test ./compat/ -run TestCompatSummary reports 115/115 passed against live PostgREST + dbrest
  • go test ./... passes (existing unit tests)
  • curl http://localhost:3001/todos returns rows (not 401) with anonymous token

tamnd added 4 commits June 9, 2026 20:11
…mous reads

With pgx QueryExecModeCacheDescribe, SendBatch sends Parse messages for ALL
batch items before executing any. PostgreSQL parses SELECT as the authenticator
role (before SET LOCAL ROLE runs) and rejects with 42501 permission denied,
which the HTTP layer maps to 401 for anonymous requests.

Fix: open a BeginTx, call applySession (which switches role and sets GUCs),
then Query. The SELECT is parsed after the role is already set, so PostgreSQL
sees the correct role for permission checks.

This is the same sequential pattern already used in executeCallRead.
Remove the batchStreamResult / batchStreamRows types that relied on the
old SendBatch approach.
Add a lightweight driver registry (backend.Register / backend.Open) so each
backend package advertises itself without requiring a switch in main. Each
package calls backend.Register in its init() function; cmd/dbrest imports them
as blank imports. cmd/dbrest.openBackend shrinks to a three-liner.
…uard

findDocs no longer accepts a project parameter since no caller used it.
Initialise the rows field on bodyResult at construction so the trailing
nil-guard after executeDelete / executeUpdate is unnecessary.
…ersons

The to-many embed test (9.1) verifies that a parent row with no matching
children returns an empty collection instead of disappearing. Carol (person 3)
has no assignments, making her the test case for that path. Without Carol both
servers returned identical two-row responses, hiding any LEFT-JOIN regression.

resetTestDB now deletes persons with id > 3 (was > 2) so Carol survives between
test runs, and the seed comment is updated to reflect 3 persons.
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