-- comparisons SELECT '1402-01-01'::jdate = '1402-01-01'::jdate AS eq_true; eq_true --------- t (1 row) SELECT '1402-01-01'::jdate = '1403-01-01'::jdate AS eq_false; eq_false ---------- f (1 row) SELECT '1402-01-01'::jdate <> '1403-01-01'::jdate AS ne_true; ne_true --------- t (1 row) SELECT '1402-01-01'::jdate < '1403-01-01'::jdate AS lt_true; lt_true --------- t (1 row) SELECT '1403-01-01'::jdate < '1402-01-01'::jdate AS lt_false; lt_false ---------- f (1 row) SELECT '1402-01-01'::jdate <= '1402-01-01'::jdate AS le_true; le_true --------- t (1 row) SELECT '1403-01-01'::jdate > '1402-01-01'::jdate AS gt_true; gt_true --------- t (1 row) SELECT '1403-01-01'::jdate >= '1403-01-01'::jdate AS ge_true; ge_true --------- t (1 row) -- sorting CREATE TABLE op_dates (d jdate); INSERT INTO op_dates VALUES ('1403-05-01'), ('1401-01-01'), ('1403-01-01'), ('1402-12-29'); SELECT d FROM op_dates ORDER BY d; d ------------ 1401-01-01 1402-12-29 1403-01-01 1403-05-01 (4 rows) -- indexing: btree opclass usable for equality and range lookups CREATE INDEX op_dates_idx ON op_dates (d); SET enable_seqscan = off; EXPLAIN (COSTS off) SELECT * FROM op_dates WHERE d = '1403-01-01'::jdate; QUERY PLAN ------------------------------------------------ Index Only Scan using op_dates_idx on op_dates Index Cond: (d = '1403-01-01'::jdate) (2 rows) EXPLAIN (COSTS off) SELECT * FROM op_dates WHERE d > '1402-01-01'::jdate ORDER BY d; QUERY PLAN ------------------------------------------------ Index Only Scan using op_dates_idx on op_dates Index Cond: (d > '1402-01-01'::jdate) (2 rows) RESET enable_seqscan; -- GROUP BY / DISTINCT SELECT DISTINCT d FROM (VALUES ('1403-01-01'::jdate), ('1403-01-01'::jdate), ('1402-01-01'::jdate)) v(d) ORDER BY d; d ------------ 1402-01-01 1403-01-01 (2 rows) SELECT d, count(*) FROM op_dates GROUP BY d ORDER BY d; d | count ------------+------- 1401-01-01 | 1 1402-12-29 | 1 1403-01-01 | 1 1403-05-01 | 1 (4 rows) -- min/max aggregates SELECT min(d), max(d), pg_typeof(min(d)) FROM op_dates; min | max | pg_typeof ------------+------------+----------- 1401-01-01 | 1403-05-01 | jdate (1 row) DROP TABLE op_dates; -- --------------------------------------------------------------------- -- btree family and support functions: jdate_ops is the default class for -- jdate in the family jdatetime_ops that the jtimestamp and jtimestamptz -- classes share; it carries core's date sort support (2) and btequalimage (4) -- --------------------------------------------------------------------- SELECT f.opfname AS family, c.opcname AS class, c.opcintype::regtype AS type, c.opcdefault AS is_default FROM pg_opclass c JOIN pg_opfamily f ON f.oid = c.opcfamily WHERE c.opcname = 'jdate_ops'; family | class | type | is_default ---------------+-----------+-------+------------ jdatetime_ops | jdate_ops | jdate | t (1 row) -- the jdate entries of the family: five operators on (jdate, jdate) SELECT amopstrategy, amopopr::regoperator FROM pg_amop WHERE amopfamily = (SELECT opcfamily FROM pg_opclass WHERE opcname = 'jdate_ops') AND amoplefttype = 'jdate'::regtype AND amoprighttype = 'jdate'::regtype ORDER BY amopstrategy; amopstrategy | amopopr --------------+----------------- 1 | <(jdate,jdate) 2 | <=(jdate,jdate) 3 | =(jdate,jdate) 4 | >=(jdate,jdate) 5 | >(jdate,jdate) (5 rows) -- support functions 1, 2 and 4 of the jdate entry, exactly as before the family -- existed (function 6, skip support, is not printed because it exists only on -- PostgreSQL 18, see the next query) SELECT amprocnum, amproc::regprocedure FROM pg_amproc WHERE amprocfamily = (SELECT opcfamily FROM pg_opclass WHERE opcname = 'jdate_ops') AND amproclefttype = 'jdate'::regtype AND amprocrighttype = 'jdate'::regtype AND amprocnum <> 6 ORDER BY amprocnum; amprocnum | amproc -----------+---------------------------- 1 | jdate_cmp(jdate,jdate) 2 | date_sortsupport(internal) 4 | btequalimage(oid) (3 rows) -- skip support (6, date_skipsupport) is present exactly when the server is -- PostgreSQL 18 or later SELECT (count(*) FILTER (WHERE amprocnum = 6) = 1) = (current_setting('server_version_num')::int >= 180000) AS skipsupport_iff_pg18 FROM pg_amproc WHERE amprocfamily = (SELECT opcfamily FROM pg_opclass WHERE opcname = 'jdate_ops') AND amproclefttype = 'jdate'::regtype AND amprocrighttype = 'jdate'::regtype; skipsupport_iff_pg18 ---------------------- t (1 row) -- amvalidate accepts the class and, with it, the whole family (the cross-type -- operators between jdate, jtimestamp and jtimestamptz are in it) SELECT amvalidate(oid) AS jdate_ops_valid FROM pg_opclass WHERE opcname = 'jdate_ops'; jdate_ops_valid ----------------- t (1 row) -- ordering through the sort support path: duplicates, negative years, both -- range edges and infinity (positive and negative), 4,000 rows CREATE TABLE op_sort (id int, d jdate); INSERT INTO op_sort SELECT g, CASE WHEN g % 50 = 0 THEN 'infinity'::jdate WHEN g % 50 = 1 THEN '-infinity'::jdate WHEN g % 50 = 2 THEN '-0061-01-01'::jdate WHEN g % 50 = 3 THEN '3177-12-29'::jdate ELSE '-0061-01-01'::jdate + ((g * 7919) % 1183000)::int END FROM generate_series(1, 4000) g; -- the ORDER BY result is non-decreasing, agrees with sorting by the -- equivalent core date (infinity included) and keeps every row SELECT count(*) AS rows_sorted, count(*) FILTER (WHERE d < prev_d) AS inversions FROM (SELECT d, lag(d) OVER (ORDER BY d, id) AS prev_d FROM op_sort) s; rows_sorted | inversions -------------+------------ 4000 | 0 (1 row) SELECT (SELECT array_agg(id ORDER BY d, id) FROM op_sort) = (SELECT array_agg(id ORDER BY d::date, id) FROM op_sort) AS same_order_as_date; same_order_as_date -------------------- t (1 row) SELECT min(d), max(d), count(DISTINCT d) > 3000 AS many_distinct, count(*) - count(DISTINCT d) > 0 AS has_duplicates FROM op_sort; min | max | many_distinct | has_duplicates -----------+----------+---------------+---------------- -infinity | infinity | t | t (1 row) -- btree build over heavy duplicates (20 distinct values, 20,000 rows): the -- deduplicated index is smaller than the same index without deduplication -- (so the opclass really is deduplication-safe) and returns the same rows CREATE TABLE op_dup (id int, d jdate); INSERT INTO op_dup SELECT g, CASE g % 20 WHEN 0 THEN 'infinity'::jdate WHEN 1 THEN '-infinity'::jdate WHEN 2 THEN '-0061-01-01'::jdate WHEN 3 THEN '-0001-12-29'::jdate WHEN 4 THEN '0000-01-01'::jdate ELSE '1403-01-01'::jdate + (g % 20) END FROM generate_series(1, 20000) g; CREATE INDEX op_dup_dedup ON op_dup (d) WITH (deduplicate_items = on); CREATE INDEX op_dup_nodedup ON op_dup (d) WITH (deduplicate_items = off); SELECT pg_relation_size('op_dup_dedup') < pg_relation_size('op_dup_nodedup') AS dedup_smaller; dedup_smaller --------------- t (1 row) DROP INDEX op_dup_nodedup; SET enable_seqscan = off; SET enable_bitmapscan = off; EXPLAIN (COSTS off) SELECT id FROM op_dup WHERE d = '-0001-12-29'::jdate; QUERY PLAN ------------------------------------------ Index Scan using op_dup_dedup on op_dup Index Cond: (d = '-0001-12-29'::jdate) (2 rows) SELECT d, count(*) AS idx_rows, sum(id) AS idx_sum FROM op_dup WHERE d IN ('infinity', '-infinity', '-0061-01-01', '-0001-12-29', '0000-01-01', '1403-01-10') GROUP BY d ORDER BY d; d | idx_rows | idx_sum -------------+----------+---------- -infinity | 1000 | 9991000 -0061-01-01 | 1000 | 9992000 -0001-12-29 | 1000 | 9993000 0000-01-01 | 1000 | 9994000 1403-01-10 | 1000 | 9999000 infinity | 1000 | 10010000 (6 rows) RESET enable_seqscan; RESET enable_bitmapscan; SET enable_indexscan = off; SET enable_bitmapscan = off; SELECT d, count(*) AS seq_rows, sum(id) AS seq_sum FROM op_dup WHERE d IN ('infinity', '-infinity', '-0061-01-01', '-0001-12-29', '0000-01-01', '1403-01-10') GROUP BY d ORDER BY d; d | seq_rows | seq_sum -------------+----------+---------- -infinity | 1000 | 9991000 -0061-01-01 | 1000 | 9992000 -0001-12-29 | 1000 | 9993000 0000-01-01 | 1000 | 9994000 1403-01-10 | 1000 | 9999000 infinity | 1000 | 10010000 (6 rows) RESET enable_indexscan; RESET enable_bitmapscan; DROP TABLE op_dup; -- the same on the 4,000-row table: index scan equals sequential scan CREATE INDEX op_sort_idx ON op_sort (d); SET enable_seqscan = off; SET enable_bitmapscan = off; SELECT count(*) AS idx_infinity, sum(id) AS idx_infinity_sum FROM op_sort WHERE d = 'infinity'::jdate; idx_infinity | idx_infinity_sum --------------+------------------ 80 | 162000 (1 row) SELECT count(*) AS idx_neg_infinity FROM op_sort WHERE d = '-infinity'::jdate; idx_neg_infinity ------------------ 80 (1 row) SELECT count(*) AS idx_min_edge FROM op_sort WHERE d = '-0061-01-01'::jdate; idx_min_edge -------------- 80 (1 row) SELECT count(*) AS idx_range, sum(id) AS idx_range_sum FROM op_sort WHERE d BETWEEN '-0010-01-01'::jdate AND '1403-01-01'::jdate; idx_range | idx_range_sum -----------+--------------- 1555 | 3067335 (1 row) RESET enable_seqscan; RESET enable_bitmapscan; SET enable_indexscan = off; SET enable_bitmapscan = off; SELECT count(*) AS seq_infinity, sum(id) AS seq_infinity_sum FROM op_sort WHERE d = 'infinity'::jdate; seq_infinity | seq_infinity_sum --------------+------------------ 80 | 162000 (1 row) SELECT count(*) AS seq_neg_infinity FROM op_sort WHERE d = '-infinity'::jdate; seq_neg_infinity ------------------ 80 (1 row) SELECT count(*) AS seq_min_edge FROM op_sort WHERE d = '-0061-01-01'::jdate; seq_min_edge -------------- 80 (1 row) SELECT count(*) AS seq_range, sum(id) AS seq_range_sum FROM op_sort WHERE d BETWEEN '-0010-01-01'::jdate AND '1403-01-01'::jdate; seq_range | seq_range_sum -----------+--------------- 1555 | 3067335 (1 row) RESET enable_indexscan; RESET enable_bitmapscan; DROP TABLE op_sort;