Skip to content

span

C data type for indexed document text spans, with casts, comparisons, B-tree/hash operator classes, aggregates, and accessors.

pg_span · C data type for indexed document text spans, with casts, comparisons, B-tree/hash operator classes, aggregates, and accessors.

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100240spanpg_span0.1.0TYPEPostgreSQLC

Usage

Sources:

span adds a compact data type for identifying a text interval inside a document. A value has the form document:begin-end, such as article-42:10-25, and can be stored, compared, grouped, and indexed.

Core Workflow

CREATE EXTENSION span;

CREATE TABLE annotations (
    annotation_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    location span NOT NULL,
    label text NOT NULL
);

INSERT INTO annotations(location, label) VALUES
    ('article-42:10-25', 'person'),
    ('article-42:30-44', 'place');

CREATE INDEX annotations_location_idx
ON annotations(location);

SELECT get_span_doc(location),
       get_span_begin(location),
       get_span_end(location),
       label
FROM annotations
ORDER BY location;

The default B-tree operator class supports =, <>, <, <=, >, and >=. A hash operator class supports equality and hash aggregation. The extension also defines min(span) and max(span) aggregates.

Constructors and Accessors

  • span(text) converts validated text to span; text(span) converts back.
  • is_span(text) checks whether text has a valid document and ordered offsets.
  • get_span_doc(span) returns the document identifier.
  • get_span_begin(span) and get_span_end(span) return integer offsets.
SELECT is_span('article-42:10-25'),
       span('article-42:10-25'),
       text('article-42:10-25'::span);

Caveats

This type models an identifier plus two integer offsets; it does not provide overlap, containment, union, or range-index operators. Ordering is useful for deterministic sorting and B-tree lookup, not interval-search semantics. Invalid forms, missing endpoints, and a begin value greater than the end are rejected. Confirm whether application offsets are bytes, characters, or another unit and keep that convention external to the type. Version 0.1.0 is old and fixed-length, so validate document-identifier length and PostgreSQL-version compatibility before migration. No preload or restart is required.

Table reader