pg_nanotime
pg_nanotime : Nanosecond timestamp and fixed-duration interval types
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 3870 | pg_nanotime | pg_nanotime | 0.2.0 |
TYPE | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-r | No | Yes | No | Yes | yes | no |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.2.0 |
18 17 16 15 14 | pg_nanotime |
- |
| RPM | PIGSTY | 0.2.0 |
18 17 16 15 14 | pg_nanotime_$v |
- |
| DEB | PIGSTY | 0.2.0 |
18 17 16 15 14 | postgresql-$v-pg-nanotime |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| el8.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| el9.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| el9.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| el10.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| el10.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| d12.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| d12.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| d13.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| d13.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| u22.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| u22.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| u24.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| u24.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| u26.x86_64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
| u26.aarch64 | PIGSTY 0.2.0 | PIGSTY 0.2.0 | N/A | N/A | N/A |
Source
github.com/bunko666-cloud/pg_nanotime
pg_nanotime-0.2.0.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Create this extension with:
Usage
Sources:
- 0.2.0 README
- 0.2.0 control file
- 0.2.0 SQL objects
- Technical reference
- 0.2.0 changelog
- PGXN release metadata
pg_nanotime 0.2.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
| Type | Storage and meaning |
|---|---|
nanotimestamptz |
16 bytes; an instant displayed in the session time zone, with PostgreSQL’s full timestamp range |
nanotimestamp |
16 bytes; a timestamp without a time zone |
nanotimestamptz8 |
8 bytes; signed Unix nanoseconds with a narrower 1677–2262 range |
nanointerval |
16 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:
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:
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.2.0 as a PGXN testing release. After replacing library and SQL files, update each database:
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.