pg_rational

bigint fractions

Overview

PackageVersionCategoryLicenseLanguage
pg_rational0.0.2TYPEMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
3720pg_rationalNoYesNoYesNoNo-
Relatedprefix semver unit pgpdf pglite_fusion md5hash asn1oid roaringbitmap

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED0.0.21817161514pg_rational-
RPMPIGSTY0.0.21817161514pg_rational_$v-
DEBPGDG0.0.21817161514postgresql-$v-rational-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
d12.aarch64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
d13.x86_64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
d13.aarch64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
u22.x86_64
u22.aarch64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
u24.x86_64
u24.aarch64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2

Build

You can build the RPM packages for pg_rational using pig build:

pig build pkg pg_rational         # build RPM packages

Install

You can install pg_rational directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_rational;          # Install for current active PG version
pig ext install -y pg_rational -v 18  # PG 18
pig ext install -y pg_rational -v 17  # PG 17
pig ext install -y pg_rational -v 16  # PG 16
pig ext install -y pg_rational -v 15  # PG 15
pig ext install -y pg_rational -v 14  # PG 14
dnf install -y pg_rational_18       # PG 18
dnf install -y pg_rational_17       # PG 17
dnf install -y pg_rational_16       # PG 16
dnf install -y pg_rational_15       # PG 15
dnf install -y pg_rational_14       # PG 14
apt install -y postgresql-18-rational   # PG 18
apt install -y postgresql-17-rational   # PG 17
apt install -y postgresql-16-rational   # PG 16
apt install -y postgresql-15-rational   # PG 15
apt install -y postgresql-14-rational   # PG 14

Create Extension:

CREATE EXTENSION pg_rational;

Usage

pg_rational: precise fractional arithmetic in 64 bits

The pg_rational extension provides exact fractional arithmetic stored in 64 bits (same size as float), with support for Stern-Brocot trees for finding intermediate fractions.

CREATE EXTENSION pg_rational;

Data Types

  • rational: Fractional type (numerator/denominator)
  • ratt: Helper type for tuple coercion

Basic Arithmetic

SELECT '1/3'::rational + '2/7'::rational;  -- 13/21
SELECT '3/4'::rational * '2/3'::rational;  -- 1/2

Functions

-- Simplify fractions
SELECT rational_simplify('36/12');  -- 3/1

-- Find intermediate fraction (Stern-Brocot tree)
SELECT rational_intermediate('1/2', '2/3');  -- 3/5

Type Conversions

SELECT 0.263157894737::float::rational;  -- 5/19
SELECT '-1/2'::rational::float;          -- -0.5
SELECT 1::rational;                       -- 1/1

Dynamic Row Ordering

A key use case is maintaining sortable row order without renumbering:

CREATE TABLE todos (
    prio rational UNIQUE DEFAULT nextval('todos_seq')::integer,
    what text NOT NULL
);

-- Insert between items at priority 1 and 2
INSERT INTO todos VALUES (rational_intermediate('1', '2'), 'new task');
-- prio becomes 3/2, no other rows affected

Index Support

Btree and hash indexes are supported for rational columns.


Last Modified 2026-03-12: add pg extension catalog (95749bf)