↑↓ select↵ open⌫ change scopeOpen full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

Wiki / SQLSTATE

22014 — invalid_argument_for_ntile_function

ntile received a non-positive bucket count. The fixed window-function path reports argument of ntile must be greater than zero with SQLSTATE 22014.

English manual coverage begins with PostgreSQL 8.1. Source history and runtime observations have their own recorded version boundaries.

Reading PG 18·Select a recorded version

aliases
Unknown
class name
Data Exception
condition name
invalid_argument_for_ntile_function
lang
en
sqlstate
22014

Definition PG 18

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.

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, 22016

Sources {#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.

Compare versions

SQLSTATE evidence and recorded runtime versions

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read the fixed errcodes row and macro.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "22014 is invalid_argument_for_ntile_function in SQLSTATE Class 22."
}
claims · resolved-path
{
  "claim_id": "resolved-path",
  "limits": "An empty input has no window-function row to evaluate.",
  "method": "Read the complete window_ntile initialization, NULL, guard, and distribution path.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.ntile.18.6"
  ],
  "statement": "When ntile state is still uninitialized, the function evaluates the bucket argument; NULL returns before initialization, non-positive raises 22014, and a positive count initializes and is reused for later rows."
}
claims · history
{
  "claim_id": "history",
  "limits": "",
  "method": "Use catalogue presence and history boundaries.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "The locked catalogue records 22014 from 8.4.0 without proving an exact implementation introduction date."
}
messages · message.nonpositive-buckets
{
  "limits": "",
  "message_id": "message.nonpositive-buckets",
  "path": "",
  "position": 0,
  "raw": {
    "id": "message.nonpositive-buckets",
    "primary_template": "argument of ntile must be greater than zero",
    "severity_source": "ERROR",
    "sources": [
      "src.ntile.18.6"
    ],
    "sqlstate": "22014"
  },
  "severity": "ERROR",
  "sources": [
    "src.ntile.18.6"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "argument of ntile must be greater than zero",
      "position": 0,
      "role": "",
      "template": "argument of ntile must be greater than zero"
    }
  ]
}
sources · src.errcodes.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L184"
}
sources · src.ntile.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/adt/windowfuncs.c",
  "position": 1,
  "sha256": "49214f9325dcdd73728d08574b0b6843e90822a18bdaf6b0d49139b1eaae6b63",
  "source_id": "src.ntile.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/adt/windowfuncs.c#L411-475"
}

Source references

Full definition and evidence JSON

Definition sources

center · PostgreSQL 18 · english-manuals:1d02a301edae7422387809250c4b141b3b301bcdd6b26136f86614db3de85d5a

Text language: en · english-manuals:1d02a301edae7422387809250c4b141b3b301bcdd6b26136f86614db3de85d5a