Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 22013: Invalid Preceding/Following Size

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 22013 (invalid_preceding_or_following_size) is raised when a window frame clause in an analytic function specifies a negative or otherwise invalid bound for ROWS, RANGE, or GROUPS. The fix is to make every frame bound a non-negative integer (or a valid expression) and to ensure the frame is defined in the correct order: ROWS BETWEEN ... PRECEDING AND ... FOLLOWING. You cannot use a negative number, and you cannot put a FOLLOWING bound before a PRECEDING bound. Check the query that triggered the error, correct the numeric literals or variables, and rerun it.

What You'll Need

  • Access to the SQL statement that produced error 22013 (from your application logs, pgAdmin query history, or the psql terminal).
  • A PostgreSQL client such as psql, pgAdmin, or DBeaver to test the corrected query.
  • Permission to run the query against the target database, or a development copy where you can experiment safely.
  • The PostgreSQL version in use (run SELECT version();) because window frame syntax and support for GROUPS mode differ between major versions.

Step-by-Step

  1. Locate the failing statement. Find the exact SQL that raised the error. In psql the previous statement is available with \errverbose or in the server log. In an application, check the stack trace or the query builder that generated the SQL.
  2. Identify the window frame clause. Look for OVER ( in the query. Inside it, find the frame definition: ROWS BETWEEN ..., RANGE BETWEEN ..., or GROUPS BETWEEN .... If the frame is omitted, PostgreSQL uses the default frame and error 22013 should not appear, so the error almost always points to an explicit frame.
  3. Check each bound for a negative value. The bounds n PRECEDING and n FOLLOWING require n to be zero or a positive integer. A value like -1 PRECEDING or -3 FOLLOWING triggers 22013. Replace negative numbers with zero or a positive value that reflects the intended window.
  4. Check the order of the bounds. The frame must go from an earlier bound to a later bound. ROWS BETWEEN 2 FOLLOWING AND 1 PRECEDING is invalid because the start is after the end. Rewrite it so the PRECEDING bound comes first, for example ROWS BETWEEN 1 PRECEDING AND 2 FOLLOWING.
  5. Check expressions and variables. If the bound comes from a parameter or a computed expression, confirm it evaluates to a non-negative integer at runtime. A common cause is a variable that is NULL or negative due to a calculation bug. Add a guard in the application or use GREATEST(value, 0) where appropriate.
  6. Verify the frame mode matches the ORDER BY. RANGE frames require an ORDER BY with exactly one column for offset bounds, and the column type must support addition and subtraction (numeric, date, timestamp, interval). If you use RANGE with a non-orderable type or multiple ORDER BY columns and offsets, rewrite as ROWS or adjust the ORDER BY.
  7. Confirm GROUPS support. GROUPS frame mode was added in PostgreSQL 11. If you are on an older major version, GROUPS is not recognised and you should use ROWS or RANGE instead. Check with SELECT version();.
  8. Test the corrected query in a transaction. Run BEGIN;, execute the fixed statement, inspect the results, then ROLLBACK; if you only wanted to validate. This avoids side effects when the statement is part of an UPDATE or DELETE.
  9. Update the source of the SQL. If the query is generated by an ORM or a reporting tool, fix the template or configuration that produced the invalid bound, not just the ad-hoc query. Otherwise the error will return on the next run.
  10. Redeploy and monitor. After the fix is in place, watch the PostgreSQL log for a recurrence of SQLSTATE 22013. If it reappears, capture the full statement and repeat from step 2.

Common Mistakes

  • Assuming the error refers to a data value. It refers to the frame bound in the window clause, not to rows in the table.
  • Using a negative number to mean "unbounded". Use UNBOUNDED PRECEDING or UNBOUNDED FOLLOWING instead.
  • Swapping PRECEDING and FOLLOWING in the BETWEEN clause. The earlier bound must come first.
  • Passing a parameter directly into the frame without validating it. Bound values must be non-negative integers.
  • Using RANGE with offset bounds on a text or boolean ORDER BY column. Use ROWS for those types.
  • Editing the query in one place but leaving the ORM template unchanged, so the error returns.

Tips

  • Start with the simplest frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Add offsets only when you need them.
  • Use ROWS when you want a physical number of rows; use RANGE when you want a value-based range such as the last seven days.
  • Keep frame bounds as literals where possible. Dynamic bounds are harder to validate and are a frequent source of 22013.
  • Log the full SQL text alongside the SQLSTATE so you can reproduce the failure quickly.
  • Test window queries against a small sample table before running them on production data.

Troubleshooting

If the error persists after correcting the frame, capture the exact statement with \errverbose in psql or from the server log and confirm you are editing the same text that runs. Check that no trigger, view, or function rewrites the query with a different frame. If the bound is supplied by an application, log the value at runtime; a negative or NULL value there will reproduce the error even when the SQL text looks correct. If you are on an older PostgreSQL major version, confirm that the frame mode you are using is supported; GROUPS requires PostgreSQL 11 or later. When the query runs but returns unexpected results, review the ORDER BY inside the OVER clause, since the frame is applied relative to that ordering. If a managed service is involved, verify that the client library is not rewriting the SQL; some ORMs generate their own window clauses. As a last resort, simplify the query to a single window function with a fixed frame, confirm it runs, then add complexity back one piece at a time.

Frequently Asked Questions

What does SQLSTATE 22013 mean?

It is the PostgreSQL error code for an invalid preceding or following size in a window frame. The frame bound is negative or ordered incorrectly.

Can I use a negative number with PRECEDING?

No. Use zero or a positive integer, or UNBOUNDED PRECEDING for no lower limit.

Does the default window frame cause this error?

No. The default frame is valid. Error 22013 comes from an explicit frame clause in the query.

Why does RANGE fail but ROWS work?

RANGE with offsets requires a single ORDER BY column of a type that supports arithmetic, such as numeric or timestamp. ROWS counts physical rows and works with any ORDER BY type.

Is GROUPS supported in my version?

GROUPS frame mode was added in PostgreSQL 11. Run SELECT version(); to check your major version.

Will fixing the query in the database stop the error in my app?

Only if the app uses the same SQL. If an ORM or report builder generates the query, update its template or configuration as well.