Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit713d9a8

Browse files
committed
Update some timestamp[tz] functions to use soft-error reporting
This commit updates two functions that convert "timestamptz" to"timestamp", and vice-versa, to use the soft error reporting rather thana their own logic to do the same. These are now named as follows:- timestamp2timestamptz_safe()- timestamptz2timestamp_safe()These functions were suffixed with "_opt_overflow", previously.This shaves some code, as it is possible to detect how a timestamp[tz]overflowed based on the returned value rather than a custom state. Itis optionally possible for the callers of these functions to rely on theerror generated internally by these functions, depending on the errorcontext.Similar work has been done ind03668e and4246a97.Reviewed-by: Amul Sul <sulamul@gmail.com>Discussion:https://postgr.es/m/aS09YF2GmVXjAxbJ@paquier.xyz
1 parent19b9662 commit713d9a8

File tree

3 files changed

+54
-87
lines changed

3 files changed

+54
-87
lines changed

‎contrib/btree_gin/btree_gin.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,11 @@ static Datum
508508
cvt_timestamptz_timestamp(Datuminput)
509509
{
510510
TimestampTzval=DatumGetTimestampTz(input);
511+
ErrorSaveContextescontext= {T_ErrorSaveContext};
511512
Timestampresult;
512-
intoverflow;
513513

514-
result=timestamptz2timestamp_opt_overflow(val,&overflow);
515-
/* We can ignorethe overflow result, since result is useful as-is */
514+
result=timestamptz2timestamp_safe(val,(Node*)&escontext);
515+
/* We can ignoreerrors, since result is useful as-is */
516516
returnTimestampGetDatum(result);
517517
}
518518

@@ -543,11 +543,11 @@ static Datum
543543
cvt_timestamp_timestamptz(Datuminput)
544544
{
545545
Timestampval=DatumGetTimestamp(input);
546+
ErrorSaveContextescontext= {T_ErrorSaveContext};
546547
TimestampTzresult;
547-
intoverflow;
548548

549-
result=timestamp2timestamptz_opt_overflow(val,&overflow);
550-
/* We can ignorethe overflow result, since result is useful as-is */
549+
result=timestamp2timestamptz_safe(val,(Node*)&escontext);
550+
/* We can ignoreerrors, since result is useful as-is */
551551
returnTimestampTzGetDatum(result);
552552
}
553553

‎src/backend/utils/adt/timestamp.c‎

Lines changed: 44 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,18 +2363,21 @@ int32
23632363
timestamp_cmp_timestamptz_internal(TimestamptimestampVal,TimestampTzdt2)
23642364
{
23652365
TimestampTzdt1;
2366-
intoverflow;
2366+
ErrorSaveContextescontext= {T_ErrorSaveContext};
23672367

2368-
dt1=timestamp2timestamptz_opt_overflow(timestampVal,&overflow);
2369-
if (overflow>0)
2368+
dt1=timestamp2timestamptz_safe(timestampVal,(Node*)&escontext);
2369+
if (escontext.error_occurred)
23702370
{
2371-
/* dt1 is larger than any finite timestamp, but less than infinity */
2372-
returnTIMESTAMP_IS_NOEND(dt2) ?-1 :+1;
2373-
}
2374-
if (overflow<0)
2375-
{
2376-
/* dt1 is less than any finite timestamp, but more than -infinity */
2377-
returnTIMESTAMP_IS_NOBEGIN(dt2) ?+1 :-1;
2371+
if (TIMESTAMP_IS_NOEND(dt1))
2372+
{
2373+
/* dt1 is larger than any finite timestamp, but less than infinity */
2374+
returnTIMESTAMP_IS_NOEND(dt2) ?-1 :+1;
2375+
}
2376+
if (TIMESTAMP_IS_NOBEGIN(dt1))
2377+
{
2378+
/* dt1 is less than any finite timestamp, but more than -infinity */
2379+
returnTIMESTAMP_IS_NOBEGIN(dt2) ?+1 :-1;
2380+
}
23782381
}
23792382

23802383
returntimestamptz_cmp_internal(dt1,dt2);
@@ -6434,25 +6437,22 @@ timestamp_timestamptz(PG_FUNCTION_ARGS)
64346437
/*
64356438
* Convert timestamp to timestamp with time zone.
64366439
*
6437-
* On successful conversion, *overflow is set to zero if it's not NULL.
6440+
* If the timestamp is finite but out of the valid range for timestamptz,
6441+
* error handling proceeds based on escontext.
64386442
*
6439-
* If the timestamp is finite but out of the valid range for timestamptz, then:
6440-
* if overflow is NULL, we throw an out-of-range error.
6441-
* if overflow is not NULL, we store +1 or -1 there to indicate the sign
6442-
* of the overflow, and return the appropriate timestamptz infinity.
6443+
* If escontext is NULL, we throw an out-of-range error (hard error).
6444+
* If escontext is not NULL, we return NOBEGIN or NOEND for lower bound or
6445+
* upper bound overflow, respectively, and record a soft error.
64436446
*/
64446447
TimestampTz
6445-
timestamp2timestamptz_opt_overflow(Timestamptimestamp,int*overflow)
6448+
timestamp2timestamptz_safe(Timestamptimestamp,Node*escontext)
64466449
{
64476450
TimestampTzresult;
64486451
structpg_tmtt,
64496452
*tm=&tt;
64506453
fsec_tfsec;
64516454
inttz;
64526455

6453-
if (overflow)
6454-
*overflow=0;
6455-
64566456
if (TIMESTAMP_NOT_FINITE(timestamp))
64576457
returntimestamp;
64586458

@@ -6467,26 +6467,14 @@ timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
64676467
returnresult;
64686468
}
64696469

6470-
if (overflow)
6471-
{
6472-
if (timestamp<0)
6473-
{
6474-
*overflow=-1;
6475-
TIMESTAMP_NOBEGIN(result);
6476-
}
6477-
else
6478-
{
6479-
*overflow=1;
6480-
TIMESTAMP_NOEND(result);
6481-
}
6482-
returnresult;
6483-
}
6470+
if (timestamp<0)
6471+
TIMESTAMP_NOBEGIN(result);
6472+
else
6473+
TIMESTAMP_NOEND(result);
64846474

6485-
ereport(ERROR,
6475+
ereturn(escontext,result,
64866476
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
64876477
errmsg("timestamp out of range")));
6488-
6489-
return0;
64906478
}
64916479

