pg_rational
pg_rational
pg_rational : bigint fractions
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3720 | pg_rational | pg_rational | 0.0.2 | TYPE | MIT | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d-- | No | Yes | No | Yes | no | no |
| Relationships | |
|---|---|
| See Also | prefix semver unit pgpdf pglite_fusion md5hash asn1oid roaringbitmap |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| 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 / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
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 rpmInstall
Make sure PGDG and PIGSTY repo available:
pig repo add pgsql -u # add both repo and update cacheInstall 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 14Create this extension with:
CREATE EXTENSION pg_rational;Usage
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/2Functions
-- Simplify fractions
SELECT rational_simplify('36/12'); -- 3/1
-- Find intermediate fraction (Stern-Brocot tree)
SELECT rational_intermediate('1/2', '2/3'); -- 3/5Type Conversions
SELECT 0.263157894737::float::rational; -- 5/19
SELECT '-1/2'::rational::float; -- -0.5
SELECT 1::rational; -- 1/1Dynamic 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 affectedIndex Support
Btree and hash indexes are supported for rational columns.
Last updated on