Infinity Timestamps Mark Open Ends

The literals infinity and -infinity are valid timestamps that compare past every finite moment, with isfinite separating open ends.

SqlData Integrity

Code

snippet
-- 'infinity' / '-infinity' are valid timestamps: infinite endpoints mark open ends
CREATE TABLE subscriptions (
    id integer PRIMARY KEY,
    valid_from timestamp NOT NULL,
    valid_to timestamp NOT NULL DEFAULT 'infinity'
);

INSERT INTO subscriptions VALUES
    (1, '2026-01-01', '2026-06-30'),
    (2, '2026-02-01', 'infinity');

-- 与任意未来时刻比较:open-ended rows 不会被 valid_to 过滤掉
SELECT id
FROM subscriptions
WHERE '2027-01-01'::timestamp >= valid_from
  AND '2027-01-01'::timestamp < valid_to
ORDER BY id;

-- isfinite 区分普通时刻与无穷端点
SELECT
    valid_to,
    isfinite(valid_to) AS finite
FROM subscriptions
ORDER BY id;

How it works

What runs here is SELECT / INSERT / CREATE — subscriptions are the objects involved.

Breaking the statements into their working parts: a WHERE clause that keeps only the rows a statement is meant to touch; an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; DEFAULT values filling omitted columns.

Putting it to use

The subscriptions table is the working surface here.

Core lines

SELECT id
FROM subscriptions
WHERE '2027-01-01'::timestamp >= valid_from
  AND '2027-01-01'::timestamp < valid_to
ORDER BY id;
CREATE TABLE subscriptions (
    id integer PRIMARY KEY,
    valid_from timestamp NOT NULL,
    valid_to timestamp NOT NULL DEFAULT 'infinity'
);
INSERT INTO subscriptions VALUES
    (1, '2026-01-01', '2026-06-30'),
    (2, '2026-02-01', 'infinity');

Interactive Tools for This Topic

FAQ

Which relations are involved here?
The references are `subscriptions`.