Hi

2022年11月21日(月) 18:39 Michael Paquier <mich...@paquier.xyz>:
>
> On Sun, Nov 20, 2022 at 03:15:34PM -0800, Ted Yu wrote:
> > + * timestamp.  These require a specific handling with their typmod is given
> > + * by the function caller through their SQL keyword.
> >
> > typo: typmod is given -> typmod given
> >
> > Other than the above, code looks good to me.
>
> Thanks for double-checking.  I intended a different wording, actually,
> so fixed this one.  And applied after an extra round of reviews.

I noticed this commit (f193883f) introduces following regressions:

    postgres=# SELECT current_timestamp(7);
    WARNING:  TIMESTAMP(7) WITH TIME ZONE precision reduced to maximum
allowed, 6
    ERROR:  timestamp(7) precision must be between 0 and 6

    postgres=# SELECT localtimestamp(7);
    WARNING:  TIMESTAMP(7) precision reduced to maximum allowed, 6
    ERROR:  timestamp(7) precision must be between 0 and 6

Suggested fix attached.

Regards

Ian Barwick
From b599765b222aacbc860ba43c42ec658ed1a8b989 Mon Sep 17 00:00:00 2001
From: Ian Barwick <barw...@gmail.com>
Date: Fri, 30 Dec 2022 10:48:31 +0900
Subject: [PATCH v1] Fix [current_|local]timestamp precision reduction

Fixes regression introduced in f193883f.
---
 src/backend/utils/adt/timestamp.c         | 10 ++--------
 src/test/regress/expected/expressions.out | 15 +++++++++++++++
 src/test/regress/sql/expressions.sql      |  3 +++
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 3f2508c0c4..b23cce1136 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -1606,10 +1606,7 @@ current_timestamp(PG_FUNCTION_ARGS)
 	int32		typmod = -1;
 
 	if (!PG_ARGISNULL(0))
-	{
-		typmod = PG_GETARG_INT32(0);
-		anytimestamp_typmod_check(true, typmod);
-	}
+		typmod = anytimestamp_typmod_check(true, PG_GETARG_INT32(0));
 
 	ts = GetCurrentTransactionStartTimestamp();
 	if (typmod >= 0)
@@ -1627,10 +1624,7 @@ sql_localtimestamp(PG_FUNCTION_ARGS)
 	int32		typmod = -1;
 
 	if (!PG_ARGISNULL(0))
-	{
-		typmod = PG_GETARG_INT32(0);
-		anytimestamp_typmod_check(false, typmod);
-	}
+		typmod = anytimestamp_typmod_check(false, PG_GETARG_INT32(0));
 
 	ts = timestamptz2timestamp(GetCurrentTransactionStartTimestamp());
 	if (typmod >= 0)
diff --git a/src/test/regress/expected/expressions.out b/src/test/regress/expected/expressions.out
index 28a20900f1..fd38830a77 100644
--- a/src/test/regress/expected/expressions.out
+++ b/src/test/regress/expected/expressions.out
@@ -50,6 +50,14 @@ SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text);
  t
 (1 row)
 
+-- precision overflow
+SELECT current_timestamp = current_timestamp(7);
+WARNING:  TIMESTAMP(7) WITH TIME ZONE precision reduced to maximum allowed, 6
+ ?column? 
+----------
+ t
+(1 row)
+
 -- localtimestamp
 SELECT now()::timestamp::text = localtimestamp::text;
  ?column? 
@@ -57,6 +65,13 @@ SELECT now()::timestamp::text = localtimestamp::text;
  t
 (1 row)
 
+SELECT localtimestamp = localtimestamp(7);
+WARNING:  TIMESTAMP(7) precision reduced to maximum allowed, 6
+ ?column? 
+----------
+ t
+(1 row)
+
 -- current_role/user/user is tested in rolnames.sql
 -- current database / catalog
 SELECT current_catalog = current_database();
diff --git a/src/test/regress/sql/expressions.sql b/src/test/regress/sql/expressions.sql
index f9a0299d17..57dfb37745 100644
--- a/src/test/regress/sql/expressions.sql
+++ b/src/test/regress/sql/expressions.sql
@@ -21,8 +21,11 @@ SELECT now()::time(3)::text = localtime(3)::text;
 SELECT current_timestamp = NOW();
 -- precision
 SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text);
+-- precision overflow
+SELECT current_timestamp = current_timestamp(7);
 -- localtimestamp
 SELECT now()::timestamp::text = localtimestamp::text;
+SELECT localtimestamp = localtimestamp(7);
 
 -- current_role/user/user is tested in rolnames.sql
 

base-commit: 388e80132c007a7528abc987e347e41432dc1542
-- 
2.31.1

Reply via email to