# 22014 — invalid_argument_for_ntile_function

> Source-backed reference for PostgreSQL SQLSTATE 22014.
---

## At a glance {#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`.

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| 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 | `—` |

<!-- source facts: data/errcodes/22014.json -->
<!-- END SQLSTATE FACTS -->

## Meaning {#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 {#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 {#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 {#messages}
- Primary, `ERROR`: `argument of ntile must be greater than zero`.
- The fixed guard adds no DETAIL or HINT.

## Versions {#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 {#related}
[`22013`](../22013/), [`22016`](../22016/)

## Sources {#sources}
[`src/backend/utils/adt/windowfuncs.c#L411-L475`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/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](../data/evidence/22014.json) retains the primary and scope.