64926480
/*
@@ -6495,7 +6483,7 @@ timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
64956483
staticTimestampTz
64966484
timestamp2timestamptz(Timestamptimestamp)
64976485
{
6498-
returntimestamp2timestamptz_opt_overflow(timestamp,NULL);
6486+
returntimestamp2timestamptz_safe(timestamp,NULL);
64996487
}
65006488

65016489
/* timestamptz_timestamp()
@@ -6515,72 +6503,51 @@ timestamptz_timestamp(PG_FUNCTION_ARGS)
65156503
staticTimestamp
65166504
timestamptz2timestamp(TimestampTztimestamp)
65176505
{
6518-
returntimestamptz2timestamp_opt_overflow(timestamp,NULL);
6506+
returntimestamptz2timestamp_safe(timestamp,NULL);
65196507
}
65206508

65216509
/*
65226510
* Convert timestamp with time zone to timestamp.
65236511
*
6524-
* On successful conversion, *overflow is set to zero if it's not NULL.
6512+
* If the timestamptz is finite but out of the valid range for timestamp,
6513+
* error handling proceeds based on escontext.
65256514
*
6526-
* If the timestamptz is finite but out of the valid range for timestamp, then:
6527-
* if overflow is NULL, we throw an out-of-range error.
6528-
* if overflow is not NULL, we store +1 or -1 there to indicate the sign
6529-
* of the overflow, and return the appropriate timestamp infinity.
6515+
* If escontext is NULL, we throw an out-of-range error (hard error).
6516+
* If escontext is not NULL, we return NOBEGIN or NOEND for lower bound or
6517+
* upper bound overflow, respectively, and record a soft error.
65306518
*/
65316519
Timestamp
6532-
timestamptz2timestamp_opt_overflow(TimestampTztimestamp,int*overflow)
6520+
timestamptz2timestamp_safe(TimestampTztimestamp,Node*escontext)
65336521
{
65346522
Timestampresult;
65356523
structpg_tmtt,
65366524
*tm=&tt;
65376525
fsec_tfsec;
65386526
inttz;
65396527

6540-
if (overflow)
6541-
*overflow=0;
6542-
65436528
if (TIMESTAMP_NOT_FINITE(timestamp))
65446529
result=timestamp;
65456530
else
65466531
{
65476532
if (timestamp2tm(timestamp,&tz,tm,&fsec,NULL,NULL)!=0)
65486533
{
6549-
if (overflow)
6550-
{
6551-
if (timestamp<0)
6552-
{
6553-
*overflow=-1;
6554-
TIMESTAMP_NOBEGIN(result);
6555-
}
6556-
else
6557-
{
6558-
*overflow=1;
6559-
TIMESTAMP_NOEND(result);
6560-
}
6561-
returnresult;
6562-
}
6563-
ereport(ERROR,
6534+
if (timestamp<0)
6535+
TIMESTAMP_NOBEGIN(result);
6536+
else
6537+
TIMESTAMP_NOEND(result);
6538+
6539+
ereturn(escontext,result,
65646540
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
65656541
errmsg("timestamp out of range")));
65666542
}
65676543
if (tm2timestamp(tm,fsec,NULL,&result)!=0)
65686544
{
6569-
if (overflow)
6570-
{
6571-
if (timestamp<0)
6572-
{
6573-
*overflow=-1;
6574-
TIMESTAMP_NOBEGIN(result);
6575-
}
6576-
else
6577-
{
6578-
*overflow=1;
6579-
TIMESTAMP_NOEND(result);
6580-
}
6581-
returnresult;
6582-
}
6583-
ereport(ERROR,
6545+
if (timestamp<0)
6546+
TIMESTAMP_NOBEGIN(result);
6547+
else
6548+
TIMESTAMP_NOEND(result);
6549+
6550+
ereturn(escontext,result,
65846551
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
65856552
errmsg("timestamp out of range")));
65866553
}

‎src/include/utils/timestamp.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ extern inttimestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
142142
/* timestamp comparison works for timestamptz also */
143143
#definetimestamptz_cmp_internal(dt1,dt2)timestamp_cmp_internal(dt1, dt2)
144144

145-
externTimestampTztimestamp2timestamptz_opt_overflow(Timestamptimestamp,
146-
int*overflow);
147-
externTimestamptimestamptz2timestamp_opt_overflow(TimestampTztimestamp,
148-
int*overflow);
145+
externTimestampTztimestamp2timestamptz_safe(Timestamptimestamp,
146+
Node*escontext);
147+
externTimestamptimestamptz2timestamp_safe(TimestampTztimestamp,
148+
Node*escontext);
149149

150150
externint32timestamp_cmp_timestamptz_internal(TimestamptimestampVal,
151151
TimestampTzdt2);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp