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

Appearance settings

Commit 7aa81c6

Browse filesBrowse files
committed
Fix precision handling for some COERCE_SQL_SYNTAX functions
f193883 has been incorrectly setting up the precision used in the timestamp compilations returned by the following functions: - LOCALTIME - LOCALTIMESTAMP - CURRENT_TIME - CURRENT_TIMESTAMP Specifying an out-of-range precision for CURRENT_TIMESTAMP and LOCALTIMESTAMP was raising a WARNING without adjusting the precision, leading to a subsequent error. LOCALTIME and CURRENT_TIME raised a WARNING without an error, still the precision given to the internal routines was not correct, so let's be clean. Ian has reported the problems in timestamp.c, while I have noticed the ones in date.c. Regression tests are added for all of them with precisions high enough to provide coverage for the warnings, something that went missing up to this commit. Author: Ian Lawrence Barwick, Michael Paquier Discussion: https://postgr.es/m/CAB8KJ=jQEnn9sYG+N752spt68wMrhmT-ocHCh4oeNmHF82QMWA@mail.gmail.com
1 parent 1f605b8 commit 7aa81c6
Copy full SHA for 7aa81c6

File tree

4 files changed

+40
-18
lines changed
Filter options

4 files changed

+40
-18
lines changed

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

Copy file name to clipboardExpand all lines: src/backend/utils/adt/date.c
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,7 @@ current_time(PG_FUNCTION_ARGS)
347347
int32 typmod = -1;
348348

349349
if (!PG_ARGISNULL(0))
350-
{
351-
typmod = PG_GETARG_INT32(0);
352-
anytime_typmod_check(true, typmod);
353-
}
350+
typmod = anytime_typmod_check(true, PG_GETARG_INT32(0));
354351

355352
GetCurrentTimeUsec(tm, &fsec, &tz);
356353

@@ -375,10 +372,7 @@ sql_localtime(PG_FUNCTION_ARGS)
375372
int32 typmod = -1;
376373

377374
if (!PG_ARGISNULL(0))
378-
{
379-
typmod = PG_GETARG_INT32(0);
380-
anytime_typmod_check(false, typmod);
381-
}
375+
typmod = anytime_typmod_check(false, PG_GETARG_INT32(0));
382376

383377
GetCurrentTimeUsec(tm, &fsec, &tz);
384378

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

Copy file name to clipboardExpand all lines: src/backend/utils/adt/timestamp.c
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,10 +1606,7 @@ current_timestamp(PG_FUNCTION_ARGS)
16061606
int32 typmod = -1;
16071607

16081608
if (!PG_ARGISNULL(0))
1609-
{
1610-
typmod = PG_GETARG_INT32(0);
1611-
anytimestamp_typmod_check(true, typmod);
1612-
}
1609+
typmod = anytimestamp_typmod_check(true, PG_GETARG_INT32(0));
16131610

16141611
ts = GetCurrentTransactionStartTimestamp();
16151612
if (typmod >= 0)
@@ -1627,10 +1624,7 @@ sql_localtimestamp(PG_FUNCTION_ARGS)
16271624
int32 typmod = -1;
16281625

16291626
if (!PG_ARGISNULL(0))
1630-
{
1631-
typmod = PG_GETARG_INT32(0);
1632-
anytimestamp_typmod_check(false, typmod);
1633-
}
1627+
typmod = anytimestamp_typmod_check(false, PG_GETARG_INT32(0));
16341628

16351629
ts = timestamptz2timestamp(GetCurrentTransactionStartTimestamp());
16361630
if (typmod >= 0)

‎src/test/regress/expected/expressions.out

Copy file name to clipboardExpand all lines: src/test/regress/expected/expressions.out
+30-1Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ SELECT now()::time(3)::text = localtime(3)::text;
3636
t
3737
(1 row)
3838

39-
-- current_timestamp / localtimestamp (always matches because of transactional behaviour)
39+
-- current_time[stamp]/ localtime[stamp] (always matches because of transactional behaviour)
4040
SELECT current_timestamp = NOW();
4141
?column?
4242
----------
@@ -57,6 +57,35 @@ SELECT now()::timestamp::text = localtimestamp::text;
5757
t
5858
(1 row)
5959

60+
-- precision overflow
61+
SELECT current_time = current_time(7);
62+
WARNING: TIME(7) WITH TIME ZONE precision reduced to maximum allowed, 6
63+
?column?
64+
----------
65+
t
66+
(1 row)
67+
68+
SELECT current_timestamp = current_timestamp(7);
69+
WARNING: TIMESTAMP(7) WITH TIME ZONE precision reduced to maximum allowed, 6
70+
?column?
71+
----------
72+
t
73+
(1 row)
74+
75+
SELECT localtime = localtime(7);
76+
WARNING: TIME(7) precision reduced to maximum allowed, 6
77+
?column?
78+
----------
79+
t
80+
(1 row)
81+
82+
SELECT localtimestamp = localtimestamp(7);
83+
WARNING: TIMESTAMP(7) precision reduced to maximum allowed, 6
84+
?column?
85+
----------
86+
t
87+
(1 row)
88+
6089
-- current_role/user/user is tested in rolnames.sql
6190
-- current database / catalog
6291
SELECT current_catalog = current_database();

‎src/test/regress/sql/expressions.sql

Copy file name to clipboardExpand all lines: src/test/regress/sql/expressions.sql
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ SELECT now()::timetz(4)::text = current_time(4)::text;
1717
SELECT now()::time::text = localtime::text;
1818
SELECT now()::time(3)::text = localtime(3)::text;
1919

20-
-- current_timestamp / localtimestamp (always matches because of transactional behaviour)
20+
-- current_time[stamp]/ localtime[stamp] (always matches because of transactional behaviour)
2121
SELECT current_timestamp = NOW();
2222
-- precision
2323
SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text);
2424
-- localtimestamp
2525
SELECT now()::timestamp::text = localtimestamp::text;
26+
-- precision overflow
27+
SELECT current_time = current_time(7);
28+
SELECT current_timestamp = current_timestamp(7);
29+
SELECT localtime = localtime(7);
30+
SELECT localtimestamp = localtimestamp(7);
2631

2732
-- current_role/user/user is tested in rolnames.sql
2833

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.