Skip to content

saturated_int

Integer data type with saturation arithmetic that clamps overflow to int4 bounds.

pg-saturated_int · Integer data type with saturation arithmetic that clamps overflow to int4 bounds.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
101218saturated_intpg-saturated_int0.0.1TYPEMITC

PostgreSQL · 10, 11, 12, 13, 14, 15, 16

Usage

Sources:

saturated_int version 0.0.1 supplies a signed 32-bit integer type whose input and arithmetic clamp overflow to -2147483648 or 2147483647 instead of raising integer out of range.

Core Workflow

CREATE EXTENSION saturated_int;

SELECT 999999999999999::saturated_int;
SELECT 2147483640::saturated_int + 100::saturated_int;
SELECT (-2147483648)::saturated_int / (-1)::saturated_int;

The results of these expressions are respectively 2147483647, 2147483647, and 2147483647.

Objects

  • saturated_int: int4-sized custom type with a range of -2147483648 through 2147483647.
  • Comparison operators: =, <>, <, <=, >, and >=.
  • Arithmetic operators: +, -, *, /, and %.
  • sum(saturated_int): aggregate with a saturated accumulator.
  • B-tree and hash operator classes for indexes, ordering, grouping, and equality lookup.

Cast and Arithmetic Notes

Casts from bigint clamp to the type’s range. Assignment casts exist between integer and saturated_int, but the extension does not define mixed-type operators: explicitly cast both operands when comparing or calculating with built-in integers.

Saturation is a data-model choice, not merely error handling: distinct out-of-range inputs collapse to the same boundary value, and aggregate overflow also clamps. Division by zero still raises an error. Use the type only when this loss of magnitude is intended. No preload or restart is required.

Table reader