-- Interval arithmetic and differences for jtimestamp: jtimestamp + interval, -- interval + jtimestamp, jtimestamp - interval, jtimestamp - jtimestamp, -- jdate - jtimestamp and jtimestamp - jdate; then the jdate operators with -- interval, time and timetz (jdate + interval and jdate - interval, jdate + time, -- jdate + timetz and their commutators), which are exactly the ones core has for -- date. The jtimestamptz operators and the time zone behaviour are in jt_dst. -- The month part of an interval is -- applied with Jalali month lengths, then days, then time, as core does for -- timestamp; the day and time parts are core's. Results are checked against -- hand-computed tables, against an independent SQL model of the month rule over -- every month of the range, and (for what does not involve months) against -- core's operators on the same instants as `timestamp`. Errors are shown as -- SQLSTATE only (a helper catches them), never as message text. -- -- Everything created here is temporary or dropped at the end: extension_schema -- runs DROP EXTENSION, which needs no column of our types left behind. SET DateStyle = 'ISO, YMD'; SET IntervalStyle = 'postgres'; SET timezone = 'UTC'; -- The text of the result of a one-value query, or "ERR ". CREATE FUNCTION pg_temp.val(q text) RETURNS text LANGUAGE plpgsql AS $f$ DECLARE r text; BEGIN EXECUTE q INTO r; RETURN coalesce(r, 'NULL'); EXCEPTION WHEN OTHERS THEN RETURN 'ERR ' || SQLSTATE; END $f$; -- --------------------------------------------------------------------- -- the operators in the catalog -- --------------------------------------------------------------------- -- all IMMUTABLE, STRICT and PARALLEL SAFE; + has its commutator, - has none SELECT o.oprname AS operator, o.oprleft::regtype AS left_type, o.oprright::regtype AS right_type, o.oprresult::regtype AS result_type, o.oprcode::text AS function, p.provolatile AS volatility, p.proisstrict AS strict, p.proparallel AS parallel, o.oprcom <> 0 AS has_commutator, o.oprnegate <> 0 AS has_negator FROM pg_operator o JOIN pg_proc p ON p.oid = o.oprcode WHERE o.oprname IN ('+', '-') AND 'jtimestamp'::regtype IN (o.oprleft, o.oprright) ORDER BY o.oprname COLLATE "C", o.oprleft::regtype::text COLLATE "C", o.oprright::regtype::text COLLATE "C"; operator | left_type | right_type | result_type | function | volatility | strict | parallel | has_commutator | has_negator ----------+------------+------------+-------------+------------------------+------------+--------+----------+----------------+------------- + | interval | jtimestamp | jtimestamp | interval_pl_jtimestamp | i | t | s | t | f + | jtimestamp | interval | jtimestamp | jtimestamp_pl_interval | i | t | s | t | f - | jdate | jtimestamp | interval | jdate_mi_jtimestamp | i | t | s | f | f - | jtimestamp | interval | jtimestamp | jtimestamp_mi_interval | i | t | s | f | f - | jtimestamp | jdate | interval | jtimestamp_mi_jdate | i | t | s | f | f - | jtimestamp | jtimestamp | interval | jtimestamp_mi | i | t | s | f | f (6 rows) -- the result types, as written in the operator table SELECT pg_typeof(jtimestamp '1403-05-15 10:00' + interval '1 day') AS ts_plus, pg_typeof(interval '1 day' + jtimestamp '1403-05-15 10:00') AS plus_ts, pg_typeof(jtimestamp '1403-05-15 10:00' - interval '1 day') AS ts_minus, pg_typeof(jtimestamp '1403-05-15 10:00' - jtimestamp '1403-05-14 10:00') AS ts_minus_ts, pg_typeof(jdate '1403-05-15' - jtimestamp '1403-05-14 10:00') AS d_minus_ts, pg_typeof(jtimestamp '1403-05-15 10:00' - jdate '1403-05-14') AS ts_minus_d; ts_plus | plus_ts | ts_minus | ts_minus_ts | d_minus_ts | ts_minus_d ------------+------------+------------+-------------+------------+------------ jtimestamp | jtimestamp | jtimestamp | interval | interval | interval (1 row) -- --------------------------------------------------------------------- -- the month rule: the table of Jalali month steps (a time of day rides along -- unchanged); "ERR 22008" is the range error past the last day -- --------------------------------------------------------------------- CREATE TEMP TABLE jta_months (ord serial, start text, months int, expected text); INSERT INTO jta_months (start, months, expected) VALUES ('1403-06-31', 1, '1403-07-30'), -- clamp: Mehr has 30 days ('1403-06-31', 6, '1403-12-30'), -- 1403 is leap ('1403-07-30', -1, '1403-06-30'), -- not 06-31: not invertible ('1403-12-30', 12, '1404-12-29'), -- 1404 is not leap ('1403-12-30', 60, '1408-12-30'), -- 1408 is leap ('1403-12-30', -12, '1402-12-29'), ('1403-12-30', -60, '1398-12-29'), ('1403-11-30', 1, '1403-12-30'), ('1404-11-30', 1, '1404-12-29'), ('1403-01-31', 11, '1403-12-30'), ('1403-01-31', 12, '1404-01-31'), ('0000-01-31', -1, '-0001-12-29'), ('0001-01-30', -1, '0000-12-30'), -- year 0 is leap in this implementation ('3177-12-29', -1, '3177-11-29'), ('3177-12-29', 1, 'ERR 22008'); -- 3178-01-29 is outside the range -- ts + n months, n months + ts, and ts - (-n) months all agree and give the -- expected value SELECT start, months, pg_temp.val(format($q$SELECT (jtimestamp %L + make_interval(months => %s))::text$q$, start || ' 13:45:30.123456', months)) AS result, pg_temp.val(format($q$SELECT (jtimestamp %L + make_interval(months => %s))::text$q$, start || ' 13:45:30.123456', months)) = CASE WHEN expected LIKE 'ERR%' THEN expected ELSE expected || ' 13:45:30.123456' END AS as_expected, pg_temp.val(format($q$SELECT (make_interval(months => %s) + jtimestamp %L)::text$q$, months, start || ' 13:45:30.123456')) = pg_temp.val(format($q$SELECT (jtimestamp %L + make_interval(months => %s))::text$q$, start || ' 13:45:30.123456', months)) AS commutes, pg_temp.val(format($q$SELECT (jtimestamp %L - make_interval(months => %s))::text$q$, start || ' 13:45:30.123456', -months)) = pg_temp.val(format($q$SELECT (jtimestamp %L + make_interval(months => %s))::text$q$, start || ' 13:45:30.123456', months)) AS minus_agrees FROM jta_months ORDER BY ord; start | months | result | as_expected | commutes | minus_agrees ------------+--------+-----------------------------+-------------+----------+-------------- 1403-06-31 | 1 | 1403-07-30 13:45:30.123456 | t | t | t 1403-06-31 | 6 | 1403-12-30 13:45:30.123456 | t | t | t 1403-07-30 | -1 | 1403-06-30 13:45:30.123456 | t | t | t 1403-12-30 | 12 | 1404-12-29 13:45:30.123456 | t | t | t 1403-12-30 | 60 | 1408-12-30 13:45:30.123456 | t | t | t 1403-12-30 | -12 | 1402-12-29 13:45:30.123456 | t | t | t 1403-12-30 | -60 | 1398-12-29 13:45:30.123456 | t | t | t 1403-11-30 | 1 | 1403-12-30 13:45:30.123456 | t | t | t 1404-11-30 | 1 | 1404-12-29 13:45:30.123456 | t | t | t 1403-01-31 | 11 | 1403-12-30 13:45:30.123456 | t | t | t 1403-01-31 | 12 | 1404-01-31 13:45:30.123456 | t | t | t 0000-01-31 | -1 | -0001-12-29 13:45:30.123456 | t | t | t 0001-01-30 | -1 | 0000-12-30 13:45:30.123456 | t | t | t 3177-12-29 | -1 | 3177-11-29 13:45:30.123456 | t | t | t 3177-12-29 | 1 | ERR 22008 | t | t | t (15 rows) -- --------------------------------------------------------------------- -- months first, then days, then time -- --------------------------------------------------------------------- -- 1403-06-30 + 1 month is 1403-07-30, one more day is 1403-08-01 (days first -- would give 06-31, then 07-30) SELECT jtimestamp '1403-06-30 10:00' + interval '1 month 1 day 2 hours' AS months_days_time, (jtimestamp '1403-06-30 10:00' + interval '1 month 1 day 2 hours') = (jtimestamp '1403-06-30 10:00' + interval '1 month' + interval '1 day' + interval '2 hours') AS months_first, (jtimestamp '1403-06-30 10:00' + interval '1 month 1 day 2 hours') <> (jtimestamp '1403-06-30 10:00' + interval '1 day' + interval '1 month' + interval '2 hours') AS not_days_first; months_days_time | months_first | not_days_first ---------------------+--------------+---------------- 1403-08-01 12:00:00 | t | t (1 row) -- the same through core: a day step of core, applied after our month step SELECT jtimestamp '1403-06-31 23:00' + interval '1 month -1 day' AS month_then_back_a_day, jtimestamp '1403-06-31 23:00' + interval '1 month 1 hour' AS month_then_hour, jtimestamp '1403-06-31 23:00' + interval '1 month 1 day 1 hour' AS crosses_midnight, jtimestamp '1403-01-01 00:00' + interval '1.5 months' AS fractional_month; month_then_back_a_day | month_then_hour | crosses_midnight | fractional_month -----------------------+---------------------+---------------------+--------------------- 1403-07-29 23:00:00 | 1403-08-01 00:00:00 | 1403-08-02 00:00:00 | 1403-02-16 00:00:00 (1 row) -- subtraction is the negated interval: the month step goes back first SELECT jtimestamp '1403-08-01 12:00' - interval '1 month 1 day 2 hours' AS minus_all, jtimestamp '1403-08-01 12:00' + interval '-1 month -1 day -2 hours' AS plus_negated, jtimestamp '1403-12-30 00:00' - interval '1 year' AS minus_year, jtimestamp '1404-12-29 00:00' - interval '1 year' AS minus_year_leap; minus_all | plus_negated | minus_year | minus_year_leap ---------------------+---------------------+---------------------+--------------------- 1403-06-31 10:00:00 | 1403-06-31 10:00:00 | 1402-12-29 00:00:00 | 1403-12-29 00:00:00 (1 row) -- not invertible, as in core SELECT jtimestamp '1403-06-31 08:00' + interval '1 month' AS plus_month, jtimestamp '1403-06-31 08:00' + interval '1 month' - interval '1 month' AS and_back, jtimestamp '1403-06-31 08:00' + interval '1 month' - interval '1 month' = jtimestamp '1403-06-31 08:00' AS inverted; plus_month | and_back | inverted ---------------------+---------------------+---------- 1403-07-30 08:00:00 | 1403-06-30 08:00:00 | f (1 row) -- 1 year is 12 months: 1403 and 1408 are leap, 1404 to 1407 are not SELECT s AS start, (s::jtimestamp + interval '1 year')::text AS plus_year, (s::jtimestamp + interval '12 months')::text AS plus_12_months, s::jtimestamp + interval '1 year' = s::jtimestamp + interval '12 months' AS same, (s::jtimestamp + interval '1 year 2 months')::text AS plus_1y2m, s::jtimestamp + interval '1 year 2 months' = s::jtimestamp + interval '14 months' AS same_14 FROM (VALUES ('1403-12-30 00:00'), ('1399-12-30 06:00'), ('1403-01-31 00:00'), ('1404-12-29 23:59:59.999999')) v(s); start | plus_year | plus_12_months | same | plus_1y2m | same_14 ----------------------------+----------------------------+----------------------------+------+----------------------------+--------- 1403-12-30 00:00 | 1404-12-29 00:00:00 | 1404-12-29 00:00:00 | t | 1405-02-30 00:00:00 | t 1399-12-30 06:00 | 1400-12-29 06:00:00 | 1400-12-29 06:00:00 | t | 1401-02-30 06:00:00 | t 1403-01-31 00:00 | 1404-01-31 00:00:00 | 1404-01-31 00:00:00 | t | 1404-03-31 00:00:00 | t 1404-12-29 23:59:59.999999 | 1405-12-29 23:59:59.999999 | 1405-12-29 23:59:59.999999 | t | 1406-02-29 23:59:59.999999 | t (4 rows) SELECT jtimestamp '1403-12-30 00:00' + interval '5 years' AS leap_to_leap, jtimestamp '1399-12-30 00:00' + interval '4 years' AS leap_to_leap_4, jtimestamp '1403-12-30 00:00' + interval '1 year' AS leap_to_common; leap_to_leap | leap_to_leap_4 | leap_to_common ---------------------+---------------------+--------------------- 1408-12-30 00:00:00 | 1403-12-30 00:00:00 | 1404-12-29 00:00:00 (1 row) -- --------------------------------------------------------------------- -- the model: the month rule against an independent SQL implementation, for the -- last and the first day of every month of every year of the range, with -- eight month counts -- --------------------------------------------------------------------- -- text of a Jalali date, month length and floor division, written with the jdate -- parser only (the leap test is: does 12-30 parse) CREATE FUNCTION pg_temp.ymd(y int, m int, d int) RETURNS text LANGUAGE sql IMMUTABLE AS $$ SELECT CASE WHEN y < 0 THEN '-' ELSE '' END || lpad(abs(y)::text, 4, '0') || '-' || lpad(m::text, 2, '0') || '-' || lpad(d::text, 2, '0') $$; CREATE FUNCTION pg_temp.mlen(y int, m int) RETURNS int LANGUAGE sql IMMUTABLE AS $$ SELECT CASE WHEN m <= 6 THEN 31 WHEN m <= 11 THEN 30 ELSE 29 + pg_input_is_valid(pg_temp.ymd(y, 12, 30), 'jdate')::int END $$; CREATE TEMP TABLE jta_model AS WITH starts AS ( SELECT y, m, d FROM generate_series(-61, 3177) y, generate_series(1, 12) m, LATERAL (VALUES (1), (pg_temp.mlen(y, m))) dd(d) ), steps AS ( SELECT s.*, n, (s.y * 12 + (s.m - 1) + n) AS t FROM starts s, unnest(ARRAY[1, -1, 5, -7, 12, -12, 25, -61]) n ), target AS ( SELECT y, m, d, n, floor(t / 12.0)::int AS ny, (t - floor(t / 12.0)::int * 12 + 1) AS nm FROM steps ) SELECT y, m, d, n, ny, nm, pg_temp.mlen(ny, nm) AS nlen FROM target WHERE ny BETWEEN -61 AND 3177; SELECT count(*) AS cases, count(*) FILTER (WHERE ((pg_temp.ymd(y, m, d) || ' 13:45:30.123456')::jtimestamp + make_interval(months => n))::text = pg_temp.ymd(ny, nm, least(d, nlen)) || ' 13:45:30.123456') AS agree FROM jta_model; cases | agree --------+-------- 621640 | 621640 (1 row) -- negative control: a model without the clamp to the last day of the target -- month must disagree somewhere, so the comparison above can fail SELECT count(*) FILTER (WHERE ((pg_temp.ymd(y, m, d) || ' 13:45:30.123456')::jtimestamp + make_interval(months => n))::text <> pg_temp.ymd(ny, nm, d) || ' 13:45:30.123456') > 0 AS unclamped_model_disagrees FROM jta_model; unclamped_model_disagrees --------------------------- t (1 row) -- --------------------------------------------------------------------- -- the range edges -- --------------------------------------------------------------------- CREATE TEMP TABLE jta_edges (ord serial, label text, stmt text); INSERT INTO jta_edges (label, stmt) VALUES ('last instant, built by adding time', $$SELECT (jtimestamp '3177-12-29 00:00:00' + interval '23:59:59.999999')::text$$), ('last instant + 1 microsecond', $$SELECT (jtimestamp '3177-12-29 23:59:59.999999' + interval '1 microsecond')::text$$), ('last day + 1 day', $$SELECT (jtimestamp '3177-12-29 12:00' + interval '1 day')::text$$), ('last day + 1 month', $$SELECT (jtimestamp '3177-12-29 12:00' + interval '1 month')::text$$), ('last day + 1 year', $$SELECT (jtimestamp '3177-12-29 12:00' + interval '1 year')::text$$), ('last day, the month step leaves the range, the days would bring it back', $$SELECT (jtimestamp '3177-12-29 12:00' + interval '1 month -40 days')::text$$), ('3177-10-30 + 2 months clamps to the last day', $$SELECT (jtimestamp '3177-10-30 12:00' + interval '2 months')::text$$), ('3177-11-29 + 1 month', $$SELECT (jtimestamp '3177-11-29 12:00' + interval '1 month')::text$$), ('last day - 12 months', $$SELECT (jtimestamp '3177-12-29 12:00' - interval '12 months')::text$$), ('first instant - 1 microsecond', $$SELECT (jtimestamp '-0061-01-01 00:00:00' - interval '1 microsecond')::text$$), ('first day - 1 day', $$SELECT (jtimestamp '-0061-01-01 00:00:00' - interval '1 day')::text$$), ('second day - 1 day', $$SELECT (jtimestamp '-0061-01-02 00:00:00' - interval '1 day')::text$$), ('first day + 1 month', $$SELECT (jtimestamp '-0061-01-01 00:00:00' + interval '1 month')::text$$), ('first day - 1 month', $$SELECT (jtimestamp '-0061-01-01 00:00:00' - interval '1 month')::text$$), ('first day + 1 microsecond - 1 month', $$SELECT (jtimestamp '-0061-01-01 00:00:00.000001' - interval '1 month')::text$$), ('-0061-03-31 - 2 months', $$SELECT (jtimestamp '-0061-03-31 12:00' - interval '2 months')::text$$), ('minus 1 year at -0060', $$SELECT (jtimestamp '-0060-06-15 12:00' - interval '1 year')::text$$), ('minus 1 year at -0061', $$SELECT (jtimestamp '-0061-06-15 12:00' - interval '1 year')::text$$), ('an interval of -2147483648 months', $$SELECT (jtimestamp '1403-05-15 10:00' + interval '-2147483648 months')::text$$), ('an interval of 2147483647 months', $$SELECT (jtimestamp '1403-05-15 10:00' + interval '2147483647 months')::text$$), ('minus an interval of -2147483648 months (cannot be negated)', $$SELECT (jtimestamp '1403-05-15 10:00' - interval '-2147483648 months')::text$$), ('minus an interval of 2147483647 months', $$SELECT (jtimestamp '1403-05-15 10:00' - interval '2147483647 months')::text$$), ('an interval of -2147483648 days', $$SELECT (jtimestamp '1403-05-15 10:00' + interval '-2147483648 days')::text$$), ('an interval of 2147483647 days', $$SELECT (jtimestamp '1403-05-15 10:00' + interval '2147483647 days')::text$$), ('minus an interval of -2147483648 days', $$SELECT (jtimestamp '1403-05-15 10:00' - interval '-2147483648 days')::text$$), ('an interval of 2147483647 months 2147483647 days', $$SELECT (jtimestamp '1403-05-15 10:00' + interval '2147483647 months 2147483647 days')::text$$), ('interval + jtimestamp at the last instant', $$SELECT (interval '1 microsecond' + jtimestamp '3177-12-29 23:59:59.999999')::text$$), ('interval + jtimestamp, month, from the first day', $$SELECT (interval '1 month' + jtimestamp '-0061-01-31 00:00:00')::text$$); SELECT label, pg_temp.val(stmt) AS result FROM jta_edges ORDER BY ord; label | result -------------------------------------------------------------------------+---------------------------- last instant, built by adding time | 3177-12-29 23:59:59.999999 last instant + 1 microsecond | ERR 22008 last day + 1 day | ERR 22008 last day + 1 month | ERR 22008 last day + 1 year | ERR 22008 last day, the month step leaves the range, the days would bring it back | ERR 22008 3177-10-30 + 2 months clamps to the last day | 3177-12-29 12:00:00 3177-11-29 + 1 month | 3177-12-29 12:00:00 last day - 12 months | 3176-12-29 12:00:00 first instant - 1 microsecond | ERR 22008 first day - 1 day | ERR 22008 second day - 1 day | -0061-01-01 00:00:00 first day + 1 month | -0061-02-01 00:00:00 first day - 1 month | ERR 22008 first day + 1 microsecond - 1 month | ERR 22008 -0061-03-31 - 2 months | -0061-01-31 12:00:00 minus 1 year at -0060 | -0061-06-15 12:00:00 minus 1 year at -0061 | ERR 22008 an interval of -2147483648 months | ERR 22008 an interval of 2147483647 months | ERR 22008 minus an interval of -2147483648 months (cannot be negated) | ERR 22008 minus an interval of 2147483647 months | ERR 22008 an interval of -2147483648 days | ERR 22008 an interval of 2147483647 days | ERR 22008 minus an interval of -2147483648 days | ERR 22008 an interval of 2147483647 months 2147483647 days | ERR 22008 interval + jtimestamp at the last instant | ERR 22008 interval + jtimestamp, month, from the first day | -0061-02-31 00:00:00 (28 rows) -- --------------------------------------------------------------------- -- infinity -- --------------------------------------------------------------------- SELECT 'infinity'::jtimestamp + interval '1 month' AS inf_plus, '-infinity'::jtimestamp - interval '1 year' AS neg_inf_minus, interval '3 days' + 'infinity'::jtimestamp AS flipped, 'infinity'::jtimestamp - interval '-5 days 3 hours' AS inf_minus_negative, '-infinity'::jtimestamp + interval '1 month 1 day 1 hour' AS all_parts; inf_plus | neg_inf_minus | flipped | inf_minus_negative | all_parts ----------+---------------+----------+--------------------+----------- infinity | -infinity | infinity | infinity | -infinity (1 row) -- against core, on the same infinity and finite values and on intervals without -- months (infinite ones included: they exist from PostgreSQL 17, and where they -- do not, core and we both refuse the literal): the same value or the same -- SQLSTATE, for + and -, in both orders of + (core's errors, such as opposite -- infinities, are core's own) WITH t(j, g) AS (VALUES ('infinity', 'infinity'), ('-infinity', '-infinity'), ('1403-05-15 10:00', '2024-08-05 10:00'), ('0001-01-01 00:00:00.5', '0622-03-22 00:00:00.5'), ('3000-12-29 23:00', '3622-03-19 23:00')), i(iv) AS (VALUES ('infinity'), ('-infinity'), ('0'), ('1 day'), ('-1 day'), ('36 hours'), ('-2 days 3 hours'), ('1 day 2 hours 3 minutes 4.5 seconds'), ('-0.000001 seconds')), r AS ( SELECT t.j, t.g, i.iv, pg_temp.val(format($q$SELECT (%L::jtimestamp + %L::interval)::timestamp::text$q$, t.j, i.iv)) AS j_plus, pg_temp.val(format($q$SELECT (%L::timestamp + %L::interval)::text$q$, t.g, i.iv)) AS g_plus, pg_temp.val(format($q$SELECT (%L::interval + %L::jtimestamp)::timestamp::text$q$, i.iv, t.j)) AS j_flip, pg_temp.val(format($q$SELECT (%L::jtimestamp - %L::interval)::timestamp::text$q$, t.j, i.iv)) AS j_minus, pg_temp.val(format($q$SELECT (%L::timestamp - %L::interval)::text$q$, t.g, i.iv)) AS g_minus FROM t, i) SELECT count(*) AS cases, count(*) FILTER (WHERE j_plus = g_plus) AS plus_agrees, count(*) FILTER (WHERE j_flip = g_plus) AS flipped_agrees, count(*) FILTER (WHERE j_minus = g_minus) AS minus_agrees, count(*) FILTER (WHERE g_plus LIKE 'ERR%') > 0 AS some_errors_compared FROM r; cases | plus_agrees | flipped_agrees | minus_agrees | some_errors_compared -------+-------------+----------------+--------------+---------------------- 45 | 45 | 45 | 45 | t (1 row) -- opposite infinities are core's error (22008) on PostgreSQL 17 and later; the -- literal itself is refused before that. Same-sign infinities are infinity. SELECT pg_temp.val($$SELECT ('-infinity'::jtimestamp + interval 'infinity')::text$$) IN ('ERR 22008', 'ERR 22007') AS opposite_plus, pg_temp.val($$SELECT ('infinity'::jtimestamp - interval 'infinity')::text$$) IN ('ERR 22008', 'ERR 22007') AS opposite_minus, pg_temp.val($$SELECT ('infinity'::jtimestamp + interval 'infinity')::text$$) IN ('infinity', 'ERR 22007') AS same_sign, pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' + interval 'infinity')::text$$) IN ('infinity', 'ERR 22007') AS finite_plus_infinite; opposite_plus | opposite_minus | same_sign | finite_plus_infinite ---------------+----------------+-----------+---------------------- t | t | t | t (1 row) -- --------------------------------------------------------------------- -- day and time parts are core's: the same as core's timestamp + interval on the -- Gregorian instants, over the whole range -- --------------------------------------------------------------------- CREATE TEMP TABLE jta_days AS SELECT t, i FROM generate_series(timestamp '0560-03-20', timestamp '3799-03-19 23:59:59', interval '37 days 5 hours 11 minutes') t, (VALUES (interval '1 day'), (interval '-1 day'), (interval '45 days 3 hours'), (interval '-400 days'), (interval '90 minutes'), (interval '-36 hours 5 seconds'), (interval '0')) v(i); SELECT count(*) AS plus_cases, count(*) FILTER (WHERE (t::jtimestamp + i)::timestamp = t + i) AS plus_agrees, count(*) FILTER (WHERE (i + t::jtimestamp)::timestamp = t + i) AS flipped_agrees FROM jta_days WHERE t + i BETWEEN timestamp '0560-03-20' AND timestamp '3799-03-19 23:59:59.999999'; plus_cases | plus_agrees | flipped_agrees ------------+-------------+---------------- 222502 | 222502 | 222502 (1 row) SELECT count(*) AS minus_cases, count(*) FILTER (WHERE (t::jtimestamp - i)::timestamp = t - i) AS minus_agrees FROM jta_days WHERE t - i BETWEEN timestamp '0560-03-20' AND timestamp '3799-03-19 23:59:59.999999'; minus_cases | minus_agrees -------------+-------------- 222502 | 222502 (1 row) -- negative control: comparing with the result of a different step must fail SELECT count(*) FILTER (WHERE (t::jtimestamp + i)::timestamp <> t - i) > 0 AS wrong_step_disagrees FROM jta_days WHERE i <> interval '0' AND t + i BETWEEN timestamp '0560-03-20' AND timestamp '3799-03-19 23:59:59.999999' AND t - i BETWEEN timestamp '0560-03-20' AND timestamp '3799-03-19 23:59:59.999999'; wrong_step_disagrees ---------------------- t (1 row) -- the result of a day step or a time step of a jtimestamp is the same under any -- session time zone (a jtimestamp has none: no DST gap or overlap can change it) CREATE TEMP TABLE jta_zones (zone text, r text); SET timezone = 'Asia/Tehran'; INSERT INTO jta_zones SELECT 'Asia/Tehran', ((jtimestamp '1403-01-01 00:30:00' + interval '1 hour')::text || ' ' || (jtimestamp '1402-12-29 23:30:00' + interval '1 day 1 hour')::text || ' ' || (jtimestamp '1403-06-31 02:30:00' + interval '1 month')::text); SET timezone = 'Europe/Paris'; INSERT INTO jta_zones SELECT 'Europe/Paris', ((jtimestamp '1403-01-01 00:30:00' + interval '1 hour')::text || ' ' || (jtimestamp '1402-12-29 23:30:00' + interval '1 day 1 hour')::text || ' ' || (jtimestamp '1403-06-31 02:30:00' + interval '1 month')::text); SET timezone = 'America/Sao_Paulo'; INSERT INTO jta_zones SELECT 'America/Sao_Paulo', ((jtimestamp '1403-01-01 00:30:00' + interval '1 hour')::text || ' ' || (jtimestamp '1402-12-29 23:30:00' + interval '1 day 1 hour')::text || ' ' || (jtimestamp '1403-06-31 02:30:00' + interval '1 month')::text); SET timezone = 'UTC'; SELECT count(DISTINCT r) AS distinct_results, min(r) AS result, count(*) AS zones FROM jta_zones; distinct_results | result | zones ------------------+-------------------------------------------------------------+------- 1 | 1403-01-01 01:30:00 1403-01-02 00:30:00 1403-07-30 02:30:00 | 3 (1 row) -- --------------------------------------------------------------------- -- jtimestamp - jtimestamp: days plus time (core's timestamp_mi) -- --------------------------------------------------------------------- SELECT jtimestamp '1403-05-25 12:00' - jtimestamp '1403-05-15 00:00' AS ten_days_noon, jtimestamp '1403-05-15 00:00' - jtimestamp '1403-05-25 12:00' AS negative, jtimestamp '1403-07-01 00:00' - jtimestamp '1403-06-31 00:00' AS across_month_end, jtimestamp '1404-01-01 00:00' - jtimestamp '1403-12-30 00:00' AS across_leap_year_end, jtimestamp '1404-01-01 00:00' - jtimestamp '1403-01-01 00:00' AS leap_year_366_days, jtimestamp '1403-05-15 10:00:00.000001' - jtimestamp '1403-05-15 10:00' AS one_microsecond, jtimestamp '3177-12-29 23:59:59.999999' - jtimestamp '-0061-01-01 00:00' AS whole_range; ten_days_noon | negative | across_month_end | across_leap_year_end | leap_year_366_days | one_microsecond | whole_range ------------------+--------------------+------------------+----------------------+--------------------+-----------------+------------------------------ 10 days 12:00:00 | -10 days -12:00:00 | 1 day | 1 day | 366 days | 00:00:00.000001 | 1183019 days 23:59:59.999999 (1 row) -- every pair of a spread of instants (and the two edges) gives core's interval CREATE TEMP TABLE jta_pts AS SELECT g AS t FROM generate_series(timestamp '0560-03-20', timestamp '3799-03-19 23:59:59.999999', interval '29 years 41 days 7 hours 13 minutes 5.123456 seconds') g UNION ALL SELECT timestamp '0560-03-20 00:00:00' UNION ALL SELECT timestamp '3799-03-19 23:59:59.999999' UNION ALL SELECT timestamp '2024-03-20 00:00:00' UNION ALL SELECT timestamp '2024-03-19 23:59:59.999999'; SELECT count(*) AS pairs, count(*) FILTER (WHERE (a.t::jtimestamp - b.t::jtimestamp)::text = (a.t - b.t)::text) AS agree, count(*) FILTER (WHERE (a.t::jtimestamp - b.t::jtimestamp)::text <> (b.t - a.t)::text) > 0 AS swapped_disagrees FROM jta_pts a, jta_pts b; pairs | agree | swapped_disagrees -------+-------+------------------- 13456 | 13456 | t (1 row) -- infinity: the same value or SQLSTATE as core (an infinite interval on 17 and -- later, an error before) WITH t(j, g) AS (VALUES ('infinity', 'infinity'), ('-infinity', '-infinity'), ('1403-05-15 10:00', '2024-08-05 10:00')) SELECT count(*) AS cases, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::jtimestamp - %L::jtimestamp)::text$q$, a.j, b.j)) = pg_temp.val(format($q$SELECT (%L::timestamp - %L::timestamp)::text$q$, a.g, b.g))) AS agree FROM t a, t b; cases | agree -------+------- 9 | 9 (1 row) -- --------------------------------------------------------------------- -- jdate - jtimestamp and jtimestamp - jdate: the day is its midnight -- --------------------------------------------------------------------- SELECT jdate '1403-05-25' - jtimestamp '1403-05-15 12:00' AS nine_days_half, jtimestamp '1403-05-15 12:00' - jdate '1403-05-25' AS negated, jdate '1403-05-15' - jtimestamp '1403-05-15 00:00' AS zero, jtimestamp '1403-05-15 00:00' - jdate '1403-05-15' AS zero_too, jdate '1404-01-01' - jtimestamp '1403-12-30 18:00' AS across_leap_year_end; nine_days_half | negated | zero | zero_too | across_leap_year_end -----------------+-------------------+----------+----------+---------------------- 9 days 12:00:00 | -9 days -12:00:00 | 00:00:00 | 00:00:00 | 06:00:00 (1 row) CREATE TEMP TABLE jta_dates AS SELECT date '0560-03-20' + g AS d FROM generate_series(0, 1183019, 9973) g UNION ALL SELECT date '3799-03-19'; SELECT count(*) AS pairs, count(*) FILTER (WHERE (a.d::jdate - b.t::jtimestamp)::text = (a.d - b.t)::text) AS date_minus_ts_agree, count(*) FILTER (WHERE (b.t::jtimestamp - a.d::jdate)::text = (b.t - a.d)::text) AS ts_minus_date_agree FROM jta_dates a, jta_pts b; pairs | date_minus_ts_agree | ts_minus_date_agree -------+---------------------+--------------------- 13920 | 13920 | 13920 (1 row) -- infinity: the same value or SQLSTATE as core's date - timestamp WITH d(j, g) AS (VALUES ('infinity', 'infinity'), ('-infinity', '-infinity'), ('1403-05-15', '2024-08-05')), t(j, g) AS (VALUES ('infinity', 'infinity'), ('-infinity', '-infinity'), ('1403-05-15 10:00', '2024-08-05 10:00')) SELECT count(*) AS cases, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::jdate - %L::jtimestamp)::text$q$, d.j, t.j)) = pg_temp.val(format($q$SELECT (%L::date - %L::timestamp)::text$q$, d.g, t.g))) AS date_minus_ts_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::jtimestamp - %L::jdate)::text$q$, t.j, d.j)) = pg_temp.val(format($q$SELECT (%L::timestamp - %L::date)::text$q$, t.g, d.g))) AS ts_minus_date_agree FROM d, t; cases | date_minus_ts_agree | ts_minus_date_agree -------+---------------------+--------------------- 9 | 9 | 9 (1 row) -- --------------------------------------------------------------------- -- jdate and interval, time and timetz: the operators core has for date, and no -- others. In the catalog, with core's counterpart of each (the same name, date -- for jdate, and the timestamp types in the result for the jtimestamp types) -- --------------------------------------------------------------------- -- all IMMUTABLE (core's date + timetz is too: the zone is in the timetz), STRICT -- and PARALLEL SAFE; every + has its commutator, - has none SELECT o.oprname AS operator, o.oprleft::regtype AS left_type, o.oprright::regtype AS right_type, o.oprresult::regtype AS result_type, o.oprcode::text AS function, p.provolatile AS volatility, p.proisstrict AS strict, p.proparallel AS parallel, o.oprcom <> 0 AS has_commutator FROM pg_operator o JOIN pg_proc p ON p.oid = o.oprcode WHERE o.oprname IN ('+', '-') AND ((o.oprleft = 'jdate'::regtype AND o.oprright IN ('interval'::regtype, 'time'::regtype, 'timetz'::regtype)) OR (o.oprright = 'jdate'::regtype AND o.oprleft IN ('interval'::regtype, 'time'::regtype, 'timetz'::regtype))) ORDER BY o.oprname COLLATE "C", o.oprleft::regtype::text COLLATE "C", o.oprright::regtype::text COLLATE "C"; operator | left_type | right_type | result_type | function | volatility | strict | parallel | has_commutator ----------+------------------------+------------------------+--------------+-------------------+------------+--------+----------+---------------- + | interval | jdate | jtimestamp | interval_pl_jdate | i | t | s | t + | jdate | interval | jtimestamp | jdate_pl_interval | i | t | s | t + | jdate | time with time zone | jtimestamptz | jdate_pl_timetz | i | t | s | t + | jdate | time without time zone | jtimestamp | jdate_pl_time | i | t | s | t + | time with time zone | jdate | jtimestamptz | timetz_pl_jdate | i | t | s | t + | time without time zone | jdate | jtimestamp | time_pl_jdate | i | t | s | t - | jdate | interval | jtimestamp | jdate_mi_interval | i | t | s | f (7 rows) -- every one of them has a core counterpart with the same volatility, the mirrored -- result type and a commutator exactly where core has one; and core has no such -- operator that we lack (7 each way) SELECT count(*) AS operators, count(c.oid) AS with_core_counterpart, count(*) FILTER (WHERE c.oprresult = CASE o.oprresult WHEN 'jtimestamp'::regtype THEN 'timestamp'::regtype WHEN 'jtimestamptz'::regtype THEN 'timestamptz'::regtype END) AS same_result, count(*) FILTER (WHERE pc.provolatile = p.provolatile) AS same_volatility, count(*) FILTER (WHERE (c.oprcom <> 0) = (o.oprcom <> 0)) AS same_commutator, (SELECT count(*) FROM pg_operator k WHERE k.oprnamespace = 'pg_catalog'::regnamespace AND k.oprname IN ('+', '-') AND ((k.oprleft = 'date'::regtype AND k.oprright IN ('interval'::regtype, 'time'::regtype, 'timetz'::regtype)) OR (k.oprright = 'date'::regtype AND k.oprleft IN ('interval'::regtype, 'time'::regtype, 'timetz'::regtype)))) AS core_operators FROM pg_operator o JOIN pg_proc p ON p.oid = o.oprcode LEFT JOIN pg_operator c ON c.oprnamespace = 'pg_catalog'::regnamespace AND c.oprname = o.oprname AND c.oprleft = CASE WHEN o.oprleft = 'jdate'::regtype THEN 'date'::regtype ELSE o.oprleft END AND c.oprright = CASE WHEN o.oprright = 'jdate'::regtype THEN 'date'::regtype ELSE o.oprright END LEFT JOIN pg_proc pc ON pc.oid = c.oprcode WHERE o.oprname IN ('+', '-') AND ((o.oprleft = 'jdate'::regtype AND o.oprright IN ('interval'::regtype, 'time'::regtype, 'timetz'::regtype)) OR (o.oprright = 'jdate'::regtype AND o.oprleft IN ('interval'::regtype, 'time'::regtype, 'timetz'::regtype))); operators | with_core_counterpart | same_result | same_volatility | same_commutator | core_operators -----------+-----------------------+-------------+-----------------+-----------------+---------------- 7 | 7 | 7 | 7 | 7 | 7 (1 row) SELECT pg_typeof(jdate '1403-05-15' + interval '1 day') AS d_plus_interval, pg_typeof(interval '1 day' + jdate '1403-05-15') AS interval_plus_d, pg_typeof(jdate '1403-05-15' - interval '1 day') AS d_minus_interval, pg_typeof(jdate '1403-05-15' + time '10:00') AS d_plus_time, pg_typeof(time '10:00' + jdate '1403-05-15') AS time_plus_d, pg_typeof(jdate '1403-05-15' + timetz '10:00+01') AS d_plus_timetz, pg_typeof(timetz '10:00+01' + jdate '1403-05-15') AS timetz_plus_d; d_plus_interval | interval_plus_d | d_minus_interval | d_plus_time | time_plus_d | d_plus_timetz | timetz_plus_d -----------------+-----------------+------------------+-------------+-------------+---------------+--------------- jtimestamp | jtimestamp | jtimestamp | jtimestamp | jtimestamp | jtimestamptz | jtimestamptz (1 row) -- --------------------------------------------------------------------- -- jdate + interval: the day is its midnight, then the jtimestamp rules. Every -- form (+, the flipped +, - of the negated interval, and the jtimestamp of the -- day plus the interval) gives the expected value -- --------------------------------------------------------------------- CREATE TEMP TABLE jta_dplus (ord serial, d text, step text, expected text); INSERT INTO jta_dplus (d, step, expected) VALUES ('1403-06-31', '1 month', '1403-07-30 00:00:00'), ('1403-12-30', '1 year', '1404-12-29 00:00:00'), ('1403-12-30', '1 year 1 day', '1405-01-01 00:00:00'), ('1403-05-15', '1 day', '1403-05-16 00:00:00'), ('1403-05-15', '36 hours', '1403-05-16 12:00:00'), ('1403-05-15', '-1 day', '1403-05-14 00:00:00'), ('1403-05-15', '1 month 1 day 2 hours', '1403-06-16 02:00:00'), ('1403-05-15', '0', '1403-05-15 00:00:00'), ('1403-05-15', '-3 hours', '1403-05-14 21:00:00'), ('0000-01-31', '-1 month', '-0001-12-29 00:00:00'), ('1403-01-30', '11 months', '1403-12-30 00:00:00'), ('1403-07-30', '-1 month', '1403-06-30 00:00:00'), ('1403-07-30', '1 microsecond', '1403-07-30 00:00:00.000001'), -- the range edges ('3177-12-29', '23:59:59.999999', '3177-12-29 23:59:59.999999'), ('3177-12-29', '24 hours', 'ERR 22008'), ('3177-12-29', '1 day', 'ERR 22008'), ('3177-12-29', '1 month', 'ERR 22008'), ('3177-12-29', '1 year', 'ERR 22008'), ('3177-11-29', '1 month', '3177-12-29 00:00:00'), ('3177-12-29', '-12 months', '3176-12-29 00:00:00'), ('3177-12-29', '1 month -40 days', 'ERR 22008'), -- the month step leaves the range first ('-0061-01-01', '1 microsecond', '-0061-01-01 00:00:00.000001'), ('-0061-01-01', '-1 microsecond', 'ERR 22008'), ('-0061-01-01', '-1 day', 'ERR 22008'), ('-0061-01-01', '-1 month', 'ERR 22008'), ('-0061-01-02', '-1 day', '-0061-01-01 00:00:00'), ('-0061-02-01', '-1 month', '-0061-01-01 00:00:00'), ('1403-05-15', '-2147483648 months', 'ERR 22008'), ('1403-05-15', '2147483647 months', 'ERR 22008'), ('1403-05-15', '-2147483648 days', 'ERR 22008'), ('1403-05-15', '2147483647 days', 'ERR 22008'); SELECT ord, d, step, expected, pg_temp.val(format($q$SELECT (jdate %L + interval %L)::text$q$, d, step)) AS d_plus_interval, pg_temp.val(format($q$SELECT (jdate %L + interval %L)::text$q$, d, step)) = expected AS plus_ok, pg_temp.val(format($q$SELECT (interval %L + jdate %L)::text$q$, step, d)) = expected AS flipped_ok, pg_temp.val(format($q$SELECT (jdate %L::jtimestamp + interval %L)::text$q$, d, step)) = expected AS via_jtimestamp_ok FROM jta_dplus ORDER BY ord; ord | d | step | expected | d_plus_interval | plus_ok | flipped_ok | via_jtimestamp_ok -----+-------------+-----------------------+-----------------------------+-----------------------------+---------+------------+------------------- 1 | 1403-06-31 | 1 month | 1403-07-30 00:00:00 | 1403-07-30 00:00:00 | t | t | t 2 | 1403-12-30 | 1 year | 1404-12-29 00:00:00 | 1404-12-29 00:00:00 | t | t | t 3 | 1403-12-30 | 1 year 1 day | 1405-01-01 00:00:00 | 1405-01-01 00:00:00 | t | t | t 4 | 1403-05-15 | 1 day | 1403-05-16 00:00:00 | 1403-05-16 00:00:00 | t | t | t 5 | 1403-05-15 | 36 hours | 1403-05-16 12:00:00 | 1403-05-16 12:00:00 | t | t | t 6 | 1403-05-15 | -1 day | 1403-05-14 00:00:00 | 1403-05-14 00:00:00 | t | t | t 7 | 1403-05-15 | 1 month 1 day 2 hours | 1403-06-16 02:00:00 | 1403-06-16 02:00:00 | t | t | t 8 | 1403-05-15 | 0 | 1403-05-15 00:00:00 | 1403-05-15 00:00:00 | t | t | t 9 | 1403-05-15 | -3 hours | 1403-05-14 21:00:00 | 1403-05-14 21:00:00 | t | t | t 10 | 0000-01-31 | -1 month | -0001-12-29 00:00:00 | -0001-12-29 00:00:00 | t | t | t 11 | 1403-01-30 | 11 months | 1403-12-30 00:00:00 | 1403-12-30 00:00:00 | t | t | t 12 | 1403-07-30 | -1 month | 1403-06-30 00:00:00 | 1403-06-30 00:00:00 | t | t | t 13 | 1403-07-30 | 1 microsecond | 1403-07-30 00:00:00.000001 | 1403-07-30 00:00:00.000001 | t | t | t 14 | 3177-12-29 | 23:59:59.999999 | 3177-12-29 23:59:59.999999 | 3177-12-29 23:59:59.999999 | t | t | t 15 | 3177-12-29 | 24 hours | ERR 22008 | ERR 22008 | t | t | t 16 | 3177-12-29 | 1 day | ERR 22008 | ERR 22008 | t | t | t 17 | 3177-12-29 | 1 month | ERR 22008 | ERR 22008 | t | t | t 18 | 3177-12-29 | 1 year | ERR 22008 | ERR 22008 | t | t | t 19 | 3177-11-29 | 1 month | 3177-12-29 00:00:00 | 3177-12-29 00:00:00 | t | t | t 20 | 3177-12-29 | -12 months | 3176-12-29 00:00:00 | 3176-12-29 00:00:00 | t | t | t 21 | 3177-12-29 | 1 month -40 days | ERR 22008 | ERR 22008 | t | t | t 22 | -0061-01-01 | 1 microsecond | -0061-01-01 00:00:00.000001 | -0061-01-01 00:00:00.000001 | t | t | t 23 | -0061-01-01 | -1 microsecond | ERR 22008 | ERR 22008 | t | t | t 24 | -0061-01-01 | -1 day | ERR 22008 | ERR 22008 | t | t | t 25 | -0061-01-01 | -1 month | ERR 22008 | ERR 22008 | t | t | t 26 | -0061-01-02 | -1 day | -0061-01-01 00:00:00 | -0061-01-01 00:00:00 | t | t | t 27 | -0061-02-01 | -1 month | -0061-01-01 00:00:00 | -0061-01-01 00:00:00 | t | t | t 28 | 1403-05-15 | -2147483648 months | ERR 22008 | ERR 22008 | t | t | t 29 | 1403-05-15 | 2147483647 months | ERR 22008 | ERR 22008 | t | t | t 30 | 1403-05-15 | -2147483648 days | ERR 22008 | ERR 22008 | t | t | t 31 | 1403-05-15 | 2147483647 days | ERR 22008 | ERR 22008 | t | t | t (31 rows) -- jdate - interval, with the same table read backwards: d - (-step) is d + step -- (an interval of -2147483648 months or days cannot be negated: 22008, so the -- two extreme rows are skipped here) SELECT count(*) AS rows, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (jdate %L - (-interval %L))::text$q$, d, step)) = expected) AS minus_negated_ok, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (jdate %L - interval %L)::text$q$, d, step)) = pg_temp.val(format($q$SELECT (jdate %L::jtimestamp - interval %L)::text$q$, d, step))) AS minus_same_as_jtimestamp FROM jta_dplus WHERE step NOT IN ('-2147483648 months', '-2147483648 days'); rows | minus_negated_ok | minus_same_as_jtimestamp ------+------------------+-------------------------- 29 | 29 | 29 (1 row) CREATE TEMP TABLE jta_dminus (ord serial, d text, step text, expected text); INSERT INTO jta_dminus (d, step, expected) VALUES ('1403-07-30', '1 month', '1403-06-30 00:00:00'), ('1403-05-15', '1 day', '1403-05-14 00:00:00'), ('1403-06-31', '-1 month', '1403-07-30 00:00:00'), ('1403-05-15', '2 days 3 hours', '1403-05-12 21:00:00'), ('1403-01-01', '1 microsecond', '1402-12-29 23:59:59.999999'), ('1404-01-01', '1 day', '1403-12-30 00:00:00'), ('1403-05-15', '-2147483648 months', 'ERR 22008'), ('1403-05-15', '-2147483648 days', 'ERR 22008'), ('1403-05-15', '2147483647 months', 'ERR 22008'); SELECT ord, d, step, expected, pg_temp.val(format($q$SELECT (jdate %L - interval %L)::text$q$, d, step)) AS d_minus_interval, pg_temp.val(format($q$SELECT (jdate %L - interval %L)::text$q$, d, step)) = expected AS ok FROM jta_dminus ORDER BY ord; ord | d | step | expected | d_minus_interval | ok -----+------------+--------------------+----------------------------+----------------------------+---- 1 | 1403-07-30 | 1 month | 1403-06-30 00:00:00 | 1403-06-30 00:00:00 | t 2 | 1403-05-15 | 1 day | 1403-05-14 00:00:00 | 1403-05-14 00:00:00 | t 3 | 1403-06-31 | -1 month | 1403-07-30 00:00:00 | 1403-07-30 00:00:00 | t 4 | 1403-05-15 | 2 days 3 hours | 1403-05-12 21:00:00 | 1403-05-12 21:00:00 | t 5 | 1403-01-01 | 1 microsecond | 1402-12-29 23:59:59.999999 | 1402-12-29 23:59:59.999999 | t 6 | 1404-01-01 | 1 day | 1403-12-30 00:00:00 | 1403-12-30 00:00:00 | t 7 | 1403-05-15 | -2147483648 months | ERR 22008 | ERR 22008 | t 8 | 1403-05-15 | -2147483648 days | ERR 22008 | ERR 22008 | t 9 | 1403-05-15 | 2147483647 months | ERR 22008 | ERR 22008 | t (9 rows) -- the day and time parts are core's date + interval: for steps without a month -- the result equals core's on the Gregorian day of the same instants (the days of -- the whole range, 120 of them, and the two ends), and with a month it equals -- the jtimestamp of the day plus the interval; errors on both sides are the same -- SQLSTATE (the core result is converted to jtimestamp, which range-checks it) CREATE TEMP TABLE jta_dsteps (step text, months boolean); INSERT INTO jta_dsteps VALUES ('1 day', false), ('-1 day', false), ('36 hours', false), ('2 days 3 hours', false), ('1 microsecond', false), ('-1 microsecond', false), ('40 days -2 hours', false), ('1 month', true), ('-1 month', true), ('1 year', true), ('1 month 1 day', true), ('-13 months 3 days 2 hours', true); SELECT count(*) AS cases, count(*) FILTER (WHERE s.months OR pg_temp.val(format($q$SELECT (%L::date::jdate + interval %L)::text$q$, a.d, s.step)) = pg_temp.val(format($q$SELECT ((%L::date + interval %L)::jtimestamp)::text$q$, a.d, s.step))) AS plus_agree_with_core, count(*) FILTER (WHERE s.months OR pg_temp.val(format($q$SELECT (%L::date::jdate - interval %L)::text$q$, a.d, s.step)) = pg_temp.val(format($q$SELECT ((%L::date - interval %L)::jtimestamp)::text$q$, a.d, s.step))) AS minus_agree_with_core, count(*) FILTER (WHERE NOT s.months OR pg_temp.val(format($q$SELECT (%L::date::jdate + interval %L)::text$q$, a.d, s.step)) = pg_temp.val(format($q$SELECT (%L::date::jdate::jtimestamp + interval %L)::text$q$, a.d, s.step))) AS plus_agree_with_jtimestamp, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + interval %L)::text$q$, a.d, s.step)) LIKE 'ERR%') > 0 AS some_errors, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + interval %L)::text$q$, a.d, s.step)) NOT LIKE 'ERR%') > 0 AS some_values FROM jta_dates a, jta_dsteps s; cases | plus_agree_with_core | minus_agree_with_core | plus_agree_with_jtimestamp | some_errors | some_values -------+----------------------+-----------------------+----------------------------+-------------+------------- 1440 | 1440 | 1440 | 1440 | t | t (1 row) -- negative control: against core's result for a step one microsecond longer, no -- value agrees (only the range errors, which are the same for both) SELECT count(*) AS cases, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + interval %L)::text$q$, a.d, s.step)) NOT LIKE 'ERR%' AND pg_temp.val(format($q$SELECT (%L::date::jdate + interval %L)::text$q$, a.d, s.step)) = pg_temp.val(format($q$SELECT ((%L::date + interval %L + interval '1 microsecond')::jtimestamp)::text$q$, a.d, s.step))) AS agree_with_a_longer_step FROM jta_dates a, jta_dsteps s WHERE NOT s.months; cases | agree_with_a_longer_step -------+-------------------------- 840 | 0 (1 row) -- infinity: the same value or SQLSTATE as core's date + interval (its result -- converted to jtimestamp, to be in the same calendar), for finite and infinite -- days and intervals (an infinite interval exists from PostgreSQL 17) WITH d(j, g) AS (VALUES ('infinity', 'infinity'), ('-infinity', '-infinity'), ('1403-05-15', '2024-08-05')), i(step) AS (VALUES ('1 day'), ('-5 days 3 hours'), ('infinity'), ('-infinity'), ('0')) SELECT count(*) AS cases, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::jdate + interval %L)::text$q$, d.j, i.step)) = pg_temp.val(format($q$SELECT ((%L::date + interval %L)::jtimestamp)::text$q$, d.g, i.step))) AS plus_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (interval %L + %L::jdate)::text$q$, i.step, d.j)) = pg_temp.val(format($q$SELECT ((interval %L + %L::date)::jtimestamp)::text$q$, i.step, d.g))) AS flipped_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::jdate - interval %L)::text$q$, d.j, i.step)) = pg_temp.val(format($q$SELECT ((%L::date - interval %L)::jtimestamp)::text$q$, d.g, i.step))) AS minus_agree FROM d, i; cases | plus_agree | flipped_agree | minus_agree -------+------------+---------------+------------- 15 | 15 | 15 | 15 (1 row) SELECT 'infinity'::jdate + interval '1 month' AS inf_plus, '-infinity'::jdate - interval '1 year' AS neg_inf_minus, interval '3 days' + 'infinity'::jdate AS flipped; inf_plus | neg_inf_minus | flipped ----------+---------------+---------- infinity | -infinity | infinity (1 row) -- --------------------------------------------------------------------- -- jdate + time, time + jdate (a jtimestamp) and jdate + timetz, timetz + jdate (a -- jtimestamptz: the instant of that wall time in the timetz's own offset; shown -- here in the session zone UTC) -- --------------------------------------------------------------------- CREATE TEMP TABLE jta_dtime (ord serial, d text, t text, expected text); INSERT INTO jta_dtime (d, t, expected) VALUES ('1403-05-15', '10:30', '1403-05-15 10:30:00'), ('1403-05-15', '00:00', '1403-05-15 00:00:00'), ('1403-05-15', '23:59:59.999999', '1403-05-15 23:59:59.999999'), ('1403-05-15', '24:00', '1403-05-16 00:00:00'), ('1403-12-29', '24:00', '1403-12-30 00:00:00'), -- 1403 is leap ('1404-12-29', '24:00', '1405-01-01 00:00:00'), ('3177-12-29', '23:59:59.999999', '3177-12-29 23:59:59.999999'), ('3177-12-29', '24:00', 'ERR 22008'), ('-0061-01-01', '00:00', '-0061-01-01 00:00:00'), ('infinity', '10:30', 'infinity'), ('-infinity', '10:30', '-infinity'); SELECT ord, d, t, expected, pg_temp.val(format($q$SELECT (jdate %L + time %L)::text$q$, d, t)) AS d_plus_time, pg_temp.val(format($q$SELECT (jdate %L + time %L)::text$q$, d, t)) = expected AS plus_ok, pg_temp.val(format($q$SELECT (time %L + jdate %L)::text$q$, t, d)) = expected AS flipped_ok FROM jta_dtime ORDER BY ord; ord | d | t | expected | d_plus_time | plus_ok | flipped_ok -----+-------------+-----------------+----------------------------+----------------------------+---------+------------ 1 | 1403-05-15 | 10:30 | 1403-05-15 10:30:00 | 1403-05-15 10:30:00 | t | t 2 | 1403-05-15 | 00:00 | 1403-05-15 00:00:00 | 1403-05-15 00:00:00 | t | t 3 | 1403-05-15 | 23:59:59.999999 | 1403-05-15 23:59:59.999999 | 1403-05-15 23:59:59.999999 | t | t 4 | 1403-05-15 | 24:00 | 1403-05-16 00:00:00 | 1403-05-16 00:00:00 | t | t 5 | 1403-12-29 | 24:00 | 1403-12-30 00:00:00 | 1403-12-30 00:00:00 | t | t 6 | 1404-12-29 | 24:00 | 1405-01-01 00:00:00 | 1405-01-01 00:00:00 | t | t 7 | 3177-12-29 | 23:59:59.999999 | 3177-12-29 23:59:59.999999 | 3177-12-29 23:59:59.999999 | t | t 8 | 3177-12-29 | 24:00 | ERR 22008 | ERR 22008 | t | t 9 | -0061-01-01 | 00:00 | -0061-01-01 00:00:00 | -0061-01-01 00:00:00 | t | t 10 | infinity | 10:30 | infinity | infinity | t | t 11 | -infinity | 10:30 | -infinity | -infinity | t | t (11 rows) CREATE TEMP TABLE jta_dtimetz (ord serial, d text, t text, expected text); INSERT INTO jta_dtimetz (d, t, expected) VALUES ('1403-05-15', '10:30+03:30', '1403-05-15 07:00:00+00'), ('1403-05-15', '00:00+00', '1403-05-15 00:00:00+00'), ('1403-05-15', '23:59:59.999999-15:59', '1403-05-16 15:58:59.999999+00'), ('1403-05-15', '00:00+15:59', '1403-05-14 08:01:00+00'), ('1403-05-15', '24:00+00', '1403-05-16 00:00:00+00'), ('1403-05-15', '12:00-03:30', '1403-05-15 15:30:00+00'), -- the safe range of jtimestamptz: the first valid instant is -0061-01-01 16:00 UTC -- and the last 3177-12-29 07:59:59.999999 UTC ('-0061-01-01', '16:00+00', '-0061-01-01 16:00:00+00'), ('-0061-01-01', '15:59:59.999999+00', 'ERR 22008'), ('-0061-01-01', '00:01-15:59', '-0061-01-01 16:00:00+00'), ('-0061-01-01', '00:00:59.999999-15:59', 'ERR 22008'), ('3177-12-29', '07:59:59.999999+00', '3177-12-29 07:59:59.999999+00'), ('3177-12-29', '08:00+00', 'ERR 22008'), ('3177-12-29', '23:58:59.999999+15:59', '3177-12-29 07:59:59.999999+00'), ('3177-12-29', '23:59+15:59', 'ERR 22008'), ('infinity', '10:30+01', 'infinity'), ('-infinity', '10:30+01', '-infinity'); SELECT ord, d, t, expected, pg_temp.val(format($q$SELECT (jdate %L + timetz %L)::text$q$, d, t)) AS d_plus_timetz, pg_temp.val(format($q$SELECT (jdate %L + timetz %L)::text$q$, d, t)) = expected AS plus_ok, pg_temp.val(format($q$SELECT (timetz %L + jdate %L)::text$q$, t, d)) = expected AS flipped_ok FROM jta_dtimetz ORDER BY ord; ord | d | t | expected | d_plus_timetz | plus_ok | flipped_ok -----+-------------+-----------------------+-------------------------------+-------------------------------+---------+------------ 1 | 1403-05-15 | 10:30+03:30 | 1403-05-15 07:00:00+00 | 1403-05-15 07:00:00+00 | t | t 2 | 1403-05-15 | 00:00+00 | 1403-05-15 00:00:00+00 | 1403-05-15 00:00:00+00 | t | t 3 | 1403-05-15 | 23:59:59.999999-15:59 | 1403-05-16 15:58:59.999999+00 | 1403-05-16 15:58:59.999999+00 | t | t 4 | 1403-05-15 | 00:00+15:59 | 1403-05-14 08:01:00+00 | 1403-05-14 08:01:00+00 | t | t 5 | 1403-05-15 | 24:00+00 | 1403-05-16 00:00:00+00 | 1403-05-16 00:00:00+00 | t | t 6 | 1403-05-15 | 12:00-03:30 | 1403-05-15 15:30:00+00 | 1403-05-15 15:30:00+00 | t | t 7 | -0061-01-01 | 16:00+00 | -0061-01-01 16:00:00+00 | -0061-01-01 16:00:00+00 | t | t 8 | -0061-01-01 | 15:59:59.999999+00 | ERR 22008 | ERR 22008 | t | t 9 | -0061-01-01 | 00:01-15:59 | -0061-01-01 16:00:00+00 | -0061-01-01 16:00:00+00 | t | t 10 | -0061-01-01 | 00:00:59.999999-15:59 | ERR 22008 | ERR 22008 | t | t 11 | 3177-12-29 | 07:59:59.999999+00 | 3177-12-29 07:59:59.999999+00 | 3177-12-29 07:59:59.999999+00 | t | t 12 | 3177-12-29 | 08:00+00 | ERR 22008 | ERR 22008 | t | t 13 | 3177-12-29 | 23:58:59.999999+15:59 | 3177-12-29 07:59:59.999999+00 | 3177-12-29 07:59:59.999999+00 | t | t 14 | 3177-12-29 | 23:59+15:59 | ERR 22008 | ERR 22008 | t | t 15 | infinity | 10:30+01 | infinity | infinity | t | t 16 | -infinity | 10:30+01 | -infinity | -infinity | t | t (16 rows) -- against core's date + time and date + timetz on the Gregorian day, for the 120 -- days of the range and a spread of times (the core result goes through the cast -- to the jtimestamp type, which range-checks it): the same value or the same -- SQLSTATE, in UTC and in a zone with an offset (the timetz carries its own -- offset, so only the display of the instant depends on the session zone) CREATE TEMP TABLE jta_times (t time, z timetz); INSERT INTO jta_times VALUES ('00:00', '00:00+00'), ('10:30:15.5', '10:30+03:30'), ('23:59:59.999999', '23:59:59.999999-15:59'), ('12:00', '12:00+15:59'), ('24:00', '24:00+00'), ('00:00:00.000001', '00:00:00.000001+00'), ('06:45', '06:45-08'); SELECT count(*) AS cases, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + %L::time)::text$q$, a.d, x.t)) = pg_temp.val(format($q$SELECT ((%L::date + %L::time)::jtimestamp)::text$q$, a.d, x.t))) AS time_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::time + %L::date::jdate)::text$q$, x.t, a.d)) = pg_temp.val(format($q$SELECT ((%L::time + %L::date)::jtimestamp)::text$q$, x.t, a.d))) AS time_flipped_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + %L::timetz)::text$q$, a.d, x.z)) = pg_temp.val(format($q$SELECT ((%L::date + %L::timetz)::jtimestamptz)::text$q$, a.d, x.z))) AS timetz_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::timetz + %L::date::jdate)::text$q$, x.z, a.d)) = pg_temp.val(format($q$SELECT ((%L::timetz + %L::date)::jtimestamptz)::text$q$, x.z, a.d))) AS timetz_flipped_agree, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + %L::timetz)::text$q$, a.d, x.z)) LIKE 'ERR%') > 0 AS some_timetz_errors, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + %L::timetz)::text$q$, a.d, x.z)) NOT LIKE 'ERR%') > 0 AS some_timetz_values FROM jta_dates a, jta_times x; cases | time_agree | time_flipped_agree | timetz_agree | timetz_flipped_agree | some_timetz_errors | some_timetz_values -------+------------+--------------------+--------------+----------------------+--------------------+-------------------- 840 | 840 | 840 | 840 | 840 | t | t (1 row) SET timezone = 'Asia/Tehran'; SELECT count(*) AS cases, count(*) FILTER (WHERE pg_temp.val(format($q$SELECT (%L::date::jdate + %L::timetz)::text$q$, a.d, x.z)) = pg_temp.val(format($q$SELECT ((%L::date + %L::timetz)::jtimestamptz)::text$q$, a.d, x.z))) AS timetz_agree_in_tehran FROM jta_dates a, jta_times x; cases | timetz_agree_in_tehran -------+------------------------ 840 | 840 (1 row) SET timezone = 'UTC'; -- a jtimestamp from jdate + time or + interval does not depend on the session -- zone, a jtimestamptz from jdate + timetz is the same instant in every zone SELECT (SELECT jdate '1403-05-15' + time '10:30') AS date_time_in_utc, (SELECT jdate '1403-05-15' + interval '1 month 2 hours') AS date_interval_in_utc, (SELECT (jdate '1403-05-15' + timetz '10:30+03:30')::timestamptz AT TIME ZONE 'UTC') AS date_timetz_in_utc; date_time_in_utc | date_interval_in_utc | date_timetz_in_utc ---------------------+----------------------+--------------------- 1403-05-15 10:30:00 | 1403-06-15 02:00:00 | 2024-08-05 07:00:00 (1 row) SET timezone = 'America/Sao_Paulo'; SELECT (SELECT jdate '1403-05-15' + time '10:30') AS date_time_in_sao_paulo, (SELECT jdate '1403-05-15' + interval '1 month 2 hours') AS date_interval_in_sao_paulo, (SELECT (jdate '1403-05-15' + timetz '10:30+03:30')::timestamptz AT TIME ZONE 'UTC') AS date_timetz_in_sao_paulo; date_time_in_sao_paulo | date_interval_in_sao_paulo | date_timetz_in_sao_paulo ------------------------+----------------------------+-------------------------- 1403-05-15 10:30:00 | 1403-06-15 02:00:00 | 2024-08-05 07:00:00 (1 row) SET timezone = 'UTC'; -- IMMUTABLE: usable in a generated column and an index expression (jdate + timetz -- too: core's date + timetz is IMMUTABLE) CREATE TABLE jta_dix ( id int, d jdate, t time, z timetz, next_month jtimestamp GENERATED ALWAYS AS (d + interval '1 month') STORED, at_time jtimestamp GENERATED ALWAYS AS (d + t) STORED, at_timetz jtimestamptz GENERATED ALWAYS AS (d + z) STORED ); CREATE INDEX jta_dix_prev_day ON jta_dix ((d - interval '1 day')); INSERT INTO jta_dix (id, d, t, z) VALUES (1, '1403-06-31', '10:00', '10:00+03:30'), (2, '1403-12-30', '24:00', '00:00-01'); SELECT id, next_month, at_time, at_timetz FROM jta_dix ORDER BY id; id | next_month | at_time | at_timetz ----+---------------------+---------------------+------------------------ 1 | 1403-07-30 00:00:00 | 1403-06-31 10:00:00 | 1403-06-31 06:30:00+00 2 | 1404-01-30 00:00:00 | 1404-01-01 00:00:00 | 1403-12-30 01:00:00+00 (2 rows) DROP TABLE jta_dix; -- --------------------------------------------------------------------- -- what resolves without an operator of its own, as in core: a time is converted -- to an interval by core's implicit cast, so jtimestamp + time, jtimestamptz + -- time and jdate - time use the interval operators (core's timestamp + time and -- date - time do the same); timetz has no such cast: no jtimestamp + timetz -- --------------------------------------------------------------------- SELECT pg_typeof(jtimestamp '1403-05-15 10:00' + time '10:30') AS ts_plus_time, jtimestamp '1403-05-15 10:00' + time '10:30' AS ts_plus_time_value, jtimestamp '1403-05-15 10:00' - time '10:30' AS ts_minus_time_value, pg_typeof(jdate '1403-05-15' - time '10:30') AS date_minus_time, jdate '1403-05-15' - time '10:30' AS date_minus_time_value, jdate '1403-05-15' - time '10:30' = jdate '1403-05-15' - interval '10:30' AS same_as_the_interval; ts_plus_time | ts_plus_time_value | ts_minus_time_value | date_minus_time | date_minus_time_value | same_as_the_interval --------------+---------------------+---------------------+-----------------+-----------------------+---------------------- jtimestamp | 1403-05-15 20:30:00 | 1403-05-14 23:30:00 | jtimestamp | 1403-05-14 13:30:00 | t (1 row) SELECT pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' + timetz '10:30+01')::text$$) AS ts_plus_timetz, pg_temp.val($$SELECT (timestamp '2024-08-05 10:00' + timetz '10:30+01')::text$$) AS core_ts_plus_timetz, pg_temp.val($$SELECT (jtimestamptz '1403-05-15 10:00+00' + timetz '10:30+01')::text$$) AS tz_plus_timetz, pg_temp.val($$SELECT (timestamptz '2024-08-05 10:00+00' + timetz '10:30+01')::text$$) AS core_tz_plus_timetz, pg_temp.val($$SELECT (time '10:30' - jdate '1403-05-15')::text$$) AS time_minus_date, pg_temp.val($$SELECT (jdate '1403-05-15' + jdate '1403-05-16')::text$$) AS date_plus_date, pg_temp.val($$SELECT (jdate '1403-05-15' - timetz '10:30+01')::text$$) AS date_minus_timetz, pg_temp.val($$SELECT (date '2024-08-05' - timetz '10:30+01')::text$$) AS core_date_minus_timetz; ts_plus_timetz | core_ts_plus_timetz | tz_plus_timetz | core_tz_plus_timetz | time_minus_date | date_plus_date | date_minus_timetz | core_date_minus_timetz ----------------+---------------------+----------------+---------------------+-----------------+----------------+-------------------+------------------------ ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 (1 row) -- a bare literal next to a jdate with + is ambiguous now (42725), as core's -- date + '5' is: write jdate + 5 or jdate + interval '5 days'. With minus the -- literal is read as a jdate (the exact match jdate - jdate), 22007, as in core SELECT pg_temp.val($$SELECT (jdate '1403-05-15' + '5')::text$$) AS date_plus_bare, pg_temp.val($$SELECT (date '2024-08-05' + '5')::text$$) AS core_date_plus_bare, pg_temp.val($$SELECT ('5' + jdate '1403-05-15')::text$$) AS bare_plus_date, pg_temp.val($$SELECT (jdate '1403-05-15' + '1 day')::text$$) AS date_plus_bare_interval, pg_temp.val($$SELECT (jdate '1403-05-15' - '5')::text$$) AS date_minus_bare, pg_temp.val($$SELECT (date '2024-08-05' - '5')::text$$) AS core_date_minus_bare, (jdate '1403-05-15' + 5)::text AS date_plus_int, (jdate '1403-05-15' + interval '5 days')::text AS date_plus_interval, (jdate '1403-05-15' - 5)::text AS date_minus_int; date_plus_bare | core_date_plus_bare | bare_plus_date | date_plus_bare_interval | date_minus_bare | core_date_minus_bare | date_plus_int | date_plus_interval | date_minus_int ----------------+---------------------+----------------+-------------------------+-----------------+----------------------+---------------+---------------------+---------------- ERR 42725 | ERR 42725 | ERR 42725 | ERR 42725 | ERR 22007 | ERR 22007 | 1403-05-20 | 1403-05-20 00:00:00 | 1403-05-10 (1 row) -- --------------------------------------------------------------------- -- what is not there: no operator against the core types, and nothing between -- two points in time other than the differences above -- --------------------------------------------------------------------- SELECT pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' - timestamp '2024-08-05 10:00')::text$$) AS ts_minus_core, pg_temp.val($$SELECT (timestamp '2024-08-05 10:00' - jtimestamp '1403-05-15 10:00')::text$$) AS core_minus_ts, pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' + jtimestamp '1403-05-15 10:00')::text$$) AS ts_plus_ts, pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' + jdate '1403-05-15')::text$$) AS ts_plus_date, pg_temp.val($$SELECT (jdate '1403-05-15' + jtimestamp '1403-05-15 10:00')::text$$) AS date_plus_ts, pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' + 1)::text$$) AS ts_plus_int, pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' - 1)::text$$) AS ts_minus_int, pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' - timestamptz '2024-08-05 10:00')::text$$) AS ts_minus_tz; ts_minus_core | core_minus_ts | ts_plus_ts | ts_plus_date | date_plus_ts | ts_plus_int | ts_minus_int | ts_minus_tz ---------------+---------------+------------+--------------+--------------+-------------+--------------+------------- ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 | ERR 42883 (1 row) -- a bare literal as the interval resolves: the only operator with a jtimestamp -- on the left and an unknown on the right is jtimestamp + interval SELECT jtimestamp '1403-05-15 10:00' + '1 day' AS bare_literal_plus, '1 month' + jtimestamp '1403-06-31 10:00' AS bare_literal_flipped; bare_literal_plus | bare_literal_flipped ---------------------+---------------------- 1403-05-16 10:00:00 | 1403-07-30 10:00:00 (1 row) -- with minus a bare literal on the right is read as the type of the left -- operand (jtimestamp - jtimestamp is an exact match), as core reads it for -- timestamp: the literal is not a jtimestamp, 22007 in both SELECT pg_temp.val($$SELECT (jtimestamp '1403-05-15 10:00' - '1 day')::text$$) AS bare_literal_minus, pg_temp.val($$SELECT (timestamp '2024-08-05 10:00' - '1 day')::text$$) AS core_bare_literal_minus; bare_literal_minus | core_bare_literal_minus --------------------+------------------------- ERR 22007 | ERR 22007 (1 row) -- --------------------------------------------------------------------- -- IMMUTABLE: usable in an index expression and a generated column -- --------------------------------------------------------------------- CREATE TABLE jta_idx ( id int, ts jtimestamp, next_month jtimestamp GENERATED ALWAYS AS (ts + interval '1 month') STORED ); CREATE INDEX jta_idx_prev_day ON jta_idx ((ts - interval '1 day')); INSERT INTO jta_idx (id, ts) VALUES (1, '1403-06-31 10:00'), (2, '1403-12-30 00:00'), (3, '1404-11-30 23:59:59.999999'); SELECT id, ts, next_month FROM jta_idx ORDER BY id; id | ts | next_month ----+----------------------------+---------------------------- 1 | 1403-06-31 10:00:00 | 1403-07-30 10:00:00 2 | 1403-12-30 00:00:00 | 1404-01-30 00:00:00 3 | 1404-11-30 23:59:59.999999 | 1404-12-29 23:59:59.999999 (3 rows) SELECT count(*) AS found FROM jta_idx WHERE ts - interval '1 day' = jtimestamp '1403-06-30 10:00'; found ------- 1 (1 row) DROP TABLE jta_idx; RESET DateStyle; RESET IntervalStyle; RESET timezone;