pg_rational
bigint fractions
Repository
begriffs/pg_rational
https://github.com/begriffs/pg_rational
Source
pg_rational-0.0.2.tar.gz
pg_rational-0.0.2.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_rational | 0.0.2 | TYPE | MIT | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3720 | pg_rational | No | Yes | No | Yes | No | No | - |
| Related | prefix semver unit pgpdf pglite_fusion md5hash asn1oid roaringbitmap |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | MIXED | 0.0.2 | 1817161514 | pg_rational | - |
| RPM | PIGSTY | 0.0.2 | 1817161514 | pg_rational_$v | - |
| DEB | PGDG | 0.0.2 | 1817161514 | postgresql-$v-rational | - |
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
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.
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.