Skip to content

fix(postgrest): escape quoted bulk column names - #1947

Closed
hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/postgrest-bulk-column-escaping
Closed

hsusul wants to merge 2 commits into
supabase:mainfrom
hsusul:fix/postgrest-bulk-column-escaping

Conversation

@hsusul

@hsusul hsusul commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Why

Closes #1944.

Bulk inserts and upserts fail for valid column names containing a double quote or backslash. _unique_columns wraps raw keys in quotes, so PostgREST parses a different name and returns PGRST204.

What changed

  • Escape each generated column identifier before joining the list.
  • Preserve the existing outer quotes, de-duplication, empty-batch behavior, and JSON body.
  • Add async request-boundary tests and generated sync coverage for both write methods and list/tuple batches.

Scope

This covers automatically generated bulk column identifiers. Caller-supplied raw select and conflict syntax are outside the change.

Blast radius

Both clients use the same helper. Plain and comma-containing names retain their existing wire format; quoted and backslashed names now preserve their literal values.

Verification

  • The new public-client tests produced 24 failed, 16 passed before the fix and 40 passed after it. They check literal query values, unchanged JSON bodies, and returned data.
  • The complete PostgREST suite passed 813 tests against isolated PostgreSQL 17.11 and PostgREST 16.5, using the repository SQL fixtures. The original live single-row/batch reproduction also succeeds after the fix; before it, both special-name batches failed while escaped HTTP controls returned 201.
  • make postgrest.mypy, make postgrest.build-sync, Ruff lint/format checks, uv build --package postgrest, and git diff --check passed. Regeneration left no additional changes.

Local checks ran on Python 3.13. Hosted CI and the other supported Python/service versions remain separate validation.

Summary by CodeRabbit

  • Bug Fixes
    • Bulk inserts and upserts now correctly handle column names containing quotes or backslashes, while preserving the original column names and data.

@hsusul
hsusul requested a review from spydon as a code owner November 3, 2026 16:00
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

Bulk column serialization now escapes backslashes and double quotes in column names. New synchronous and asynchronous tests cover bulk inserts and upserts with list and tuple inputs.

Changes: Bulk column escaping

Layer / File(s) Summary
Escape bulk column names and verify bulk writes
src/postgrest/src/postgrest/base_request_builder.py, src/postgrest/tests/_async/test_bulk_column_escaping.py, src/postgrest/tests/_sync/test_bulk_column_escaping.py
_unique_columns escapes backslashes and double quotes before quoting column names. Tests check encoded column parameters, request bodies, and response data for inserts and upserts in both clients.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • supabase/supabase-py#1939: It updates _unique_columns input handling and enables tuple row sequences, which the new tuple tests exercise.

Suggested reviewers: o-santi

Merge Risk: Minimal · up to 46fd2

No actionable merge-blocking issue is established for the bulk column escaping change. Merge after normal checks pass.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(postgrest): escape quoted bulk column names" matches the main change. The raw summary and objectives both describe escaping backslashes and double quotes in the generated quoted column …
Linked Issues check Passed Directly linked issue #1944 is the only active target. Its coding requirement is to escape generated quoted column identifiers for bulk insert and upsert, for list and tuple inputs, in both clients. T…
Out of Scope Changes check Passed The whole-PR diff against the merge base c725015 contains three changed files, per the change summary. The first is the production helper change in base_request_builder.py, which directly implements #…
Full details: Title check

Explanation

The title "fix(postgrest): escape quoted bulk column names" matches the main change. The raw summary and objectives both describe escaping backslashes and double quotes in the generated quoted column names used by bulk insert and upsert in _unique_columns, with new tests for list and tuple inputs. The title is short, specific, and uses a conventional commit prefix that names the affected package.

Full details: Linked Issues check

Explanation

Directly linked issue #1944 is the only active target. Its coding requirement is to escape generated quoted column identifiers for bulk insert and upsert, for list and tuple inputs, in both clients. The reviewed head meets this requirement. In src/postgrest/src/postgrest/base_request_builder.py, _unique_columns now escapes each key by replacing \ with \, then " with ", before wrapping it in the existing outer double quotes and joining. This order is correct for the quoted-value syntax the issue cites. Both clients use this shared helper, so the sync client needs no separate production change. The added tests in src/postgrest/tests/_async/test_bulk_column_escaping.py and src/postgrest/tests/_sync/test_bulk_column_escaping.py are parametrized over insert and upsert, list and tuple inputs, and the names a"b, c\d, a"b, plain, and a,b. They assert the literal columns query value, the unchanged JSON body, and the returned data. I read the sync test and the helper diff directly. I did not read the async test file or the pre_insert and pre_upsert call sites directly. Those paths rely on the issue text and the PR summary, which state that both write paths use _unique_columns. The PR reports a passing live PostgREST suite, which I did not re-run. Caller-supplied raw select and conflict syntax is unchanged, as the issue requires.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base c725015 contains three changed files, per the change summary. The first is the production helper change in base_request_builder.py, which directly implements #1944. The second and third are the async and sync bulk column escaping test files. These tests cover the requirement, so they are in scope. The change does not touch select, conflict, or raw filter syntax. The change does not alter the JSON body, empty-batch handling, or de-duplication. The str(key) call only affects non-string keys, which the existing behavior already covers. I found no unrelated change in the evidence I reviewed. I did not run a full file-level whole-PR diff, so this verdict relies on the change summary for files other than the helper.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Tip

🎯 Customize your risk assessments

Guide how CodeRabbit assesses merge risk and security architecture risk. Highlight sensitive changes and explain your security boundaries using Merge Risk Instructions and Security Risk Instructions in your review settings. Security architecture risk assessments require the Advanced plan or above.

✅ Automatic approvals, with your rules

Let CodeRabbit approve pull requests once required threads are resolved, pre-merge checks pass, and your risk limits are met. Enable Automatic approvals in review settings, with overrides for teams or file paths.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between c725015 and 46fd2be.

Files selected for processing: 3
  • src/postgrest/src/postgrest/base_request_builder.py
  • src/postgrest/tests/_async/test_bulk_column_escaping.py
  • src/postgrest/tests/_sync/test_bulk_column_escaping.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@spydon

spydon commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Superseded by #1948

@spydon spydon closed this Nov 3, 2026
spydon added a commit that referenced this pull request Nov 3, 2026
…1948)

## Why

Closes #1944. Alternative to #1947.

Bulk inserts and upserts send the row keys in the `columns` query
parameter, wrapped in double quotes but not escaped. A column name
containing `"` or `\` is then parsed as a different name by PostgREST,
which responds with `PGRST204`.

## What changed

- Extract the existing quote and escape logic from `sanitize_param` and
`sanitize_array_element` into a single `quote` helper in `utils.py`.
- Use `quote` in `_unique_columns`, so bulk column names get the same
escaping as filter values and array elements.
- Plain and comma-containing names keep their current wire format
(`"plain"`, `"a,b"`).

## Tests

- `test_quote` unit tests the helper directly.
- `test_bulk_write_columns` (async and generated sync) gains cases for
names containing a comma, a double quote, a backslash, and both.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved handling of column names and values containing commas,
quotation marks, or backslashes when requests are built. These
characters are now quoted and escaped consistently, reducing malformed
requests in bulk writes and parameter values across synchronous and
asynchronous operations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

PostgREST bulk writes do not escape quoted column names

2 participants