isfinite Separates Real From Infinite Values
Test dates, timestamps, and intervals with isfinite, which flags the infinity literals that stand in for open-ended ranges.
Code
-- isfinite 有 date、timestamp、interval 三个变体
SELECT
isfinite('2026-05-01'::date) AS plain_date,
isfinite('infinity'::date) AS far_future_date,
isfinite('-infinity'::timestamp) AS distant_past_ts,
isfinite('2026-05-01 09:30:00'::timestamp) AS real_moment,
isfinite('90 days'::interval) AS finite_interval;
-- 开放端点的有效期用 infinity 表示"没有截止日"
CREATE TABLE memberships (
member text PRIMARY KEY,
valid_from date NOT NULL,
valid_to date NOT NULL DEFAULT 'infinity'
);
INSERT INTO memberships VALUES ('ada', '2024-01-01');
-- isfinite 把"有截止日"与"永不过期"区分开
SELECT
member,
valid_to,
isfinite(valid_to) AS has_expiry,
CASE WHEN isfinite(valid_to)
THEN valid_to::text
ELSE 'no end date' END AS expiry_label
FROM memberships;What the code does
What runs here is SELECT / INSERT / CREATE — memberships are the objects involved.
The pieces that do the work: NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; DEFAULT values filling omitted columns.
Using it
The memberships table is the working surface here.
Key statements
SELECT
member,
valid_to,
isfinite(valid_to) AS has_expiry,
CASE WHEN isfinite(valid_to)
THEN valid_to::text
ELSE 'no end date' END AS expiry_label
FROM memberships;
CREATE TABLE memberships (
member text PRIMARY KEY,
valid_from date NOT NULL,
valid_to date NOT NULL DEFAULT 'infinity'
);
INSERT INTO memberships VALUES ('ada', '2024-01-01');
Interactive Tools for This Topic
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.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.Date Add/Subtract CalculatorAdd or subtract days, weeks, months, or years from a date, clamping month ends such as January 31 plus one month.Date Difference CalculatorCalculate the number of days, weeks, whole months, and years between two ISO dates, computed in UTC.
FAQ
- What objects does the SQL involve?
- `memberships` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.