Skip to content

pg_nanotime

Nanosecond timestamp and fixed-duration interval types

pg_nanotime · Nanosecond timestamp and fixed-duration interval types

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
3870pg_nanotimepg_nanotime0.4.0TYPEPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
Upstream META.json marks 0.4.0 as testing. Only global clocks require preload and reset on restart.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.4.019 18 17 16 15pg_nanotime-
RPMPIGSTY0.4.019 18 17 16 15pg_nanotime_$v-
DEBPIGSTY0.4.019 18 17 16 15postgresql-$v-pg-nanotime-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
el8.aarch64N/APIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
el9.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
el9.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
el10.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
el10.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
d12.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
d12.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
d13.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
d13.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
u22.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
u22.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
u24.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
u24.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
u26.x86_64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
u26.aarch64PIGSTY 0.4.0PIGSTY 0.4.0PIGSTY 0.4.0N/AN/A
PackageVersionOSORGSIZEFile URL
pg_nanotime_190.4.0el9.x86_64pigsty118.6 KiBpg_nanotime_19-0.4.0-1PGSTY.el9.x86_64.rpm
pg_nanotime_190.4.0el9.aarch64pigsty121.6 KiBpg_nanotime_19-0.4.0-1PGSTY.el9.aarch64.rpm
pg_nanotime_190.4.0el10.x86_64pigsty119.6 KiBpg_nanotime_19-0.4.0-1PGSTY.el10.x86_64.rpm
pg_nanotime_190.4.0el10.aarch64pigsty122.3 KiBpg_nanotime_19-0.4.0-1PGSTY.el10.aarch64.rpm
postgresql-19-pg-nanotime0.4.0d12.x86_64pigsty111.5 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~bookworm_amd64.deb
postgresql-19-pg-nanotime0.4.0d12.aarch64pigsty113.0 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~bookworm_arm64.deb
postgresql-19-pg-nanotime0.4.0d13.x86_64pigsty111.0 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~trixie_amd64.deb
postgresql-19-pg-nanotime0.4.0d13.aarch64pigsty112.7 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~trixie_arm64.deb
postgresql-19-pg-nanotime0.4.0u22.x86_64pigsty119.2 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~jammy_amd64.deb
postgresql-19-pg-nanotime0.4.0u22.aarch64pigsty122.1 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~jammy_arm64.deb
postgresql-19-pg-nanotime0.4.0u24.x86_64pigsty115.6 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~noble_amd64.deb
postgresql-19-pg-nanotime0.4.0u24.aarch64pigsty119.5 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~noble_arm64.deb
postgresql-19-pg-nanotime0.4.0u26.x86_64pigsty115.0 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~resolute_amd64.deb
postgresql-19-pg-nanotime0.4.0u26.aarch64pigsty113.5 KiBpostgresql-19-pg-nanotime_0.4.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_nanotime_180.4.0el8.x86_64pigsty115.7 KiBpg_nanotime_18-0.4.0-1PGSTY.el8.x86_64.rpm
pg_nanotime_180.4.0el8.aarch64pigsty117.1 KiBpg_nanotime_18-0.4.0-1PGSTY.el8.aarch64.rpm
pg_nanotime_180.4.0el9.x86_64pigsty118.6 KiBpg_nanotime_18-0.4.0-1PGSTY.el9.x86_64.rpm
pg_nanotime_180.4.0el9.aarch64pigsty121.6 KiBpg_nanotime_18-0.4.0-1PGSTY.el9.aarch64.rpm
pg_nanotime_180.4.0el10.x86_64pigsty119.6 KiBpg_nanotime_18-0.4.0-1PGSTY.el10.x86_64.rpm
pg_nanotime_180.4.0el10.aarch64pigsty122.3 KiBpg_nanotime_18-0.4.0-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-nanotime0.4.0d12.x86_64pigsty111.4 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-nanotime0.4.0d12.aarch64pigsty112.9 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-nanotime0.4.0d13.x86_64pigsty111.0 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~trixie_amd64.deb
postgresql-18-pg-nanotime0.4.0d13.aarch64pigsty112.8 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~trixie_arm64.deb
postgresql-18-pg-nanotime0.4.0u22.x86_64pigsty119.2 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~jammy_amd64.deb
postgresql-18-pg-nanotime0.4.0u22.aarch64pigsty122.0 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~jammy_arm64.deb
postgresql-18-pg-nanotime0.4.0u24.x86_64pigsty115.6 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~noble_amd64.deb
postgresql-18-pg-nanotime0.4.0u24.aarch64pigsty119.5 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~noble_arm64.deb
postgresql-18-pg-nanotime0.4.0u26.x86_64pigsty115.0 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~resolute_amd64.deb
postgresql-18-pg-nanotime0.4.0u26.aarch64pigsty113.5 KiBpostgresql-18-pg-nanotime_0.4.0-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_nanotime_170.4.0el8.x86_64pigsty115.7 KiBpg_nanotime_17-0.4.0-1PGSTY.el8.x86_64.rpm
pg_nanotime_170.4.0el8.aarch64pigsty117.0 KiBpg_nanotime_17-0.4.0-1PGSTY.el8.aarch64.rpm
pg_nanotime_170.4.0el9.x86_64pigsty118.6 KiBpg_nanotime_17-0.4.0-1PGSTY.el9.x86_64.rpm
pg_nanotime_170.4.0el9.aarch64pigsty121.5 KiBpg_nanotime_17-0.4.0-1PGSTY.el9.aarch64.rpm
pg_nanotime_170.4.0el10.x86_64pigsty119.6 KiBpg_nanotime_17-0.4.0-1PGSTY.el10.x86_64.rpm
pg_nanotime_170.4.0el10.aarch64pigsty122.2 KiBpg_nanotime_17-0.4.0-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-nanotime0.4.0d12.x86_64pigsty111.4 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-nanotime0.4.0d12.aarch64pigsty112.9 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-nanotime0.4.0d13.x86_64pigsty111.0 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~trixie_amd64.deb
postgresql-17-pg-nanotime0.4.0d13.aarch64pigsty112.7 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~trixie_arm64.deb
postgresql-17-pg-nanotime0.4.0u22.x86_64pigsty124.1 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~jammy_amd64.deb
postgresql-17-pg-nanotime0.4.0u22.aarch64pigsty127.1 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~jammy_arm64.deb
postgresql-17-pg-nanotime0.4.0u24.x86_64pigsty115.4 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~noble_amd64.deb
postgresql-17-pg-nanotime0.4.0u24.aarch64pigsty119.4 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~noble_arm64.deb
postgresql-17-pg-nanotime0.4.0u26.x86_64pigsty114.9 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~resolute_amd64.deb
postgresql-17-pg-nanotime0.4.0u26.aarch64pigsty113.5 KiBpostgresql-17-pg-nanotime_0.4.0-1PGSTY~resolute_arm64.deb

