-- jdate_part SELECT jdate_part('year', '1403-05-15'::jdate) AS f_year; f_year -------- 1403 (1 row) SELECT jdate_part('month', '1403-05-15'::jdate) AS f_month; f_month --------- 5 (1 row) SELECT jdate_part('day', '1403-05-15'::jdate) AS f_day; f_day ------- 15 (1 row) SELECT jdate_part('quarter', '1403-05-15'::jdate) AS f_quarter; f_quarter ----------- 2 (1 row) SELECT jdate_part('quarter', '1403-11-01'::jdate) AS f_quarter_q4; f_quarter_q4 -------------- 4 (1 row) SELECT jdate_part('doy', '1403-01-01'::jdate) AS f_doy_new_year; f_doy_new_year ---------------- 1 (1 row) SELECT jdate_part('doy', '1403-07-01'::jdate) AS f_doy_second_half; f_doy_second_half ------------------- 187 (1 row) SELECT jdate_part('decade', '1403-05-15'::jdate) AS f_decade; f_decade ---------- 140 (1 row) SELECT jdate_part('century', '1403-05-15'::jdate) AS f_century; f_century ----------- 15 (1 row) SELECT jdate_part('hour', '1403-05-15'::jdate) AS f_hour_zero; f_hour_zero ------------- 0 (1 row) SELECT jdate_part('minute', '1403-05-15'::jdate) AS f_minute_zero; f_minute_zero --------------- 0 (1 row) SELECT jdate_part('second', '1403-05-15'::jdate) AS f_second_zero; f_second_zero --------------- 0 (1 row) -- dow is Saturday-first (Saturday 0 .. Friday 6): core's dow (Sunday 0) plus -- one, modulo 7. isodow is the plain ISO number and must equal core's. Both -- are calendar-agnostic, so they follow the identical underlying day. SELECT jdate_part('dow', '1403-05-15'::jdate) = (date_part('dow', '1403-05-15'::jdate::date)::int + 1) % 7 AS dow_saturday_first; dow_saturday_first -------------------- t (1 row) SELECT jdate_part('isodow', '1403-05-15'::jdate) = date_part('isodow', '1403-05-15'::jdate::date) AS isodow_matches_gregorian; isodow_matches_gregorian -------------------------- t (1 row) -- epoch is calendar-agnostic too: must match the Gregorian answer for -- the identical underlying day SELECT jdate_part('epoch', '1403-05-15'::jdate) = date_part('epoch', '1403-05-15'::jdate::date) AS epoch_matches_gregorian; epoch_matches_gregorian ------------------------- t (1 row) SELECT EXTRACT(epoch FROM '1403-01-01'::jdate) AS extract_epoch; extract_epoch --------------- 1710892800 (1 row) -- unsupported unit SELECT jdate_part('week', '1403-05-15'::jdate); ERROR: unit not supported for jdate_part/extract -- EXTRACT() overload, registered directly in pg_catalog SELECT EXTRACT(year FROM '1403-05-15'::jdate) AS extract_year; extract_year -------------- 1403 (1 row) SELECT EXTRACT(month FROM '1403-05-15'::jdate) AS extract_month; extract_month --------------- 5 (1 row) SELECT pg_typeof(EXTRACT(year FROM '1403-05-15'::jdate)) AS extract_returns_numeric; extract_returns_numeric ------------------------- numeric (1 row) -- return types: jdate_part is bigint for every unit, EXTRACT is numeric for -- every unit, finite or infinite SELECT f, pg_typeof(jdate_part(f, '1403-05-15'::jdate)) AS part_type, pg_typeof(EXTRACT(epoch FROM '1403-05-15'::jdate)) AS extract_type FROM (VALUES ('year'), ('month'), ('day'), ('quarter'), ('doy'), ('dow'), ('isodow'), ('decade'), ('century'), ('epoch'), ('hour')) AS u(f) ORDER BY f; f | part_type | extract_type ---------+-----------+-------------- century | bigint | numeric day | bigint | numeric decade | bigint | numeric dow | bigint | numeric doy | bigint | numeric epoch | bigint | numeric hour | bigint | numeric isodow | bigint | numeric month | bigint | numeric quarter | bigint | numeric year | bigint | numeric (11 rows) SELECT pg_typeof(jdate_part('epoch', 'infinity'::jdate)) AS part_inf_type, pg_typeof(EXTRACT(epoch FROM 'infinity'::jdate)) AS extract_inf_type; part_inf_type | extract_inf_type ---------------+------------------ bigint | numeric (1 row) SELECT proname, prorettype::regtype AS rettype, provolatile, proparallel FROM pg_proc WHERE proname IN ('jdate_part', 'extract') AND proargtypes[1] = 'jdate'::regtype ORDER BY proname; proname | rettype | provolatile | proparallel ------------+---------+-------------+------------- extract | numeric | i | s jdate_part | bigint | i | s (2 rows) -- epoch is an exact int64 (no float rounding), through both functions, at both -- edges of the range and on negative epochs SELECT d, jdate_part('epoch', d) AS part_epoch, EXTRACT(epoch FROM d) AS extract_epoch, jdate_part('epoch', d) = date_part('epoch', d::date) AS same_as_core, jdate_part('epoch', d) % 86400 AS not_whole_days FROM (VALUES ('-0061-01-01'::jdate), ('0001-01-01'), ('1348-10-11'), ('1403-01-01'), ('3177-12-29')) AS v(d) ORDER BY d; d | part_epoch | extract_epoch | same_as_core | not_whole_days -------------+--------------+---------------+--------------+---------------- -0061-01-01 | -44488483200 | -44488483200 | t | 0 0001-01-01 | -42531868800 | -42531868800 | t | 0 1348-10-11 | 0 | 0 | t | 0 1403-01-01 | 1710892800 | 1710892800 | t | 0 3177-12-29 | 57724358400 | 57724358400 | t | 0 (5 rows) -- confirm EXTRACT for pre-existing types is completely unaffected SELECT EXTRACT(year FROM date '2024-01-01') AS extract_year_date_unaffected; extract_year_date_unaffected ------------------------------ 2024 (1 row) SELECT EXTRACT(year FROM timestamp '2024-01-01 00:00:00') AS extract_year_timestamp_unaffected; extract_year_timestamp_unaffected ----------------------------------- 2024 (1 row) -- jdate_trunc SELECT jdate_trunc('day', '1403-05-15'::jdate) AS trunc_day; trunc_day ------------ 1403-05-15 (1 row) SELECT jdate_trunc('month', '1403-05-15'::jdate) AS trunc_month; trunc_month ------------- 1403-05-01 (1 row) SELECT jdate_trunc('quarter', '1403-05-15'::jdate) AS trunc_quarter; trunc_quarter --------------- 1403-04-01 (1 row) SELECT jdate_trunc('quarter', '1403-11-20'::jdate) AS trunc_quarter_q4; trunc_quarter_q4 ------------------ 1403-10-01 (1 row) SELECT jdate_trunc('year', '1403-05-15'::jdate) AS trunc_year; trunc_year ------------ 1403-01-01 (1 row) SELECT jdate_trunc('year', '-infinity'::jdate) AS trunc_year_infinite; trunc_year_infinite --------------------- -infinity (1 row) SELECT jdate_trunc('hour', '1403-05-15'::jdate); -- unsupported unit ERROR: unit not supported for jdate_trunc -- week starts on Saturday, consistent with the Saturday-first dow: the week -- start is the day minus its dow. Every day of one full week (Saturday .. -- Friday) truncates to that Saturday, the next Saturday starts the next week; -- the first week crosses a month boundary (Mordad 31 -> Shahrivar 1, 1403) SELECT d AS jdate, to_char(d::date, 'Dy') AS weekday, jdate_part('dow', d) AS dow, jdate_trunc('week', d) AS week_start FROM (SELECT '1403-05-27'::jdate + n AS d FROM generate_series(0, 7) AS n) AS w ORDER BY d; jdate | weekday | dow | week_start ------------+---------+-----+------------ 1403-05-27 | Sat | 0 | 1403-05-27 1403-05-28 | Sun | 1 | 1403-05-27 1403-05-29 | Mon | 2 | 1403-05-27 1403-05-30 | Tue | 3 | 1403-05-27 1403-05-31 | Wed | 4 | 1403-05-27 1403-06-01 | Thu | 5 | 1403-05-27 1403-06-02 | Fri | 6 | 1403-05-27 1403-06-03 | Sat | 0 | 1403-06-03 (8 rows) -- a week across the year boundary (1402 has 365 days) and one across month -- and year in the negative years (year -1 to year 0) SELECT d AS jdate, to_char(d::date, 'Dy') AS weekday, jdate_trunc('week', d) AS week_start FROM (SELECT '1402-12-25'::jdate + n AS d FROM generate_series(0, 7) AS n) AS w ORDER BY d; jdate | weekday | week_start ------------+---------+------------ 1402-12-25 | Fri | 1402-12-19 1402-12-26 | Sat | 1402-12-26 1402-12-27 | Sun | 1402-12-26 1402-12-28 | Mon | 1402-12-26 1402-12-29 | Tue | 1402-12-26 1403-01-01 | Wed | 1402-12-26 1403-01-02 | Thu | 1402-12-26 1403-01-03 | Fri | 1402-12-26 (8 rows) SELECT d AS jdate, to_char(d::date, 'Dy') AS weekday, jdate_trunc('week', d) AS week_start FROM (SELECT '-0001-12-26'::jdate + n AS d FROM generate_series(0, 7) AS n) AS w ORDER BY d; jdate | weekday | week_start -------------+---------+------------- -0001-12-26 | Sat | -0001-12-26 -0001-12-27 | Sun | -0001-12-26 -0001-12-28 | Mon | -0001-12-26 -0001-12-29 | Tue | -0001-12-26 0000-01-01 | Wed | -0001-12-26 0000-01-02 | Thu | -0001-12-26 0000-01-03 | Fri | -0001-12-26 0000-01-04 | Sat | 0000-01-04 (8 rows) -- a week start in the previous month and the previous year SELECT jdate_trunc('week', '1403-01-01'::jdate) AS week_start_prev_year, jdate_trunc('week', '1403-06-03'::jdate) AS week_start_prev_month; week_start_prev_year | week_start_prev_month ----------------------+----------------------- 1402-12-26 | 1403-06-03 (1 row) -- unit names are case-insensitive, and infinity stays infinity SELECT jdate_trunc('WEEK', '1403-05-31'::jdate) AS upper_week, jdate_trunc('WeEk', '1403-05-31'::jdate) AS mixed_week, jdate_trunc('week', 'infinity'::jdate) AS week_inf, jdate_trunc('week', '-infinity'::jdate) AS week_ninf; upper_week | mixed_week | week_inf | week_ninf ------------+------------+----------+----------- 1403-05-27 | 1403-05-27 | infinity | -infinity (1 row) -- both range edges: the first days of the range have a week start below the -- minimum jdate (an out-of-range error, no clamping) unless they are -- Saturdays themselves; the top edge needs no check CREATE FUNCTION pg_temp.week_probe(d jdate) RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN jdate_trunc('week', d)::text; EXCEPTION WHEN datetime_field_overflow THEN RETURN 'error ' || SQLSTATE; END $$; SELECT d AS jdate, jdate_part('dow', d) AS dow, pg_temp.week_probe(d) AS week_start FROM (SELECT '-0061-01-01'::jdate + n AS d FROM generate_series(0, 7) AS n) AS w ORDER BY d; jdate | dow | week_start -------------+-----+------------- -0061-01-01 | 5 | error 22008 -0061-01-02 | 6 | error 22008 -0061-01-03 | 0 | -0061-01-03 -0061-01-04 | 1 | -0061-01-03 -0061-01-05 | 2 | -0061-01-03 -0061-01-06 | 3 | -0061-01-03 -0061-01-07 | 4 | -0061-01-03 -0061-01-08 | 5 | -0061-01-03 (8 rows) SELECT d AS jdate, jdate_part('dow', d) AS dow, pg_temp.week_probe(d) AS week_start FROM (SELECT '3177-12-29'::jdate - n AS d FROM generate_series(0, 7) AS n) AS w ORDER BY d; jdate | dow | week_start ------------+-----+------------ 3177-12-22 | 3 | 3177-12-19 3177-12-23 | 4 | 3177-12-19 3177-12-24 | 5 | 3177-12-19 3177-12-25 | 6 | 3177-12-19 3177-12-26 | 0 | 3177-12-26 3177-12-27 | 1 | 3177-12-26 3177-12-28 | 2 | 3177-12-26 3177-12-29 | 3 | 3177-12-26 (8 rows) -- decade is year / 10, as in core (decade 202 is 2020-2029), not the first -- year of the decade: the jdate years 1400..1409 are decade 140 SELECT jdate_part('decade', '1403-05-15'::jdate) AS decade_1403; decade_1403 ------------- 140 (1 row) SELECT EXTRACT(decade FROM '1403-05-15'::jdate) AS extract_decade_1403; extract_decade_1403 --------------------- 140 (1 row) SELECT pg_typeof(EXTRACT(decade FROM '1403-05-15'::jdate)) AS extract_decade_type; extract_decade_type --------------------- numeric (1 row) SELECT d AS jdate, jdate_part('decade', d::jdate) AS decade FROM (VALUES ('0000-05-15'), ('0009-05-15'), ('0010-05-15'), ('0019-05-15'), ('1399-05-15'), ('1400-05-15'), ('1409-05-15'), ('1410-05-15'), ('3177-12-29')) AS v(d); jdate | decade ------------+-------- 0000-05-15 | 0 0009-05-15 | 0 0010-05-15 | 1 0019-05-15 | 1 1399-05-15 | 139 1400-05-15 | 140 1409-05-15 | 140 1410-05-15 | 141 3177-12-29 | 317 (9 rows) -- the documented examples side by side: the decade is a number (year / 10 -- rounded down), never the first year of the decade, so there is no 1400 SELECT jdate_part('year', d::jdate) AS year, jdate_part('decade', d::jdate) AS decade FROM (VALUES ('1403-05-15'), ('1400-01-01'), ('1399-12-29'), ('0000-05-15'), ('-0001-05-15')) AS v(d); year | decade ------+-------- 1403 | 140 1400 | 140 1399 | 139 0 | 0 -1 | -1 (5 rows) -- Jalali years below 0 follow core's decade numbering, which for the same -- (astronomical, year 0 exists) year number is floor(year / 10): decade 0 is -- years 0..9 and decade -1 is years -10..-1, so every decade has ten years. -- Core agrees, e.g. EXTRACT(decade FROM date '0002-06-01 BC') = -1 for the -- astronomical year -1, and date '0062-06-01 BC' (year -61) gives -7. SELECT d AS jdate, jdate_part('decade', d::jdate) AS decade FROM (VALUES ('-0001-05-15'), ('-0010-05-15'), ('-0011-05-15'), ('-0020-05-15'), ('-0021-05-15'), ('-0060-05-15'), ('-0061-01-01')) AS v(d); jdate | decade -------------+-------- -0001-05-15 | -1 -0010-05-15 | -1 -0011-05-15 | -2 -0020-05-15 | -2 -0021-05-15 | -3 -0060-05-15 | -6 -0061-01-01 | -7 (7 rows) SELECT EXTRACT(decade FROM date '0002-06-01 BC') AS core_decade_year_m1, EXTRACT(decade FROM date '0062-06-01 BC') AS core_decade_year_m61; core_decade_year_m1 | core_decade_year_m61 ---------------------+---------------------- -1 | -7 (1 row) -- every year of the supported range: decade matches floor(year / 10), matches -- core's decade for the same year number (core has no year 0, so astronomical -- year y <= 0 is core's (y - 1) BC-shifted year), matches trunc(year / 10) -- from year 0 up, and EXTRACT agrees with jdate_part; the year column proves -- the generated dates are the intended years WITH y AS ( SELECT g, (CASE WHEN g < 0 THEN '-' ELSE '' END || lpad(abs(g)::text, 4, '0') || '-05-15')::jdate AS d, make_date(CASE WHEN g >= 1 THEN g ELSE g - 1 END, 6, 1) AS core_d FROM generate_series(-61, 3177) AS g ) SELECT count(*) AS years, count(*) FILTER (WHERE jdate_part('year', d) <> g) AS wrong_year, count(*) FILTER (WHERE jdate_part('decade', d) <> floor(jdate_part('year', d)::numeric / 10)) AS not_floor_div, count(*) FILTER (WHERE g >= 0 AND jdate_part('decade', d) <> trunc(jdate_part('year', d)::numeric / 10)) AS not_trunc_div, count(*) FILTER (WHERE jdate_part('decade', d) <> date_part('decade', core_d)) AS differs_from_core, count(*) FILTER (WHERE EXTRACT(decade FROM d) <> jdate_part('decade', d)::numeric) AS extract_differs FROM y; years | wrong_year | not_floor_div | not_trunc_div | differs_from_core | extract_differs -------+------------+---------------+---------------+-------------------+----------------- 3239 | 0 | 0 | 0 | 0 | 0 (1 row) -- dow, isodow and epoch are calendar-agnostic: isodow and epoch must equal -- core's answer for the identical day, and dow is core's shifted to a -- Saturday start ((core dow + 1) % 7), including at both ends of the -- supported range SELECT d AS jdate, jdate_part('dow', d::jdate) AS dow, jdate_part('dow', d::jdate) = (date_part('dow', d::jdate::date)::int + 1) % 7 AS dow_ok, jdate_part('isodow', d::jdate) AS isodow, jdate_part('isodow', d::jdate) = date_part('isodow', d::jdate::date) AS isodow_ok, jdate_part('epoch', d::jdate) AS epoch, jdate_part('epoch', d::jdate) = date_part('epoch', d::jdate::date) AS epoch_ok, EXTRACT(epoch FROM d::jdate) = EXTRACT(epoch FROM d::jdate::date) AS extract_epoch_ok FROM (VALUES ('-0061-01-01'), ('0000-01-01'), ('1348-10-11'), ('1403-05-15'), ('3177-12-29')) AS v(d); jdate | dow | dow_ok | isodow | isodow_ok | epoch | epoch_ok | extract_epoch_ok -------------+-----+--------+--------+-----------+--------------+----------+------------------ -0061-01-01 | 5 | t | 4 | t | -44488483200 | t | t 0000-01-01 | 4 | t | 3 | t | -42563491200 | t | t 1348-10-11 | 5 | t | 4 | t | 0 | t | t 1403-05-15 | 2 | t | 1 | t | 1722816000 | t | t 3177-12-29 | 3 | t | 2 | t | 57724358400 | t | t (5 rows) -- one full week, Saturday to Friday, side by side: dow counts from Saturday -- (0) to Friday (6), isodow from Monday (1) to Sunday (7). The first week -- crosses a month boundary (Mordad 31 -> Shahrivar 1, 1403); the second is in -- negative years and crosses the year boundary from -1 to 0. The to_char -- column is core's own weekday name for the same day, from the Gregorian date. SELECT d AS jdate, to_char(d::date, 'Dy') AS weekday, jdate_part('dow', d) AS dow, jdate_part('isodow', d) AS isodow, EXTRACT(dow FROM d) AS extract_dow, EXTRACT(isodow FROM d) AS extract_isodow, date_part('dow', d::date) AS core_dow FROM (SELECT '1403-05-27'::jdate + n AS d FROM generate_series(0, 6) AS n) AS w ORDER BY d; jdate | weekday | dow | isodow | extract_dow | extract_isodow | core_dow ------------+---------+-----+--------+-------------+----------------+---------- 1403-05-27 | Sat | 0 | 6 | 0 | 6 | 6 1403-05-28 | Sun | 1 | 7 | 1 | 7 | 0 1403-05-29 | Mon | 2 | 1 | 2 | 1 | 1 1403-05-30 | Tue | 3 | 2 | 3 | 2 | 2 1403-05-31 | Wed | 4 | 3 | 4 | 3 | 3 1403-06-01 | Thu | 5 | 4 | 5 | 4 | 4 1403-06-02 | Fri | 6 | 5 | 6 | 5 | 5 (7 rows) SELECT d AS jdate, to_char(d::date, 'Dy') AS weekday, jdate_part('dow', d) AS dow, jdate_part('isodow', d) AS isodow, EXTRACT(dow FROM d) AS extract_dow, EXTRACT(isodow FROM d) AS extract_isodow, date_part('dow', d::date) AS core_dow FROM (SELECT '-0001-12-26'::jdate + n AS d FROM generate_series(0, 6) AS n) AS w ORDER BY d; jdate | weekday | dow | isodow | extract_dow | extract_isodow | core_dow -------------+---------+-----+--------+-------------+----------------+---------- -0001-12-26 | Sat | 0 | 6 | 0 | 6 | 6 -0001-12-27 | Sun | 1 | 7 | 1 | 7 | 0 -0001-12-28 | Mon | 2 | 1 | 2 | 1 | 1 -0001-12-29 | Tue | 3 | 2 | 3 | 2 | 2 0000-01-01 | Wed | 4 | 3 | 4 | 3 | 3 0000-01-02 | Thu | 5 | 4 | 5 | 4 | 4 0000-01-03 | Fri | 6 | 5 | 6 | 5 | 5 (7 rows) -- 1403-01-01 is a Wednesday: dow 4 (Saturday-first), isodow 3 SELECT jdate_part('dow', '1403-01-01'::jdate) AS dow_new_year, jdate_part('isodow', '1403-01-01'::jdate) AS isodow_new_year, jdate_part('dow', '1403-01-01'::jdate) + 1 AS dow_one_based; dow_new_year | isodow_new_year | dow_one_based --------------+-----------------+--------------- 4 | 3 | 5 (1 row) -- the same over every day of the supported range, jdate_part and EXTRACT SELECT count(*) AS days, count(*) FILTER (WHERE jdate_part('dow', d) <> (date_part('dow', d::date)::int + 1) % 7) AS dow_differs, count(*) FILTER (WHERE jdate_part('isodow', d) <> date_part('isodow', d::date)) AS isodow_differs, count(*) FILTER (WHERE jdate_part('epoch', d) <> date_part('epoch', d::date)) AS epoch_differs, count(*) FILTER (WHERE EXTRACT(dow FROM d) <> (EXTRACT(dow FROM d::date)::int + 1) % 7) AS extract_dow_differs, count(*) FILTER (WHERE EXTRACT(epoch FROM d) <> EXTRACT(epoch FROM d::date)) AS extract_epoch_differs FROM (SELECT g::date::jdate AS d FROM generate_series('-0061-01-01'::jdate::date::timestamp, '3177-12-29'::jdate::date::timestamp, interval '1 day') AS g) AS s; days | dow_differs | isodow_differs | epoch_differs | extract_dow_differs | extract_epoch_differs ---------+-------------+----------------+---------------+---------------------+----------------------- 1183020 | 0 | 0 | 0 | 0 | 0 (1 row) -- infinity: EXTRACT keeps +/-Infinity for epoch, jdate_part (bigint) is NULL SELECT EXTRACT(epoch FROM 'infinity'::jdate) AS extract_epoch_inf, EXTRACT(epoch FROM '-infinity'::jdate) AS extract_epoch_ninf; extract_epoch_inf | extract_epoch_ninf -------------------+-------------------- Infinity | -Infinity (1 row) SELECT jdate_part('epoch', 'infinity'::jdate) AS part_epoch_inf, jdate_part('epoch', '-infinity'::jdate) AS part_epoch_ninf; part_epoch_inf | part_epoch_ninf ----------------+----------------- | (1 row) -- infinity: jdate_part is NULL for every unit (a bigint has no infinity), where -- core's date gives NULL for the units that cycle (dow, isodow, month, day, -- quarter, doy) and +/-Infinity for the ones that only grow (year, decade, -- century, epoch); EXTRACT below keeps core's answers SELECT f, v, jdate_part(f, v::jdate) AS jdate_part, date_part(f, v::date) AS core_part, jdate_part(f, v::jdate) IS NULL AS part_null FROM (VALUES ('dow'), ('isodow'), ('epoch'), ('year'), ('month'), ('day'), ('quarter'), ('doy'), ('decade'), ('century')) AS u(f), (VALUES ('infinity'), ('-infinity')) AS i(v) ORDER BY v <> 'infinity', f; f | v | jdate_part | core_part | part_null ---------+-----------+------------+-----------+----------- century | infinity | | Infinity | t day | infinity | | | t decade | infinity | | Infinity | t dow | infinity | | | t doy | infinity | | | t epoch | infinity | | Infinity | t isodow | infinity | | | t month | infinity | | | t quarter | infinity | | | t year | infinity | | Infinity | t century | -infinity | | -Infinity | t day | -infinity | | | t decade | -infinity | | -Infinity | t dow | -infinity | | | t doy | -infinity | | | t epoch | -infinity | | -Infinity | t isodow | -infinity | | | t month | -infinity | | | t quarter | -infinity | | | t year | -infinity | | -Infinity | t (20 rows) -- the same through EXTRACT (numeric) SELECT v, EXTRACT(dow FROM v::jdate) IS NOT DISTINCT FROM (EXTRACT(dow FROM v::date)::int + 1) % 7 AS dow_same, EXTRACT(isodow FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(isodow FROM v::date) AS isodow_same, EXTRACT(epoch FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(epoch FROM v::date) AS epoch_same, EXTRACT(year FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(year FROM v::date) AS year_same, EXTRACT(month FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(month FROM v::date) AS month_same, EXTRACT(day FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(day FROM v::date) AS day_same, EXTRACT(quarter FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(quarter FROM v::date) AS quarter_same, EXTRACT(doy FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(doy FROM v::date) AS doy_same, EXTRACT(decade FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(decade FROM v::date) AS decade_same, EXTRACT(century FROM v::jdate) IS NOT DISTINCT FROM EXTRACT(century FROM v::date) AS century_same FROM (VALUES ('infinity'), ('-infinity')) AS i(v) ORDER BY v <> 'infinity'; v | dow_same | isodow_same | epoch_same | year_same | month_same | day_same | quarter_same | doy_same | decade_same | century_same -----------+----------+-------------+------------+-----------+------------+----------+--------------+----------+-------------+-------------- infinity | t | t | t | t | t | t | t | t | t | t -infinity | t | t | t | t | t | t | t | t | t | t (2 rows) SELECT EXTRACT(year FROM 'infinity'::jdate) AS extract_year_inf, EXTRACT(century FROM '-infinity'::jdate) AS extract_century_ninf, EXTRACT(month FROM 'infinity'::jdate) IS NULL AS extract_month_inf_null; extract_year_inf | extract_century_ninf | extract_month_inf_null ------------------+----------------------+------------------------ Infinity | -Infinity | t (1 row) -- time-of-day units give 0 for finite dates and NULL for infinity (they do -- not grow); an unsupported unit still fails on infinity SELECT jdate_part('hour', 'infinity'::jdate) IS NULL AS hour_inf_null, jdate_part('second', '-infinity'::jdate) IS NULL AS second_ninf_null, jdate_part('hour', '1403-05-15'::jdate) AS hour_finite; hour_inf_null | second_ninf_null | hour_finite ---------------+------------------+------------- t | t | 0 (1 row) SELECT jdate_part('week', 'infinity'::jdate); ERROR: unit not supported for jdate_part/extract -- the unit is decoded once per call site and cached; feed varying units from a -- table column (same call site, rows evaluated in n order) to prove the cache -- never returns a stale field: repeats, case changes, switches back and forth CREATE TEMP TABLE unit_seq (n int, u text, j jdate); INSERT INTO unit_seq SELECT n, u, '1403-05-15'::jdate FROM unnest(ARRAY['year', 'year', 'YEAR', 'Month', 'month', 'month', 'MONTH', 'day', 'Day', 'year', 'quarter', 'doy', 'quarter', 'quarter', 'epoch', 'dow', 'isodow', 'Decade', 'century', 'year']) WITH ORDINALITY AS t(u, n); CREATE TEMP TABLE unit_ref (u text, part bigint, ext numeric); INSERT INTO unit_ref VALUES ('year', jdate_part('year'::text, '1403-05-15'::jdate), pg_catalog.extract('year'::text, '1403-05-15'::jdate)), ('month', jdate_part('month'::text, '1403-05-15'::jdate), pg_catalog.extract('month'::text, '1403-05-15'::jdate)), ('day', jdate_part('day'::text, '1403-05-15'::jdate), pg_catalog.extract('day'::text, '1403-05-15'::jdate)), ('quarter', jdate_part('quarter'::text, '1403-05-15'::jdate), pg_catalog.extract('quarter'::text, '1403-05-15'::jdate)), ('doy', jdate_part('doy'::text, '1403-05-15'::jdate), pg_catalog.extract('doy'::text, '1403-05-15'::jdate)), ('epoch', jdate_part('epoch'::text, '1403-05-15'::jdate), pg_catalog.extract('epoch'::text, '1403-05-15'::jdate)), ('dow', jdate_part('dow'::text, '1403-05-15'::jdate), pg_catalog.extract('dow'::text, '1403-05-15'::jdate)), ('isodow', jdate_part('isodow'::text, '1403-05-15'::jdate), pg_catalog.extract('isodow'::text, '1403-05-15'::jdate)), ('decade', jdate_part('decade'::text, '1403-05-15'::jdate), pg_catalog.extract('decade'::text, '1403-05-15'::jdate)), ('century', jdate_part('century'::text, '1403-05-15'::jdate), pg_catalog.extract('century'::text, '1403-05-15'::jdate)); SELECT s.n, s.u, s.part, s.part = r.part AS part_ok, s.ext, s.ext = r.ext AS extract_ok FROM (SELECT n, u, jdate_part(u, j) AS part, pg_catalog.extract(u, j) AS ext FROM (SELECT * FROM unit_seq ORDER BY n OFFSET 0) q) s JOIN unit_ref r ON r.u = lower(s.u) ORDER BY s.n; n | u | part | part_ok | ext | extract_ok ----+---------+------------+---------+------------+------------ 1 | year | 1403 | t | 1403 | t 2 | year | 1403 | t | 1403 | t 3 | YEAR | 1403 | t | 1403 | t 4 | Month | 5 | t | 5 | t 5 | month | 5 | t | 5 | t 6 | month | 5 | t | 5 | t 7 | MONTH | 5 | t | 5 | t 8 | day | 15 | t | 15 | t 9 | Day | 15 | t | 15 | t 10 | year | 1403 | t | 1403 | t 11 | quarter | 2 | t | 2 | t 12 | doy | 139 | t | 139 | t 13 | quarter | 2 | t | 2 | t 14 | quarter | 2 | t | 2 | t 15 | epoch | 1722816000 | t | 1722816000 | t 16 | dow | 2 | t | 2 | t 17 | isodow | 1 | t | 1 | t 18 | Decade | 140 | t | 140 | t 19 | century | 15 | t | 15 | t 20 | year | 1403 | t | 1403 | t (20 rows) -- jdate_trunc has its own cache SELECT n, u, jdate_trunc(u, j) AS trunc FROM (SELECT * FROM unit_seq WHERE lower(u) IN ('year', 'month', 'day', 'quarter') ORDER BY n OFFSET 0) q ORDER BY n; n | u | trunc ----+---------+------------ 1 | year | 1403-01-01 2 | year | 1403-01-01 3 | YEAR | 1403-01-01 4 | Month | 1403-05-01 5 | month | 1403-05-01 6 | month | 1403-05-01 7 | MONTH | 1403-05-01 8 | day | 1403-05-15 9 | Day | 1403-05-15 10 | year | 1403-01-01 11 | quarter | 1403-04-01 13 | quarter | 1403-04-01 14 | quarter | 1403-04-01 20 | year | 1403-01-01 (14 rows) -- an invalid unit in the middle of the stream raises its error, and the units -- after it are still decoded correctly (one call site inside the function) CREATE FUNCTION unit_probe(u text) RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN jdate_part(u, '1403-05-15'::jdate)::text || '/' || pg_catalog.extract(u, '1403-05-15'::jdate)::text; EXCEPTION WHEN invalid_parameter_value OR feature_not_supported THEN RETURN 'error ' || SQLSTATE; END $$; CREATE FUNCTION unit_probe_trunc(u text) RETURNS text LANGUAGE plpgsql AS $$ BEGIN RETURN jdate_trunc(u, '1403-05-15'::jdate)::text; EXCEPTION WHEN invalid_parameter_value OR feature_not_supported THEN RETURN 'error ' || SQLSTATE; END $$; SELECT n, u, unit_probe(u) AS probe, unit_probe_trunc(u) AS probe_trunc FROM unnest(ARRAY['month', 'month', 'bogus', 'month', 'Year', 'YEAR', 'yeer', 'year', 'week', 'day', 'hour', 'day', '', 'day', 'MONTH', 'a_unit_name_longer_than_the_cache_buffer', 'month', 'Month']) WITH ORDINALITY AS t(u, n) ORDER BY n; n | u | probe | probe_trunc ----+------------------------------------------+-------------+------------- 1 | month | 5/5 | 1403-05-01 2 | month | 5/5 | 1403-05-01 3 | bogus | error 22023 | error 22023 4 | month | 5/5 | 1403-05-01 5 | Year | 1403/1403 | 1403-01-01 6 | YEAR | 1403/1403 | 1403-01-01 7 | yeer | error 22023 | error 22023 8 | year | 1403/1403 | 1403-01-01 9 | week | error 0A000 | 1403-05-13 10 | day | 15/15 | 1403-05-15 11 | hour | 0/0 | error 0A000 12 | day | 15/15 | 1403-05-15 13 | | error 22023 | error 22023 14 | day | 15/15 | 1403-05-15 15 | MONTH | 5/5 | 1403-05-01 16 | a_unit_name_longer_than_the_cache_buffer | error 22023 | error 22023 17 | month | 5/5 | 1403-05-01 18 | Month | 5/5 | 1403-05-01 (18 rows)