Skip to content

pg_lexo

Lexicographic position type for inserting and reordering list items

pg_lexo · Lexicographic position type for inserting and reordering list items

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3530pg_lexopg_lexo0.6.0TYPEMITRust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d--NoYesNoYesnono
Release/package 0.6.1 ships SQL/control version 0.6.0. Objects use the chosen installation schema; the old lexo schema was removed. superuser=false is not trusted=true. PGSTY patches lexo_next for native-type ordering and empty tables, and lexo_rebalance to preserve order independently of text collation.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.6.019 18 17 16 15pg_lexo-
RPMPIGSTY0.6.119 18 17 16 15pg_lexo_$v-
DEBPIGSTY0.6.119 18 17 16 15postgresql-$v-pg-lexo-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
el8.aarch64N/APIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
el9.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
el9.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
el10.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
el10.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
d12.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
d12.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
d13.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
d13.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
u22.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
u22.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
u24.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
u24.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
u26.x86_64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
u26.aarch64PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1PIGSTY 0.6.1N/A
PackageVersionOSORGSIZEFile URL
pg_lexo_190.6.1el9.x86_64pigsty934.0 KiBpg_lexo_19-0.6.1-2PGSTY.el9.x86_64.rpm
pg_lexo_190.6.1el9.aarch64pigsty902.1 KiBpg_lexo_19-0.6.1-2PGSTY.el9.aarch64.rpm
pg_lexo_190.6.1el10.x86_64pigsty934.3 KiBpg_lexo_19-0.6.1-2PGSTY.el10.x86_64.rpm
pg_lexo_190.6.1el10.aarch64pigsty903.0 KiBpg_lexo_19-0.6.1-2PGSTY.el10.aarch64.rpm
postgresql-19-pg-lexo0.6.1d12.x86_64pigsty634.9 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~bookworm_amd64.deb
postgresql-19-pg-lexo0.6.1d12.aarch64pigsty554.8 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~bookworm_arm64.deb
postgresql-19-pg-lexo0.6.1d13.x86_64pigsty635.2 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~trixie_amd64.deb
postgresql-19-pg-lexo0.6.1d13.aarch64pigsty555.2 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~trixie_arm64.deb
postgresql-19-pg-lexo0.6.1u22.x86_64pigsty705.7 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~jammy_amd64.deb
postgresql-19-pg-lexo0.6.1u22.aarch64pigsty655.2 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~jammy_arm64.deb
postgresql-19-pg-lexo0.6.1u24.x86_64pigsty691.6 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~noble_amd64.deb
postgresql-19-pg-lexo0.6.1u24.aarch64pigsty650.5 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~noble_arm64.deb
postgresql-19-pg-lexo0.6.1u26.x86_64pigsty688.5 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~resolute_amd64.deb
postgresql-19-pg-lexo0.6.1u26.aarch64pigsty648.1 KiBpostgresql-19-pg-lexo_0.6.1-2PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_lexo_180.6.1el8.x86_64pigsty378.5 KiBpg_lexo_18-0.6.1-1PGSTY.el8.x86_64.rpm
pg_lexo_180.6.1el8.aarch64pigsty265.6 KiBpg_lexo_18-0.6.1-1PGSTY.el8.aarch64.rpm
pg_lexo_180.6.1el9.x86_64pigsty928.3 KiBpg_lexo_18-0.6.1-2PGSTY.el9.x86_64.rpm
pg_lexo_180.6.1el9.aarch64pigsty896.3 KiBpg_lexo_18-0.6.1-2PGSTY.el9.aarch64.rpm
pg_lexo_180.6.1el10.x86_64pigsty929.7 KiBpg_lexo_18-0.6.1-2PGSTY.el10.x86_64.rpm
pg_lexo_180.6.1el10.aarch64pigsty896.9 KiBpg_lexo_18-0.6.1-2PGSTY.el10.aarch64.rpm
postgresql-18-pg-lexo0.6.1d12.x86_64pigsty632.3 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~bookworm_amd64.deb
postgresql-18-pg-lexo0.6.1d12.aarch64pigsty553.3 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~bookworm_arm64.deb
postgresql-18-pg-lexo0.6.1d13.x86_64pigsty632.5 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~trixie_amd64.deb
postgresql-18-pg-lexo0.6.1d13.aarch64pigsty553.5 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~trixie_arm64.deb
postgresql-18-pg-lexo0.6.1u22.x86_64pigsty703.1 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~jammy_amd64.deb
postgresql-18-pg-lexo0.6.1u22.aarch64pigsty654.0 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~jammy_arm64.deb
postgresql-18-pg-lexo0.6.1u24.x86_64pigsty693.6 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~noble_amd64.deb
postgresql-18-pg-lexo0.6.1u24.aarch64pigsty648.6 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~noble_arm64.deb
postgresql-18-pg-lexo0.6.1u26.x86_64pigsty685.2 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~resolute_amd64.deb
postgresql-18-pg-lexo0.6.1u26.aarch64pigsty646.8 KiBpostgresql-18-pg-lexo_0.6.1-2PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_lexo_170.6.1el8.x86_64pigsty378.4 KiBpg_lexo_17-0.6.1-1PGSTY.el8.x86_64.rpm
pg_lexo_170.6.1el8.aarch64pigsty265.6 KiBpg_lexo_17-0.6.1-1PGSTY.el8.aarch64.rpm
pg_lexo_170.6.1el9.x86_64pigsty923.7 KiBpg_lexo_17-0.6.1-2PGSTY.el9.x86_64.rpm
pg_lexo_170.6.1el9.aarch64pigsty894.5 KiBpg_lexo_17-0.6.1-2PGSTY.el9.aarch64.rpm
pg_lexo_170.6.1el10.x86_64pigsty924.8 KiBpg_lexo_17-0.6.1-2PGSTY.el10.x86_64.rpm
pg_lexo_170.6.1el10.aarch64pigsty895.1 KiBpg_lexo_17-0.6.1-2PGSTY.el10.aarch64.rpm
postgresql-17-pg-lexo0.6.1d12.x86_64pigsty631.3 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~bookworm_amd64.deb
postgresql-17-pg-lexo0.6.1d12.aarch64pigsty551.3 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~bookworm_arm64.deb
postgresql-17-pg-lexo0.6.1d13.x86_64pigsty630.2 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~trixie_amd64.deb
postgresql-17-pg-lexo0.6.1d13.aarch64pigsty551.6 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~trixie_arm64.deb
postgresql-17-pg-lexo0.6.1u22.x86_64pigsty700.2 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~jammy_amd64.deb
postgresql-17-pg-lexo0.6.1u22.aarch64pigsty650.5 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~jammy_arm64.deb
postgresql-17-pg-lexo0.6.1u24.x86_64pigsty690.6 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~noble_amd64.deb
postgresql-17-pg-lexo0.6.1u24.aarch64pigsty645.8 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~noble_arm64.deb
postgresql-17-pg-lexo0.6.1u26.x86_64pigsty682.3 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~resolute_amd64.deb
postgresql-17-pg-lexo0.6.1u26.aarch64pigsty644.6 KiBpostgresql-17-pg-lexo_0.6.1-2PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_lexo_160.6.1el8.x86_64pigsty378.4 KiBpg_lexo_16-0.6.1-1PGSTY.el8.x86_64.rpm
pg_lexo_160.6.1el8.aarch64pigsty265.6 KiBpg_lexo_16-0.6.1-1PGSTY.el8.aarch64.rpm
pg_lexo_160.6.1el9.x86_64pigsty921.6 KiBpg_lexo_16-0.6.1-2PGSTY.el9.x86_64.rpm
pg_lexo_160.6.1el9.aarch64pigsty888.5 KiBpg_lexo_16-0.6.1-2PGSTY.el9.aarch64.rpm
pg_lexo_160.6.1el10.x86_64pigsty921.9 KiBpg_lexo_16-0.6.1-2PGSTY.el10.x86_64.rpm
pg_lexo_160.6.1el10.aarch64pigsty889.2 KiBpg_lexo_16-0.6.1-2PGSTY.el10.aarch64.rpm
postgresql-16-pg-lexo0.6.1d12.x86_64pigsty628.7 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~bookworm_amd64.deb
postgresql-16-pg-lexo0.6.1d12.aarch64pigsty550.5 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~bookworm_arm64.deb
postgresql-16-pg-lexo0.6.1d13.x86_64pigsty628.7 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~trixie_amd64.deb
postgresql-16-pg-lexo0.6.1d13.aarch64pigsty550.6 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~trixie_arm64.deb
postgresql-16-pg-lexo0.6.1u22.x86_64pigsty698.9 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~jammy_amd64.deb
postgresql-16-pg-lexo0.6.1u22.aarch64pigsty649.2 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~jammy_arm64.deb
postgresql-16-pg-lexo0.6.1u24.x86_64pigsty690.1 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~noble_amd64.deb
postgresql-16-pg-lexo0.6.1u24.aarch64pigsty645.4 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~noble_arm64.deb
postgresql-16-pg-lexo0.6.1u26.x86_64pigsty681.6 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~resolute_amd64.deb
postgresql-16-pg-lexo0.6.1u26.aarch64pigsty642.5 KiBpostgresql-16-pg-lexo_0.6.1-2PGSTY~resolute_arm64.deb

