-- comparisons SELECT '1402-01-01'::jdate = '1402-01-01'::jdate AS eq_true; SELECT '1402-01-01'::jdate = '1403-01-01'::jdate AS eq_false; SELECT '1402-01-01'::jdate <> '1403-01-01'::jdate AS ne_true; SELECT '1402-01-01'::jdate < '1403-01-01'::jdate AS lt_true; SELECT '1403-01-01'::jdate < '1402-01-01'::jdate AS lt_false; SELECT '1402-01-01'::jdate <= '1402-01-01'::jdate AS le_true; SELECT '1403-01-01'::jdate > '1402-01-01'::jdate AS gt_true; SELECT '1403-01-01'::jdate >= '1403-01-01'::jdate AS ge_true; -- 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; -- 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; EXPLAIN (COSTS off) SELECT * FROM op_dates WHERE d > '1402-01-01'::jdate ORDER BY d; 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; SELECT d, count(*) FROM op_dates GROUP BY d ORDER BY d; -- min/max aggregates SELECT min(d), max(d), pg_typeof(min(d)) FROM op_dates; 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'; -- 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; -- 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; -- 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; -- 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'; -- 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; 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; SELECT min(d), max(d), count(DISTINCT d) > 3000 AS many_distinct, count(*) - count(DISTINCT d) > 0 AS has_duplicates FROM op_sort; -- 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; 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; 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; 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; 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; SELECT count(*) AS idx_neg_infinity FROM op_sort WHERE d = '-infinity'::jdate; SELECT count(*) AS idx_min_edge FROM op_sort WHERE d = '-0061-01-01'::jdate; 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; 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; SELECT count(*) AS seq_neg_infinity FROM op_sort WHERE d = '-infinity'::jdate; SELECT count(*) AS seq_min_edge FROM op_sort WHERE d = '-0061-01-01'::jdate; 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; RESET enable_indexscan; RESET enable_bitmapscan; DROP TABLE op_sort;