From edbe4053d7b3d8302ff99a7e5933aed469ff8e33 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Thu, 29 Oct 2026 18:46:33 +0100 Subject: [PATCH 1/4] docs(database): have the server enforce maybe_single_row on writes Restructure the maybe_single_row spec into the template's sections. Writes send the single-object Accept header so a write affecting more than one row is rolled back, a 406 PGRST116 reporting 0 rows resolves to Nothing, and the error for more than one row on a read may carry PGRST116. --- .../using_modifiers/maybe_single_row.md | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md index b1fb0e62..ab0d35f8 100644 --- a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md +++ b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md @@ -1,21 +1,27 @@ # Expect At Most One Row -Enables query execution where the end result is one of three states: +## API -| Query Match | Result | -| ------------------ | -------------------- | -| No rows. | Success with Nothing | -| Exactly one row. | Success with Row | -| More than one row. | Failure with Error | +Spec: [PostgREST Documentation: Singular or Plural](https://docs.postgrest.org/en/latest/references/api/resource_representation.html#singular-plural) -Where: +- `GET` or `HEAD /{relation}` +- `POST`, `PATCH`, or `DELETE /{relation}` with `Prefer: return=representation` +- `GET` or `POST /rpc/{function}` -- **Nothing**: Idiomatic result for this SDK (e.g. `null`, `nil`, zero-value, etc.) -- **Row**: The single row result -- **Error**: A failure indication that the user can handle, as idiomatic for this SDK +## Behavior -This contrasts starkly with the behavior of [`single_row`](single_row.md) by: +The query resolves to one of three results: Nothing when no row matches, the Row when exactly one row matches, and an Error when more than one row matches. Nothing is the SDK's idiomatic empty value, such as `null`, `nil`, or a zero value. The Error is a failure indication that the user can handle, as idiomatic for this SDK, of the same type [`single_row`](single_row.md) raises. -- being less strict, supporting success paths for both 'no rows' and 'exactly one row' matches -- handling cardinality enforcement on the client-side (never surfacing [`PGRST116`](https://docs.postgrest.org/en/latest/references/errors.html#pgrst116)) -- sending the request with default HTTP `Accept` header (e.g. `application/json`, _not_ `application/vnd.pgrst.object+json` - see [Singular or Plural](https://docs.postgrest.org/en/latest/references/api/resource_representation.html#singular-plural)) +**Reads.** A `GET` or `HEAD` request on a relation, and every call to `/rpc/{function}` whatever its method, keeps the `Accept` header it sends without this modifier. The SDK counts the elements of the JSON array in the response body: an empty array resolves to Nothing, one element resolves to that element as the Row, and more than one raises the Error defined under Errors. The single-object `Accept` header would make the server answer a read that matches no row with HTTP 406, so the SDK keeps the default header and a lookup that finds nothing is not recorded as a failed request. A function call is counted on the client even when it writes, because the rows a function returns say nothing about the rows it changed. + +**Writes.** An insert, upsert, update, or delete on a relation sends `Accept: application/vnd.pgrst.object+json`, so the count is checked inside the transaction. The server rolls back a write that affects more than one row and answers HTTP 406 with `PGRST116`, which the SDK raises as the Error, and nothing is written. Counting on the client instead would let the write commit to every matched row before the Error reached the caller. A successful answer carries one JSON object, which the SDK returns as the Row. A write that affects no row is answered with HTTP 406 and `PGRST116` as well, and the SDK resolves it to Nothing as defined under Errors. + +## Errors + +- HTTP 406 with `PGRST116` on a write, whose `details` contains `0 rows` as whole words (the server sends `The result contains 0 rows`): the write affected no row, so the SDK resolves to Nothing instead of raising. Any other HTTP 406 on a write, including a `PGRST116` whose `details` reports more than one row, surfaces as the Error unchanged. +- More than one row on a read: the server answered with success, so the SDK builds the Error itself. It may carry HTTP status 406 and code `PGRST116`, the server's answer to more than one row on a write and on [`single_row`](single_row.md), so that a caller handles both modifiers with one check. It carries no other code. + +## Related + +- [Expect Single Row](single_row.md): exactly one row, enforced by the server for every method +- Select After Mutation (`database.mutate.select_after_mutation`): returns the written rows that this modifier counts From befc6c332a4bd2fd0bfbcffd8cf0c2c686182018 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Thu, 29 Oct 2026 18:49:10 +0100 Subject: [PATCH 2/4] docs(database): tighten maybe_single_row results and align single_row Define the result for an empty or non-array body, keep Accept parameters such as nulls=stripped on writes, and note in single_row that maybe_single_row delegates the count to the server only for writes. --- .../specs/database/using_modifiers/maybe_single_row.md | 6 +++--- .../specs/database/using_modifiers/single_row.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md index ab0d35f8..df6d0b40 100644 --- a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md +++ b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md @@ -5,16 +5,16 @@ Spec: [PostgREST Documentation: Singular or Plural](https://docs.postgrest.org/en/latest/references/api/resource_representation.html#singular-plural) - `GET` or `HEAD /{relation}` -- `POST`, `PATCH`, or `DELETE /{relation}` with `Prefer: return=representation` +- `POST`, `PATCH`, or `DELETE /{relation}` - `GET` or `POST /rpc/{function}` ## Behavior The query resolves to one of three results: Nothing when no row matches, the Row when exactly one row matches, and an Error when more than one row matches. Nothing is the SDK's idiomatic empty value, such as `null`, `nil`, or a zero value. The Error is a failure indication that the user can handle, as idiomatic for this SDK, of the same type [`single_row`](single_row.md) raises. -**Reads.** A `GET` or `HEAD` request on a relation, and every call to `/rpc/{function}` whatever its method, keeps the `Accept` header it sends without this modifier. The SDK counts the elements of the JSON array in the response body: an empty array resolves to Nothing, one element resolves to that element as the Row, and more than one raises the Error defined under Errors. The single-object `Accept` header would make the server answer a read that matches no row with HTTP 406, so the SDK keeps the default header and a lookup that finds nothing is not recorded as a failed request. A function call is counted on the client even when it writes, because the rows a function returns say nothing about the rows it changed. +**Writes.** An insert, upsert, update, or delete on a relation sends `application/vnd.pgrst.object+json` as the `Accept` media type, keeping any parameters the header already carries, such as `nulls=stripped`. The server then checks the count inside the transaction: it rolls back a write that affects more than one row and answers HTTP 406 with `PGRST116`, which the SDK raises as the Error. Counting on the client instead would let the write commit to every matched row before the Error reached the caller. A successful answer with a JSON object as its body resolves to that object as the Row, and one with an empty body, as for a write that does not request `Prefer: return=representation`, resolves to Nothing. A write that affects no row is answered with HTTP 406 and `PGRST116` as well, and the SDK resolves it to Nothing as defined under Errors. -**Writes.** An insert, upsert, update, or delete on a relation sends `Accept: application/vnd.pgrst.object+json`, so the count is checked inside the transaction. The server rolls back a write that affects more than one row and answers HTTP 406 with `PGRST116`, which the SDK raises as the Error, and nothing is written. Counting on the client instead would let the write commit to every matched row before the Error reached the caller. A successful answer carries one JSON object, which the SDK returns as the Row. A write that affects no row is answered with HTTP 406 and `PGRST116` as well, and the SDK resolves it to Nothing as defined under Errors. +**Reads.** Every other request, which is a `GET` or `HEAD` on a relation or a call to `/rpc/{function}` whatever its method, keeps the `Accept` header it sends without this modifier, so a read that matches no row is answered with an empty array rather than HTTP 406. The SDK counts the elements of a JSON array body: an empty array resolves to Nothing, one element resolves to that element as the Row, and more than one raises the Error defined under Errors. A body that is not a JSON array, such as the result of a function that returns a single value, resolves to that value as the Row, and an empty body, as for a `HEAD` request, resolves to Nothing. A function call is counted on the client even when the function writes, because the rows a function returns say nothing about the rows it changed. ## Errors diff --git a/packages/capability-matrix/specs/database/using_modifiers/single_row.md b/packages/capability-matrix/specs/database/using_modifiers/single_row.md index 8e846ef5..87957d90 100644 --- a/packages/capability-matrix/specs/database/using_modifiers/single_row.md +++ b/packages/capability-matrix/specs/database/using_modifiers/single_row.md @@ -15,5 +15,5 @@ Where: This contrasts starkly with the behavior of [`maybe_single_row`](maybe_single_row.md) by: - being more strict, with only the single success path for 'exactly one row' matches -- delegating cardinality enforcement to the PostgREST service as [`PGRST116`](https://docs.postgrest.org/en/latest/references/errors.html#pgrst116) +- delegating cardinality enforcement to the PostgREST service as [`PGRST116`](https://docs.postgrest.org/en/latest/references/errors.html#pgrst116) for every method, where `maybe_single_row` delegates it only for writes - sending the request with the 'Singular' HTTP `Accept` header (`application/vnd.pgrst.object+json` - see [Singular or Plural](https://docs.postgrest.org/en/latest/references/api/resource_representation.html#singular-plural)) From 8af462890e6f197624e6b9131a7fd8512133cdb3 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Thu, 29 Oct 2026 18:50:55 +0100 Subject: [PATCH 3/4] docs(database): restore the result table in maybe_single_row --- .../database/using_modifiers/maybe_single_row.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md index df6d0b40..324786c4 100644 --- a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md +++ b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md @@ -10,7 +10,19 @@ Spec: [PostgREST Documentation: Singular or Plural](https://docs.postgrest.org/e ## Behavior -The query resolves to one of three results: Nothing when no row matches, the Row when exactly one row matches, and an Error when more than one row matches. Nothing is the SDK's idiomatic empty value, such as `null`, `nil`, or a zero value. The Error is a failure indication that the user can handle, as idiomatic for this SDK, of the same type [`single_row`](single_row.md) raises. +The query resolves to one of three results: + +| Query Match | Result | +| ------------------ | -------------------- | +| No rows. | Success with Nothing | +| Exactly one row. | Success with Row | +| More than one row. | Failure with Error | + +Where: + +- **Nothing**: the SDK's idiomatic empty value, such as `null`, `nil`, or a zero value +- **Row**: the single row result +- **Error**: a failure indication that the user can handle, as idiomatic for this SDK, of the same type [`single_row`](single_row.md) raises **Writes.** An insert, upsert, update, or delete on a relation sends `application/vnd.pgrst.object+json` as the `Accept` media type, keeping any parameters the header already carries, such as `nulls=stripped`. The server then checks the count inside the transaction: it rolls back a write that affects more than one row and answers HTTP 406 with `PGRST116`, which the SDK raises as the Error. Counting on the client instead would let the write commit to every matched row before the Error reached the caller. A successful answer with a JSON object as its body resolves to that object as the Row, and one with an empty body, as for a write that does not request `Prefer: return=representation`, resolves to Nothing. A write that affects no row is answered with HTTP 406 and `PGRST116` as well, and the SDK resolves it to Nothing as defined under Errors. From c5a11a8ddb71dbb9566ddc5ae41c346ef208cf45 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Fri, 30 Oct 2026 09:52:47 +0100 Subject: [PATCH 4/4] docs(database): count maybe_single_row reads from Content-Range Counting body elements misread a function that returns an array and had nothing to count on a HEAD request. --- .../specs/database/using_modifiers/maybe_single_row.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md index 324786c4..07e41bd8 100644 --- a/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md +++ b/packages/capability-matrix/specs/database/using_modifiers/maybe_single_row.md @@ -26,7 +26,11 @@ Where: **Writes.** An insert, upsert, update, or delete on a relation sends `application/vnd.pgrst.object+json` as the `Accept` media type, keeping any parameters the header already carries, such as `nulls=stripped`. The server then checks the count inside the transaction: it rolls back a write that affects more than one row and answers HTTP 406 with `PGRST116`, which the SDK raises as the Error. Counting on the client instead would let the write commit to every matched row before the Error reached the caller. A successful answer with a JSON object as its body resolves to that object as the Row, and one with an empty body, as for a write that does not request `Prefer: return=representation`, resolves to Nothing. A write that affects no row is answered with HTTP 406 and `PGRST116` as well, and the SDK resolves it to Nothing as defined under Errors. -**Reads.** Every other request, which is a `GET` or `HEAD` on a relation or a call to `/rpc/{function}` whatever its method, keeps the `Accept` header it sends without this modifier, so a read that matches no row is answered with an empty array rather than HTTP 406. The SDK counts the elements of a JSON array body: an empty array resolves to Nothing, one element resolves to that element as the Row, and more than one raises the Error defined under Errors. A body that is not a JSON array, such as the result of a function that returns a single value, resolves to that value as the Row, and an empty body, as for a `HEAD` request, resolves to Nothing. A function call is counted on the client even when the function writes, because the rows a function returns say nothing about the rows it changed. +**Reads.** Every other request, which is a `GET` or `HEAD` on a relation or a call to `/rpc/{function}` whatever its method, keeps the `Accept` header it sends without this modifier, so a read that matches no row is answered with HTTP 200 rather than HTTP 406. The SDK takes the row count from the range before the `/` in the `Content-Range` response header, not from the body, because a function that returns a single array value, such as one declared `RETURNS int[]`, answers with a JSON array that the header counts as one row. A range of `*` is 0 rows, and a range of `{first}-{last}` is `last - first + 1` rows. A response without a `Content-Range` header in either form is counted by the elements of its body when the body is a JSON array, and as one row otherwise. + +0 rows resolves to Nothing, and more than one row raises the Error defined under Errors. One row resolves to the Row: the single element when the body is a JSON array with exactly one element, and the body itself otherwise. A function that returns a one-element array therefore resolves to that element, since its response cannot be told apart from a set of one row. A `HEAD` response has no body, so it resolves to Nothing for 0 rows or one row, and raises the Error for more than one. + +A function call is counted on the client even when the function writes, because the rows a function returns say nothing about the rows it changed. ## Errors