Source

pig build pkg pg_lexo;		# build rpm/deb

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_lexo;		# install by package name

pig install pg_lexo -v 19;   # install for PG 19
pig install pg_lexo -v 18;   # install for PG 18
pig install pg_lexo -v 17;   # install for PG 17
pig install pg_lexo -v 16;   # install for PG 16

Create this extension with:

CREATE EXTENSION pg_lexo;

Usage

Sources:

pg_lexo 0.6.0 provides a Base62 lexo type for stable user-controlled ordering. It generates short values before, after, or between existing positions, allowing an item to move without renumbering every row. The type has comparison operators plus default B-tree and hash operator classes.

Core Workflow

CREATE EXTENSION pg_lexo;

CREATE TABLE items (
    id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    title text NOT NULL,
    position lexo NOT NULL UNIQUE DEFERRABLE
);

INSERT INTO items (title, position)
VALUES ('first', lexo_first());

INSERT INTO items (title, position)
SELECT 'last', lexo_after(position)
FROM items ORDER BY position DESC LIMIT 1;

INSERT INTO items (title, position)
VALUES ('middle', lexo_between('H'::lexo, 'I'::lexo));

SELECT * FROM items ORDER BY position;

lexo_first() starts at H. lexo_after(lexo), lexo_before(lexo), and lexo_between(lexo, lexo) generate neighboring positions; either argument to lexo_between may be NULL to represent an open end. Inputs contain only 0-9, A-Z, and a-z, whose ASCII order matches the type’s ordering.

