pg_rational

pg_rational

pg_rational : bigint fractions

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3720
pg_rational
pg_rational
0.0.2
TYPE
MIT
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--
No
Yes
No
Yes
no
no
Relationships
See Also
prefix
semver
unit
pgpdf
pglite_fusion
md5hash
asn1oid
roaringbitmap

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
MIXED
0.0.2
18
17
16
15
14
pg_rational-
RPM
PIGSTY
0.0.2
18
17
16
15
14
pg_rational_$v-
DEB
PGDG
0.0.2
18
17
16
15
14
postgresql-$v-rational-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
el8.aarch64
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
el9.x86_64
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
el9.aarch64
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
el10.x86_64
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
el10.aarch64
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
PIGSTY 0.0.2
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
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
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
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
u24.aarch64
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2
PGDG 0.0.2

Source

pig build pkg pg_rational;		# build rpm

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install pg_rational;		# install via package name, for the active PG version

pig install pg_rational -v 18;   # install for PG 18
pig install pg_rational -v 17;   # install for PG 17
pig install pg_rational -v 16;   # install for PG 16
pig install pg_rational -v 15;   # install for PG 15
pig install pg_rational -v 14;   # install for PG 14

Create this extension with:

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 updated on