The direct answer

Specify the complete ordering that chooses the limited row set, including null placement and a unique tie-breaker. A stable candidate view or a one-row limit does not identify which tied row is first. Preserve the selected row identity and current permission separately; an ordering policy supplies no query-work ceiling or authority.

Define the selection question before choosing a row count

A fictional AI report explains one permitted catalogue item at a time. The application chooses the lowest numeric priority, puts unknown priorities last and breaks ties by a unique record ID. Returning one row is useful only if that row belongs to the declared selection policy.

This worksheet selects a separate fixed top-one report over ordinary integer priorities and unique, non-null integer IDs. The trusted application owns the complete template, directions and limit. No caller supplies SQL, column choices, expressions or null policy. No query, database, schema, connection or result fixture is constructed.

The existing reporting profile already orders its own report IDs and limits its result. Keep that baseline. This new worksheet explains a different priority-based selection; it does not change the earlier execution or SQL composition contracts.

Give tied and missing priorities an explicit disposition

PostgreSQL 18’s sorting chapter says unsorted output order is unspecified. With multiple sort expressions, later values order rows tied on earlier ones. Direction and null-placement options apply independently to each column. The documented defaults put nulls first for descending order and last otherwise.

The application policy below chooses lower priorities first, unknown last and ascending ID as the final tie-breaker. Null denotes unknown priority in this invented business rule; it is not silently changed to zero or assigned a fabricated priority. Explicitly record that meaning rather than borrowing a database default as a business decision.

The selected types and comparisons must be independently supported. The sorting chapter’s operator-class qualification matters for custom types; those are outside this ordinary-integer profile. Actual type definitions, key uniqueness and template behavior are unknown here and block a real positive.

Work one boundary tie on paper

Invented permitted candidates — PROPOSED_UNRUN
RecordPriorityUnique ID
R-A51
R-B52
R-CUnknown / null3

Under the declared policy, R-A precedes R-B through the ID tie-breaker, and the unknown-priority candidate follows them. The paper top-one choice is therefore R-A. This is a derivation from invented values and the declared ordering, not a captured PostgreSQL result.

If the plan keeps only priority order, the boundary tie leaves no supported claim that R-A rather than R-B is first. A later prompt saying “take the best one” cannot repair that missing selection rule. Likewise, changing sort direction while relying on implicit null placement changes the reviewed proposal.

The example holds its candidate set fixed for discussion. Real candidate visibility, filtering and current rights need their own established evidence; a missing candidate can change the selected result even when the ordering contract is complete.

Separate visibility, ordering and complete result evidence

PostgreSQL’s LIMIT chapter says a positive count returns no more than that many rows, possibly fewer. It requires a unique ordering for a predictable subset and notes that limit choices influence query plans. A limit is not an implicit identity rule.

The read-view guide addresses which content is visible. This worksheet addresses which visible candidate the fixed selection policy chooses. An assertion about one cannot replace evidence for the other. Neither decides which rows the user may access now.

Require the actual adapter to preserve the selected report, candidate association, ordering policy and row identity through complete bounded capture. A count of one cannot catch the wrong one. Fewer rows, incomplete capture or an unavailable result must retain their own disposition instead of becoming an assumed successful explanation.

Keep a small selection receipt

Original top-one receipt — PROPOSED_UNRUN
FieldIndependent evidence required
Report and candidatesTrusted template revision, authorized predicate and established content view.
Business orderingPriority meaning, each direction, explicit unknown placement and final unique ID.
SubsetFixed positive limit, expected selection scope and exact returned identities.
Execution and releaseSupported types/adapter, complete capture, resource controls and current row/audience rights.

The useful positive makes the fixed R-A selection plan conditionally eligible only after every receipt field is independently established. A later actual result would still need its own supported association and release decision. No report was executed and no model received a row.

Actual database version, business data invariants, template, effective view, ID uniqueness, adapter, capture and current permission are unimplemented or unknown. Unknowns hold the positive; a plausible table or documentation version does not fill them.

Walk eight inert selection cards

All eight cards are PROPOSED_UNRUN
ConditionProposed disposition
1. Complete declared policy, admitted candidates and every prerequisite holdConditionally admit only the fixed top-one plan.
2. Priority is the only key and the boundary is tiedDo not claim which tied identity is first.
3. Candidate visibility is stable but ordering is absentHold the selected-identity claim.
4. Null placement or a direction changesRe-review business selection before reuse.
5. The final key repeats or its uniqueness is unknownHold the unique-order prerequisite.
6. A row limit is offered as the complete resource policyRequire independent work, memory, duration and output bounds.
7. Count matches but identity or complete capture does notReject the result association; count is insufficient.
8. Order matches but candidate scope or current rights failWithhold disclosure independently.

Limit the assurance to this bounded report

The selected bodies identify PostgreSQL 18 documentation, not an installed patch or inspected service. This guide supplies no SQL statement, pagination, cursor, offset workflow, locking scheme, custom comparison or repair query. It demonstrates no query-cost or performance ceiling; independently supported resource limits remain prerequisites.

No SQL was assembled or executed; no database, transaction, schema, adapter, runtime fixture, API or model operation occurred. Ordinary editorial file handling is separate. Boundary is prelaunch with planned request-path checks, not a shipped report selector or SQL sandbox. Its existing synthetic policy evidence covers different behavior.

Primary sources

Official references supporting the guidance. Examples in this guide are synthetic and are not reports of a production incident.

  1. PostgreSQL 18 — Sorting Rows (ORDER BY)
  2. PostgreSQL 18 — LIMIT and OFFSET