22014 — invalid_argument_for_ntile_function
At a glance
ntile received a non-positive bucket count. The fixed window-function path reports argument of ntile must be greater than zero with SQLSTATE 22014.
| Field | Value |
|---|---|
| SQLSTATE | 22014 |
| Condition | invalid_argument_for_ntile_function |
| Status | active |
| Known present by | 8.4.0 |
| Locked snapshots | 9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3 |
| Macros | ERRCODE_INVALID_ARGUMENT_FOR_NTILE |
| Aliases | — |
Meaning
While context->ntile is still zero, before a successful initialization, window_ntile evaluates the bucket argument. A NULL argument returns NULL before setting that state, so a later row may evaluate the argument again; a value less than or equal to zero raises 22014, while a positive count initializes the state and is reused for later rows. An empty input produces no window-function row to evaluate and is not this error.
Diagnosis
Inspect the resolved ntile argument for the affected partition and distinguish three cases: positive count, NULL count, and non-positive count. A NULL count gives a NULL result on rows that are evaluated; an empty partition gives no result at all. Neither should be reported as the non-positive-argument error.
Response
Pass a positive integer bucket count after checking the expression that supplies it. Use COALESCE or another fallback only when the resulting bucket policy is intentional. The fixed guard raises ERROR; in an explicit transaction roll back or return to an existing savepoint before retrying, while autocommit can retry only the corrected statement.
Messages
- Primary,
ERROR:argument of ntile must be greater than zero. - The fixed guard adds no DETAIL or HINT.
Versions
The locked catalogue records this condition from 8.4.0; fixed source coverage is PostgreSQL 18.6. The cited behavior is the window-function implementation, not a general argument validator.
Related
Sources
src/backend/utils/adt/windowfuncs.c#L411-L475 shows the partition-row count, NULL early return, positive-count guard, and bucket calculation. Runtime verification is not_run; this source evidence is not a runtime observation. The structured evidence record retains the primary and scope.