Common SQL Errors

PostgreSQL Error - 22013 invalid_preceding_or_following_size Error Explained and Fixed

August 4, 2025

PostgreSQL raises invalid_preceding_or_following_size (SQLSTATE 22013) when a window frame's PRECEDING or FOLLOWING offset is negative, null, or reverses frame order.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is the invalid_preceding_or_following_size error?

invalid_preceding_or_following_size appears when a window function frame declares an impossible size, such as a negative or larger-than-partition offset. Correct the frame clause so the starting point is not after the ending point, and ensure numeric offsets are positive constants.

Error Highlights

Typical Error Message

invalid_preceding_or_following_size

Error Type

Window Function Error

Language

PostgreSQL

Symbol

invalid_preceding_or_following_size

Error Code

22013

SQL State

Explanation

Table of Contents

ERROR: 22013: invalid preceding or following size

PostgreSQL throws this SQLSTATE when a window frame definition contains an illegal numeric offset. The planner rejects frames where the starting row is after the ending row, the offset is negative, or the value is not a constant integer.

What is the invalid_preceding_or_following_size error?

The error indicates that the PRECEDING or FOLLOWING value in a ROWS or RANGE clause violates frame rules.

PostgreSQL requires non-negative integers and a start position that is not logically after the end position.

When does PostgreSQL raise invalid_preceding_or_following_size?

The engine checks frame validity at parse time. Any SELECT using a window function with an invalid frame specification will fail immediately, regardless of data volume or indexes.

What Causes This Error?

Negative numbers in PRECEDING or FOLLOWING cause rejection. Using non-constant expressions like a column reference also fails.

Specifying a larger PRECEDING value on the right side of BETWEEN than on the left reverses the frame and triggers the error.

How to Fix invalid_preceding_or_following_size

Rewrite the frame so offsets are non-negative constants and the left bound is less than or equal to the right bound.

If you need dynamic offsets, compute them outside the frame and use RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING instead.

Common Scenarios and Solutions

Ordering rows by date and trying ROWS BETWEEN 5 FOLLOWING AND 1 PRECEDING reverses the frame. Swap the offsets or use ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING.

A negative offset like ROWS BETWEEN -1 PRECEDING AND CURRENT ROW must be replaced with 1 FOLLOWING or removed entirely.

Best Practices to Avoid This Error

Always use positive integers in PRECEDING or FOLLOWING. Document window frames in code reviews. Test complex analytic queries in an IDE such as Galaxy, which validates syntax instantly and highlights frame errors before execution.

Related Errors and Solutions

frame_offset_out_of_range appears when RANGE frames exceed bigint range. window_frame_too_large happens when the computed frame size exceeds work_mem.

Both are solved by reducing frame size or increasing resources.

.

Common Causes

Related Errors

FAQs

Does this error depend on PostgreSQL version?

The rule exists in all supported versions, but PostgreSQL 15 adds clearer wording.

Can I use a parameter as a frame offset?

No. PostgreSQL requires constant integers. Use CASE logic or generate the SQL dynamically.

Why does RANGE behave differently from ROWS?

RANGE operates on values, not row counts, so offset semantics differ and may trigger related errors.

How does Galaxy help avoid this error?

Galaxy's live syntax checker flags invalid window frames instantly, reducing trial-and-error debugging time.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo