PG
PRO
22014ERRORTier 2 — Caution✅ HIGH confidence

invalid argument for ntile function

Category: Data ExceptionVersions: All Postgres versions

What this means

SQLSTATE 22014 is raised when the NTILE window function receives an argument that is not a positive integer — for example, zero, a negative number, or NULL.

Why it happens

  1. 1Calling NTILE(0) or NTILE with a negative or NULL argument
  2. 2A dynamic expression for the NTILE bucket count evaluates to a non-positive value

How to reproduce

NTILE with zero buckets.

trigger — this will ERROR
SELECT NTILE(0) OVER (ORDER BY salary) FROM employees;
ERROR: argument of ntile must be greater than zero

Fix 1: Ensure the NTILE argument is a positive integer

When the bucket count is computed dynamically.

fix
SELECT NTILE(GREATEST(bucket_count, 1)) OVER (ORDER BY salary)
FROM employees;

Why this works

GREATEST ensures the bucket count is at least 1, preventing the 22014 error.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 22 — Data Exception

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for window function argument validation. Stable across versions.

See also

📄 Reference pages

Window FunctionsNTILE
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →