-- jtimestamp and jtimestamptz report bad input softly (through an -- ErrorSaveContext, with our type name and core's SQLSTATE), so -- pg_input_is_valid, pg_input_error_info, array input and COPY ... ON_ERROR -- ignore can handle it instead of aborting the statement. Modelled on -- softerr.sql; only SQLSTATEs are printed, never message text. -- -- Version note: pg_input_is_valid/pg_input_error_info need PostgreSQL 16+, -- but COPY ... (ON_ERROR ignore) needs PostgreSQL 17+, so the COPY half of -- this file does not run on 16 (softerr_1.out is the alternate for softerr; -- jt_softerr would need the same). Whether core's timestamp_in and -- timestamptz_in report all their errors softly on 16 has not been verified: -- the first CI run on 16 decides whether a jt_softerr_1.out is needed. -- -- The table created here is dropped at the end: extension_schema runs -- DROP EXTENSION, which needs no column of our types left behind. SET DateStyle = 'ISO, YMD'; SET timezone = 'UTC'; -- --------------------------------------------------------------------- -- pg_input_is_valid and the SQLSTATE of pg_input_error_info, for the same -- inputs under both types. sqlstate is NULL for a good input. -- --------------------------------------------------------------------- CREATE TEMP TABLE soft_in (n int, kind text, v text); INSERT INTO soft_in VALUES (1, 'good', '1403-05-15 10:00:00'), (2, 'good', '1403-05-15'), (3, 'good, fraction', '1403-05-15 10:00:00.123456'), (4, 'good, offset', '1403-05-15 10:00:00+03:30'), (5, 'good, 24:00:00', '1403-05-15 24:00:00'), (6, 'good, first', '-0061-01-01 16:00:00+00'), (7, 'good, last', '3177-12-29 07:59:59.999999+00'), (8, 'infinity', 'infinity'), (9, '-infinity', '-infinity'), (10, 'epoch', 'epoch'), (11, 'syntax', 'garbage'), (12, 'syntax, empty', ''), (13, 'syntax, time only', '12:00:00'), (14, 'syntax, Gregorian words', 'March 20, 2024 12:00'), (15, 'syntax, duplicate date', '1403-05-15 1403-05-16'), (16, 'month', '1403-13-01 10:00:00'), (17, 'day (not a leap year)', '1404-12-30 10:00:00'), (18, 'day (month 7)', '1403-07-31 10:00:00'), (19, 'range, above', '3178-01-01 00:00:00'), (20, 'range, below', '-0062-12-29 00:00:00'), (21, 'range, last day end (above for jtimestamptz)', '3177-12-29 23:59:59.999999+00'), (22, 'range, BC', '1403-05-15 10:00:00 BC'), (23, 'overflow', '99999999999-01-01 10:00:00'), (24, 'time field, hour 25', '1403-05-15 25:00:00'), (25, 'time field, minute 60', '1403-05-15 10:60:00'), (26, 'time field, second 61', '1403-05-15 10:00:61'), (27, 'zone, unknown', '1403-05-15 10:00:00 Mars/Olympus'), (28, 'zone, displacement +16:00', '1403-05-15 10:00:00+16:00'), (29, 'zone, displacement -16:00', '1403-05-15 10:00:00-16:00'); SELECT s.n, s.kind, t.typ, pg_input_is_valid(s.v, t.typ) AS valid, (SELECT e.sql_error_code FROM pg_input_error_info(s.v, t.typ) e) AS sqlstate FROM soft_in s CROSS JOIN (VALUES ('jtimestamp'), ('jtimestamptz')) AS t(typ) ORDER BY s.n, t.typ; n | kind | typ | valid | sqlstate ----+----------------------------------------------+--------------+-------+---------- 1 | good | jtimestamp | t | 1 | good | jtimestamptz | t | 2 | good | jtimestamp | t | 2 | good | jtimestamptz | t | 3 | good, fraction | jtimestamp | t | 3 | good, fraction | jtimestamptz | t | 4 | good, offset | jtimestamp | t | 4 | good, offset | jtimestamptz | t | 5 | good, 24:00:00 | jtimestamp | t | 5 | good, 24:00:00 | jtimestamptz | t | 6 | good, first | jtimestamp | t | 6 | good, first | jtimestamptz | t | 7 | good, last | jtimestamp | t | 7 | good, last | jtimestamptz | t | 8 | infinity | jtimestamp | t | 8 | infinity | jtimestamptz | t | 9 | -infinity | jtimestamp | t | 9 | -infinity | jtimestamptz | t | 10 | epoch | jtimestamp | t | 10 | epoch | jtimestamptz | t | 11 | syntax | jtimestamp | f | 22007 11 | syntax | jtimestamptz | f | 22007 12 | syntax, empty | jtimestamp | f | 22007 12 | syntax, empty | jtimestamptz | f | 22007 13 | syntax, time only | jtimestamp | f | 22007 13 | syntax, time only | jtimestamptz | f | 22007 14 | syntax, Gregorian words | jtimestamp | f | 22007 14 | syntax, Gregorian words | jtimestamptz | f | 22007 15 | syntax, duplicate date | jtimestamp | f | 22007 15 | syntax, duplicate date | jtimestamptz | f | 22007 16 | month | jtimestamp | f | 22008 16 | month | jtimestamptz | f | 22008 17 | day (not a leap year) | jtimestamp | f | 22008 17 | day (not a leap year) | jtimestamptz | f | 22008 18 | day (month 7) | jtimestamp | f | 22008 18 | day (month 7) | jtimestamptz | f | 22008 19 | range, above | jtimestamp | f | 22008 19 | range, above | jtimestamptz | f | 22008 20 | range, below | jtimestamp | f | 22008 20 | range, below | jtimestamptz | f | 22008 21 | range, last day end (above for jtimestamptz) | jtimestamp | t | 21 | range, last day end (above for jtimestamptz) | jtimestamptz | f | 22008 22 | range, BC | jtimestamp | f | 22008 22 | range, BC | jtimestamptz | f | 22008 23 | overflow | jtimestamp | f | 22008 23 | overflow | jtimestamptz | f | 22008 24 | time field, hour 25 | jtimestamp | f | 22008 24 | time field, hour 25 | jtimestamptz | f | 22008 25 | time field, minute 60 | jtimestamp | f | 22008 25 | time field, minute 60 | jtimestamptz | f | 22008 26 | time field, second 61 | jtimestamp | f | 22008 26 | time field, second 61 | jtimestamptz | f | 22008 27 | zone, unknown | jtimestamp | f | 22023 27 | zone, unknown | jtimestamptz | f | 22023 28 | zone, displacement +16:00 | jtimestamp | f | 22009 28 | zone, displacement +16:00 | jtimestamptz | f | 22009 29 | zone, displacement -16:00 | jtimestamp | f | 22009 29 | zone, displacement -16:00 | jtimestamptz | f | 22009 (58 rows) -- the input function with a typmod: rounding happens before the range check SELECT t.typ, s.kind, pg_input_is_valid(s.v, t.typ || '(0)') AS valid, (SELECT e.sql_error_code FROM pg_input_error_info(s.v, t.typ || '(0)') e) AS sqlstate FROM (VALUES (1, 'jtimestamp', 'last microsecond rounds up past the end', '3177-12-29 23:59:59.999999'), (2, 'jtimestamp', 'last second, .4 rounds down', '3177-12-29 23:59:59.4'), (3, 'jtimestamptz', 'last microsecond rounds up past the end', '3177-12-29 07:59:59.999999+00'), (4, 'jtimestamptz', 'last second, .4 rounds down', '3177-12-29 07:59:59.4+00') ) AS s(n, typ, kind, v) JOIN (VALUES ('jtimestamp'), ('jtimestamptz')) AS t(typ) USING (typ) ORDER BY s.n; typ | kind | valid | sqlstate --------------+-----------------------------------------+-------+---------- jtimestamp | last microsecond rounds up past the end | f | 22008 jtimestamp | last second, .4 rounds down | t | jtimestamptz | last microsecond rounds up past the end | f | 22008 jtimestamptz | last second, .4 rounds down | t | (4 rows) -- array input: one bad element makes the whole literal invalid SELECT t.typ, a.kind, pg_input_is_valid(a.v, t.typ || '[]') AS valid, (SELECT e.sql_error_code FROM pg_input_error_info(a.v, t.typ || '[]') e) AS sqlstate FROM (VALUES (1, 'good', '{"1403-01-01 10:00:00","1403-12-29 23:59:59"}'), (2, 'good, with NULL', '{"1403-01-01 10:00:00",NULL}'), (3, 'good, infinity', '{infinity,-infinity}'), (4, 'bad element', '{"1403-01-01 10:00:00",x}'), (5, 'range element', '{"1403-01-01 10:00:00","3178-01-01 00:00:00"}'), (6, 'month element', '{"1403-13-01 10:00:00"}'), (7, 'time-field element', '{"1403-05-15 25:00:00"}'), (8, 'zone element', '{"1403-05-15 10:00:00 Mars/Olympus"}'), (9, 'displacement element', '{"1403-05-15 10:00:00+16:00"}') ) AS a(n, kind, v) CROSS JOIN (VALUES ('jtimestamp'), ('jtimestamptz')) AS t(typ) ORDER BY a.n, t.typ; typ | kind | valid | sqlstate --------------+----------------------+-------+---------- jtimestamp | good | t | jtimestamptz | good | t | jtimestamp | good, with NULL | t | jtimestamptz | good, with NULL | t | jtimestamp | good, infinity | t | jtimestamptz | good, infinity | t | jtimestamp | bad element | f | 22007 jtimestamptz | bad element | f | 22007 jtimestamp | range element | f | 22008 jtimestamptz | range element | f | 22008 jtimestamp | month element | f | 22008 jtimestamptz | month element | f | 22008 jtimestamp | time-field element | f | 22008 jtimestamptz | time-field element | f | 22008 jtimestamp | zone element | f | 22023 jtimestamptz | zone element | f | 22023 jtimestamp | displacement element | f | 22009 jtimestamptz | displacement element | f | 22009 (18 rows) -- the same array literal gives an array when it is valid SELECT '{"1403-01-01 10:00:00",NULL,infinity}'::jtimestamp[] AS ts_array, '{"1403-01-01 10:00:00+03:30",NULL,-infinity}'::jtimestamptz[] AS tz_array; ts_array | tz_array ---------------------------------------+------------------------------------------- {"1403-01-01 10:00:00",NULL,infinity} | {"1403-01-01 06:30:00+00",NULL,-infinity} (1 row) -- hard errors still raise, with the same SQLSTATEs \set VERBOSITY sqlstate SELECT 'garbage'::jtimestamp; ERROR: 22007 SELECT 'garbage'::jtimestamptz; ERROR: 22007 SELECT '1403-13-01 10:00:00'::jtimestamp; ERROR: 22008 SELECT '1403-13-01 10:00:00'::jtimestamptz; ERROR: 22008 SELECT '3178-01-01 00:00:00'::jtimestamp; ERROR: 22008 SELECT '3178-01-01 00:00:00+00'::jtimestamptz; ERROR: 22008 SELECT '1403-05-15 25:00:00'::jtimestamp; ERROR: 22008 SELECT '1403-05-15 25:00:00'::jtimestamptz; ERROR: 22008 SELECT '1403-05-15 10:00:00 Mars/Olympus'::jtimestamp; ERROR: 22023 SELECT '1403-05-15 10:00:00 Mars/Olympus'::jtimestamptz; ERROR: 22023 SELECT '1403-05-15 10:00:00+16:00'::jtimestamptz; ERROR: 22009 SELECT '{"1403-01-01 10:00:00",x}'::jtimestamp[]; ERROR: 22007 SELECT '{"1403-01-01 10:00:00+00","3178-01-01 00:00:00+00"}'::jtimestamptz[]; ERROR: 22008 \set VERBOSITY default -- --------------------------------------------------------------------- -- COPY ... ON_ERROR ignore skips bad rows and loads the good ones. Row 1, -- 7 and 9 are good; the others each fail in one column. -- --------------------------------------------------------------------- CREATE TABLE jt_softerr_t (id int, ts jtimestamp, tz jtimestamptz); COPY jt_softerr_t FROM STDIN (ON_ERROR ignore); ERROR: option "on_error" not recognized LINE 1: COPY jt_softerr_t FROM STDIN (ON_ERROR ignore); ^ SELECT id, ts, tz FROM jt_softerr_t ORDER BY id; id | ts | tz ----+----+---- (0 rows) -- a typmod column rounds in the input function, so a value that rounds out -- of the range is skipped and one that rounds into it loads CREATE TABLE jt_softerr_p (id int, ts jtimestamp(0), tz jtimestamptz(0)); COPY jt_softerr_p FROM STDIN (ON_ERROR ignore); ERROR: option "on_error" not recognized LINE 1: COPY jt_softerr_p FROM STDIN (ON_ERROR ignore); ^ SELECT id, ts, tz FROM jt_softerr_p ORDER BY id; id | ts | tz ----+----+---- (0 rows) -- the default, ON_ERROR stop, still aborts on the first bad row (one run -- with the bad value in each column) and keeps the earlier rows out TRUNCATE jt_softerr_t; \set VERBOSITY sqlstate COPY jt_softerr_t FROM STDIN (ON_ERROR stop); ERROR: 42601 COPY jt_softerr_t FROM STDIN (ON_ERROR stop); ERROR: 42601 COPY jt_softerr_t FROM STDIN; ERROR: 22009 \set VERBOSITY default SELECT count(*) AS rows_after_stop FROM jt_softerr_t; rows_after_stop ----------------- 0 (1 row) DROP TABLE jt_softerr_t, jt_softerr_p;