Source

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

pig install pg_nanotime -v 19;   # install for PG 19
pig install pg_nanotime -v 18;   # install for PG 18
pig install pg_nanotime -v 17;   # install for PG 17

Create this extension with:

CREATE EXTENSION pg_nanotime;

Usage

Sources:

pg_nanotime 0.3.0 provides nanosecond timestamp and interval types on PostgreSQL 17–18 for event timestamps, latency measurements and externally supplied nanosecond data. Nanosecond storage precision does not imply nanosecond clock accuracy.

Core Workflow

CREATE EXTENSION pg_nanotime;
CREATE TABLE events (at nanotimestamptz DEFAULT clock_ns(), body text);
INSERT INTO events (body) VALUES ('received');
SELECT at, at::timestamptz FROM events;
TypeStorage and meaning
nanotimestamptz16 bytes; an instant displayed in the session time zone, with PostgreSQL’s full timestamp range
nanotimestamp16 bytes; a timestamp without a time zone
nanotimestamptz88 bytes; signed Unix nanoseconds with a narrower 1677–2262 range
nanointerval16 bytes; a fixed elapsed duration, without calendar months

Conversion to built-in timestamps floors to microseconds, including before the Unix epoch. Casting an instant to bigint yields Unix nanoseconds and fails if the result exceeds bigint range. Do not use double precision to transport large integer nanosecond values.