Table Helpers

lexo_next(table, position_column, key_column, key_value) finds the current maximum and returns the next position, optionally within a group. lexo_add_column(table, column) adds a lexo column. lexo_rebalance(table, position_column, key_column, key_value) rewrites matching rows with evenly distributed positions and returns the number updated.

The helpers issue dynamic SQL and require the caller to have the corresponding table privileges. Rebalancing updates every selected row and should run with application writers coordinated. If positions have a unique constraint, use a deferrable constraint and defer it within the rebalancing transaction to allow intermediate values to overlap. Position generation itself does not reserve a value: concurrent sessions can derive the same position, so enforce a suitable unique constraint and retry or serialize conflicting moves. When no Base62 value exists between adjacent positions (or before the minimum), rebalance first.

Version 0.6 Boundary

Version 0.6 removed the dedicated lexo schema. The former lexo.lexorank type became lexo, and schema-qualified functions such as lexo.first() became names such as lexo_first(). Older examples are incompatible until migrated. Release/package 0.6.1 retains SQL/control version 0.6.0 and targets PostgreSQL 16–18 through pgrx 0.16.1. Pigsty packages patch lexo_next to use native ordering instead of the unavailable max(lexo) aggregate, and keep lexo_rebalance ordering independent of text collation.

Table reader