Skip to content

pg_kv

Shared-memory key/value cache, token-bucket rate limits and sorted sets.

pg_kv · Shared-memory key/value cache, token-bucket rate limits and sorted sets.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2895pg_kvpg_kv0.1.0FEATPostgreSQLRust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd--NoYesYesYesnono
Relationships
Schemaskv
Early release, not production ready. Preload/restart and superuser installation required. Cluster-wide keyspace: nontransactional, not durable or replicated. Grant kv schema usage deliberately; prefixing keys is not tenant access control. Capacity eviction can reset rate-limit allowance.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.1.019 18 17 16 15pg_kv-
RPMPIGSTY0.1.019 18 17 16 15pg_kv_$v-
DEBPIGSTY0.1.019 18 17 16 15postgresql-$v-pg-kv-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el8.aarch64N/APIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el9.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
el10.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d12.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
d13.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u22.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u24.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.x86_64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
u26.aarch64PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0PIGSTY 0.1.0
PackageVersionOSORGSIZEFile URL
pg_kv_190.1.0el9.x86_64pigsty307.1 KiBpg_kv_19-0.1.0-2PGSTY.el9.x86_64.rpm
pg_kv_190.1.0el9.aarch64pigsty299.2 KiBpg_kv_19-0.1.0-2PGSTY.el9.aarch64.rpm
pg_kv_190.1.0el10.x86_64pigsty307.7 KiBpg_kv_19-0.1.0-2PGSTY.el10.x86_64.rpm
pg_kv_190.1.0el10.aarch64pigsty301.1 KiBpg_kv_19-0.1.0-2PGSTY.el10.aarch64.rpm
postgresql-19-pg-kv0.1.0d12.x86_64pigsty217.9 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~bookworm_amd64.deb
postgresql-19-pg-kv0.1.0d12.aarch64pigsty192.5 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~bookworm_arm64.deb
postgresql-19-pg-kv0.1.0d13.x86_64pigsty218.4 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~trixie_amd64.deb
postgresql-19-pg-kv0.1.0d13.aarch64pigsty193.5 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~trixie_arm64.deb
postgresql-19-pg-kv0.1.0u22.x86_64pigsty235.9 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~jammy_amd64.deb
postgresql-19-pg-kv0.1.0u22.aarch64pigsty222.1 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~jammy_arm64.deb
postgresql-19-pg-kv0.1.0u24.x86_64pigsty233.7 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~noble_amd64.deb
postgresql-19-pg-kv0.1.0u24.aarch64pigsty221.1 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~noble_arm64.deb
postgresql-19-pg-kv0.1.0u26.x86_64pigsty232.8 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~resolute_amd64.deb
postgresql-19-pg-kv0.1.0u26.aarch64pigsty220.9 KiBpostgresql-19-pg-kv_0.1.0-2PGSTY~resolute_arm64.deb

Source

pig build pkg pg_kv;		# 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_kv;		# install by package name

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

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pg_kv';

Create this extension with:

CREATE EXTENSION pg_kv;

Usage

Sources:

pg_kv 0.1.0 provides a shared-memory cache, counters, token-bucket rate limiting and sorted sets. This early release targets PostgreSQL 15–18 and is not production ready.

Core Workflow

Add the library to the existing preload list and restart PostgreSQL before installing the extension as a superuser:

shared_preload_libraries = 'pg_kv'
CREATE EXTENSION pg_kv;
SELECT kv.set('app:session:42', '{"user":1}', interval '30 minutes');
SELECT kv.get('app:session:42');
SELECT kv.ttl('app:session:42');
SELECT kv.incr('app:views:7');
SELECT kv.rate_limit('app:api:42', 100, interval '1 minute');
SELECT kv.zadd('app:leaderboard', 'player:9', 1520);
SELECT * FROM kv.zrange('app:leaderboard', 0, 9, reverse => true);
SELECT * FROM kv.stats();

Installation fails if the library was not preloaded. Grant schema kv usage only to intended callers. The keyspace is shared across every database in the cluster; prefixes organize keys but do not enforce tenant isolation.

Operations and Capacity

kv.ttl returns milliseconds, -1 for a permanent key and -2 for a missing key. kv.expire changes expiry, kv.del removes a key, and kv.rate_limit_info reports allowance and retry timing. Sorted sets also support kv.zscore, kv.zrank, kv.zrevrank, kv.zincrby, kv.zcard and kv.zrem.

Restart settings include pg_kv.max_entries, pg_kv.max_key_bytes, pg_kv.max_value_bytes, pg_kv.shards, pg_kv.sweep_interval_ms, pg_kv.max_zset_members and pg_kv.max_member_bytes. Storage is bounded; oversize values fail, and pressure can evict cached keys. kv.flush is an administrative operation unavailable to the public role by default.

Durability Boundary

Cache changes survive transaction rollback but disappear on a server restart or crash. They are not WAL logged or replicated; a standby has its own cache. Use this for disposable state, not authoritative records or a globally coordinated limiter across independent servers.

Table reader