Skip to content

Commit d3f2fac

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting timestamp to other types per pg_cast
select castsource::regtype, casttarget::regtype, castfunc, castcontext,castmethod, pp.prosrc, pp.proname from pg_cast pc join pg_proc pp on pp.oid = pc.castfunc and pc.castfunc > 0 and (castsource::regtype ='timestamp'::regtype) order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname -----------------------------+-----------------------------+----------+-------------+------------+-----------------------+------------- timestamp without time zone | date | 2029 | a | f | timestamp_date | date timestamp without time zone | time without time zone | 1316 | a | f | timestamp_time | time timestamp without time zone | timestamp with time zone | 2028 | i | f | timestamp_timestamptz | timestamptz timestamp without time zone | timestamp without time zone | 1961 | i | f | timestamp_scale | timestamp (4 rows) discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent 55e963d commit d3f2fac

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/backend/utils/adt/date.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,10 @@ timestamp_date(PG_FUNCTION_ARGS)
13301330
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
13311331
DateADT result;
13321332

1333-
result = timestamp2date_safe(timestamp, NULL);
1333+
result = timestamp2date_safe(timestamp, fcinfo->context);
1334+
if (SOFT_ERROR_OCCURRED(fcinfo->context))
1335+
PG_RETURN_NULL();
1336+
13341337
PG_RETURN_DATEADT(result);
13351338
}
13361339

@@ -2008,7 +2011,7 @@ timestamp_time(PG_FUNCTION_ARGS)
20082011
PG_RETURN_NULL();
20092012

20102013
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
2011-
ereport(ERROR,
2014+
ereturn(fcinfo->context, (Datum) 0,
20122015
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
20132016
errmsg("timestamp out of range")));
20142017

src/backend/utils/adt/timestamp.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ timestamp_scale(PG_FUNCTION_ARGS)
352352

353353
result = timestamp;
354354

355-
AdjustTimestampForTypmod(&result, typmod, NULL);
355+
if (!AdjustTimestampForTypmod(&result, typmod, fcinfo->context))
356+
PG_RETURN_NULL();
356357

357358
PG_RETURN_TIMESTAMP(result);
358359
}
@@ -6432,8 +6433,13 @@ Datum
64326433
timestamp_timestamptz(PG_FUNCTION_ARGS)
64336434
{
64346435
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
6436+
TimestampTz result;
64356437

6436-
PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(timestamp));
6438+
result = timestamp2timestamptz_safe(timestamp, fcinfo->context);
6439+
if (SOFT_ERROR_OCCURRED(fcinfo->context))
6440+
PG_RETURN_NULL();
6441+
6442+
PG_RETURN_TIMESTAMPTZ(result);
64376443
}
64386444

64396445
/*

0 commit comments

Comments
 (0)