Infinity Timestamps Mark Open Ends
The literals infinity and -infinity are valid timestamps that compare past every finite moment, with isfinite separating open ends.
Code
-- '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
SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.SPDX License LookupSearch common SPDX license identifiers and see permissions, conditions, limitations, copyleft strength, and OSI or FSF approval status.
FAQ
- Which relations are involved here?
- The references are `subscriptions`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.