﻿# PostgREST null placement does not make null an ordered comparison value

PostgREST nullsfirst and nullslast place unknown values in output order; SQL comparisons still return unknown for null, so continuation filters need a separate null-group rule.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/guide-postgrest-null-order-filter-boundary/
Language: en

## Decide where unscored documents belong

A source list is ordered by an optional review score. Some documents have no score yet. PostgREST's [ordering reference](https://docs.postgrest.org/en/stable/references/api/tables_views.html) supports `nullsfirst` and `nullslast` alongside direction. Choose the policy deliberately: an unscored source might belong after reviewed sources, regardless of whether scored values ascend or descend. Null placement is part of the API's displayed ordering rather than an automatic interpretation of what missing review means.

## Keep direction and null placement independent

PostgreSQL's [sorting guide](https://www.postgresql.org/docs/current/queries-order.html) places nulls last by default for ascending order and first for descending order. Changing direction can therefore move the unscored group even when the client did not explicitly change its business policy. An explicit setting such as `score.desc.nullslast` documents the intended combination. Preserve the same null policy when changing page size or adding secondary ordering fields.

## Do not turn placement into ordinary comparison

The [comparison reference](https://www.postgresql.org/docs/current/functions-comparison.html) defines ordinary comparisons involving null as unknown, rather than true or false. Placing null after every known score does not make `score` greater than the last known value for filter purposes. A continuation filter containing only an ordinary greater-than condition can omit the null group despite an ordering that visually places it later. Use dedicated null predicates when the requested subset includes those rows.

## Define the transition between groups

For a client traversing valued rows and then unscored rows, define when it enters the null group and how it orders records inside that group. If null comes first, define the opposite transition. Carry enough continuation state to distinguish the group instead of treating a missing score as a substitute numeric boundary. [Task contracts](/journal/learn-codex-task-contract/) identify whether every source must remain reachable, including sources that have not received a review score.

## Inspect selection separately from presentation

Compare the explicit ordering with the filters selecting each page, then inspect representative unscored sources. [Tool observations](/journal/learn-codex-tool-observation/) should retain the group transition and actual predicates. A response can be correctly sorted while excluding rows the client expected its continuation to reach. This distinction directs the remedy toward selection logic rather than repeatedly changing null placement or assuming that missing scores were silently converted into ordinary sortable numbers.