Durations, Aggregates and Series

Subtracting timestamps produces a nanosecond duration. Version 0.2.0 adds interval aggregates and series generation:

WITH samples(ns) AS (VALUES (1250::bigint), (2751::bigint))
SELECT sum(nanointerval_from_ns(ns)) AS total,
       avg(nanointerval_from_ns(ns)) AS mean,
       abs(nanointerval_from_ns(-1250::bigint)) AS magnitude
FROM samples;

SELECT *
FROM generate_series(
  '2026-10-24 00:00:00+00'::nanotimestamptz,
  '2026-10-24 00:00:00.000000003+00'::nanotimestamptz,
  nanointerval_from_ns(1::bigint)
);

sum and avg accumulate exactly, ignore NULL values and return NULL for no input; overflow raises an error. The average rounds to the nearest nanosecond, with ties away from zero. Series include a reachable stop value, support negative steps and reject a zero step.

Use nanointerval_from_ns(bigint) for exact integer input and nanointerval_from_ns(numeric) for larger values; fractional input rounds to nanoseconds. The double-precision overload becomes inexact beyond 2^53 nanoseconds.

Other useful objects include time_bucket_ns for fixed-duration buckets, extract_ns for precise fields, date_trunc and timezone. Explicitly type duration literals: an untyped string in timestamp arithmetic may resolve to PostgreSQL’s calendar-aware interval. Timestamp types support B-tree, hash and BRIN minmax indexes; the interval type supports B-tree and hash.

Clocks and Boundaries

clock_ns() reads the wall clock on each call. now_ns() caches an extension clock reading for the transaction and statement_ns() for the statement; these readings are not the same sampling instant as PostgreSQL’s built-in timestamps. Ordinary clocks can move backwards with the system clock.

clock_ns_monotonic() produces strictly increasing values within one backend. clock_ns_global() does so across backends in one PostgreSQL instance and requires this configuration followed by a server restart:

shared_preload_libraries = 'pg_nanotime'

Preserve other libraries already in the setting. Ordinary types and clocks do not require preloading. Global monotonic state is in shared memory and does not survive restart; it is not a durable sequence or distributed ordering mechanism. Install as a superuser. The extension is relocatable and is not marked trusted.

nanotime_clock_status() reports clock and monotonic-state diagnostics. nanotime.clock_source defaults to realtime; the optional tai source depends on the operating system’s configured TAI offset. TAI values still use UTC-style output, so do not mix these sources in the same column.

Upgrade and Migration

Upstream marks 0.3.0 as a PGXN testing release. After replacing library and SQL files, update each database:

ALTER EXTENSION pg_nanotime UPDATE TO '0.3.0';

The column-conversion helper nanotime_migrate_column defaults to the 16-byte type, even for a bigint source, and changes the column with ALTER TABLE, rebuilding its indexes. Review the conversion and locking cost before running it. TimescaleDB integration is optional; its integer-time and continuous-aggregate requirements are separate from merely storing nanosecond timestamps.

0.3.0 Range Types and Timescale Helper

Version 0.3.0 adds nanotstzrange and nanotsrange with multirange, GiST and exclusion-constraint support. It also adds nanotime_timescale_setup() for the optional TimescaleDB integration and extends the technical helpers for range construction and extraction. Existing timestamp and interval objects keep their prior semantics. Replace matching library and SQL files before running ALTER EXTENSION pg_nanotime UPDATE TO '0.3.0'; review any range indexes and the optional TimescaleDB setup separately.

Table reader