PostgreSQL Error 22013: Invalid Preceding or Following Size in Window Function

PostgreSQL error 22013 occurs when the offset value specified in the PRECEDING or FOLLOWING clause of a window function frame is invalid. This typically happens when the offset is negative, NULL, or of an incompatible data type. The database engine cannot construct a meaningful window frame with such values and immediately raises this error.

Top 3 Causes

1. Negative Offset Value

The most common cause is passing a negative integer as a frame offset. SQL standards and PostgreSQL both require that PRECEDING and FOLLOWING offsets must be zero or a positive integer.