Incident brief
ntile argument must be positive
The ntile(n) window function divides rows into n ranked buckets and requires n to be a positive integer; zero or a negative bucket count raises this data exception.
What lands in your log
ERROR: argument of ntile must be greater than zero
In 10 seconds
- What triggers it
- Use ntile(n) OVER (...) with n set to zero or a negative value.
- Fix
- Pass a positive bucket count to ntile().
- Proof
- Reproduced on PostgreSQL 18.4 → A single windowed SELECT reproduces SQLSTATE 22014; ntile refuses the non-positive bucket count.
Fix
What to do right now
Application-level steps for this error.
- Pass a positive bucket count to ntile().
- Validate a dynamic bucket count (GREATEST(n, 1)) before it reaches the window function.
-- ntile needs a positive number of buckets.
SELECT ntile(2) OVER (ORDER BY g) FROM generate_series(1,3) g;For this error
See this error live on the server
Run these against the affected instance to confirm the diagnosis before you act.
ntile argument must be positive (22014) is statement-local: PostgreSQL does not retain the rejected value after the statement ends. Capture the exact bound value and statement position in application/server logs, then run this targeted validation before retrying.
Validate ntile bucket count
Validate the specific value/shape that can raise this SQLSTATE.
SELECT buckets, buckets <= 0 AS would_raise_22014
FROM (VALUES (0)) v(buckets);Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual, not paraphrased.
PostgreSQL 18 Documentation, Appendix A. PostgreSQL Error Codes (Table A.1, Class 22, Data Exception)
22014 → invalid_argument_for_ntile_functionRead the full section on postgresql.org →
Calling ntile with a non-positive argument
ntile needs a positive number of buckets, so ntile(0) raises 'argument of ntile must be greater than zero'.The session continues normally
ntile(0) failed on its own, outside a transaction, so PostgreSQL discards nothing and the next statement in session B runs clean.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab. The commands below are exactly what was executed.
- 1Use ntile(n) OVER (...) with n set to zero or a negative value.
- 2PostgreSQL evaluates the bucket-count argument as the window is processed.
- 3A non-positive argument aborts the statement with SQLSTATE 22014.
A quartile report used ntile() with a bucket count read from configuration, and a mis-set zero aborted the ranking query.
-- The error is self-contained in one statement; no schema is required.
SELECT 'no schema needed' AS setup_note;SELECT ntile(0) OVER (ORDER BY g) FROM generate_series(1,3) g;SELECT 'ok' AS session_after_error;What PostgreSQL actually returned
setup_note
------------------
no schema needed
(1 row)ERROR: argument of ntile must be greater than zero session_after_error
---------------------
ok
(1 row)The same ranking works once ntile is given a positive bucket count.
Without this
Before: a zero bucket count aborts
With this, tested
After: ntile(2) assigns buckets
- A second operational test: exact SQL, raw output, measured result, and engineer notes
Card required. Cancel before day 7 and you are not charged.
Connected
Everything this error touches
Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.
Verification
- Last verified
- 2026-07-24 (isolated lab, PostgreSQL 18.4)
- Verification scope
- Verified against PostgreSQL 18.4 in an isolated lab environment
- Audit status
- reviewed
Went further?
Pro unlocks the second lab proof
Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.