Skip to content

Commit e08d082

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 7469370 commit e08d082

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/backend/utils/adt/date.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,16 @@ timestamp_date(PG_FUNCTION_ARGS)
13731373
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
13741374
DateADT result;
13751375

1376-
result = timestamp2date_opt_overflow(timestamp, NULL);
1376+
if (likely(!fcinfo->context))
1377+
result = timestamptz2date_opt_overflow(timestamp, NULL);
1378+
else
1379+
{
1380+
int overflow;
1381+
result = timestamp2date_opt_overflow(timestamp, &overflow);
1382+
1383+
if (overflow != 0)
1384+
PG_RETURN_NULL();
1385+
}
13771386
PG_RETURN_DATEADT(result);
13781387
}
13791388

@@ -2089,7 +2098,7 @@ timestamp_time(PG_FUNCTION_ARGS)
20892098
PG_RETURN_NULL();
20902099

20912100
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
2092-
ereport(ERROR,
2101+
ereturn(fcinfo->context, (Datum) 0,
20932102
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
20942103
errmsg("timestamp out of range")));
20952104

src/backend/utils/adt/timestamp.c

Lines changed: 15 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
}
@@ -6430,7 +6431,19 @@ timestamp_timestamptz(PG_FUNCTION_ARGS)
64306431
{
64316432
Timestamp timestamp = PG_GETARG_TIMESTAMP(0);
64326433

6433-
PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(timestamp));
6434+
if (likely(!fcinfo->context))
6435+
PG_RETURN_TIMESTAMPTZ(timestamp2timestamptz(timestamp));
6436+
else
6437+
{
6438+
TimestampTz result;
6439+
int overflow;
6440+
6441+
result = timestamp2timestamptz_opt_overflow(timestamp, &overflow);
6442+
if (overflow != 0)
6443+
PG_RETURN_NULL();
6444+
else
6445+
PG_RETURN_TIMESTAMPTZ(result);
6446+
}
64346447
}
64356448

64366449
/*

0 commit comments

Comments
 (0)