[PATCH] Remove workarounds to format [u]int64's

Started by Aleksander Alekseevalmost 4 years ago12 messages
#1Aleksander Alekseev
aleksander@timescale.com
1 attachment(s)

Hi hackers,

I learned from Tom [1]/messages/by-id/771048.1647528068@sss.pgh.pa.us that we can simplify the code like:

```
char buff[32];
snprintf(buf, sizeof(buf), INT64_FORMAT, ...)
ereport(WARNING, (errmsg("%s ...", buf)));
```

... and rely on %lld/%llu now as long as we explicitly cast the
argument to long long int / unsigned long long. This was previously
addressed in 6a1cd8b9 and d914eb34, but I see more places where we
still use an old approach.

Suggested patch fixes this. Tested locally - no warnings; passes all the tests.

[1]: /messages/by-id/771048.1647528068@sss.pgh.pa.us

--
Best regards,
Aleksander Alekseev

Attachments:

v1-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v1-0001-Remove-workarounds-to-format-u-int64-s.patchDownload
From 152a9b9e8f1eaa9bd319daca7d1adff282c0c321 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Mon, 21 Mar 2022 11:01:57 +0300
Subject: [PATCH v1] Remove workarounds to format [u]int64's

Now that we always use our own sprintf(), we can rely on %lld and %llu to be
portable, so we can use those. This reduces the code verbosity.

Author: Aleksander Alekseev <aleksander@timescale.com>
---
 src/backend/access/brin/brin.c    |  8 +--
 src/backend/commands/copyfrom.c   | 28 ++++-----
 src/backend/commands/sequence.c   | 95 ++++++++-----------------------
 src/backend/utils/adt/xid8funcs.c |  5 +-
 4 files changed, 39 insertions(+), 97 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index ba78ecff66..c44b807619 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -1018,11 +1018,9 @@ brin_summarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long int)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
@@ -1095,11 +1093,9 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long int)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 7b3f5a84b8..bab40c4201 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -115,21 +115,17 @@ void
 CopyFromErrorCallback(void *arg)
 {
 	CopyFromState cstate = (CopyFromState) arg;
-	char		curlineno_str[32];
-
-	snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
-			 cstate->cur_lineno);
 
 	if (cstate->opts.binary)
 	{
 		/* can't usefully display the data */
 		if (cstate->cur_attname)
-			errcontext("COPY %s, line %s, column %s",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		else
-			errcontext("COPY %s, line %s",
-					   cstate->cur_relname, curlineno_str);
+			errcontext("COPY %s, line %llu",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 	}
 	else
 	{
@@ -139,16 +135,16 @@ CopyFromErrorCallback(void *arg)
 			char	   *attval;
 
 			attval = limit_printout_length(cstate->cur_attval);
-			errcontext("COPY %s, line %s, column %s: \"%s\"",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: \"%s\"",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname, attval);
 			pfree(attval);
 		}
 		else if (cstate->cur_attname)
 		{
 			/* error is relevant to a particular column, value is NULL */
-			errcontext("COPY %s, line %s, column %s: null input",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: null input",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		}
 		else
@@ -163,14 +159,14 @@ CopyFromErrorCallback(void *arg)
 				char	   *lineval;
 
 				lineval = limit_printout_length(cstate->line_buf.data);
-				errcontext("COPY %s, line %s: \"%s\"",
-						   cstate->cur_relname, curlineno_str, lineval);
+				errcontext("COPY %s, line %llu: \"%s\"",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno, lineval);
 				pfree(lineval);
 			}
 			else
 			{
-				errcontext("COPY %s, line %s",
-						   cstate->cur_relname, curlineno_str);
+				errcontext("COPY %s, line %llu",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 			}
 		}
 	}
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index ab592ce2f1..8db875c003 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -707,13 +707,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached maximum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached maximum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long int)maxv)));
 				}
 				next = minv;
 			}
@@ -730,13 +727,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached minimum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached minimum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long int)minv)));
 				}
 				next = maxv;
 			}
@@ -976,18 +970,11 @@ do_setval(Oid relid, int64 next, bool iscalled)
 
 	if ((next < minv) || (next > maxv))
 	{
-		char		bufv[100],
-					bufm[100],
-					bufx[100];
-
-		snprintf(bufv, sizeof(bufv), INT64_FORMAT, next);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, minv);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, maxv);
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("setval: value %s is out of bounds for sequence \"%s\" (%s..%s)",
-						bufv, RelationGetRelationName(seqrel),
-						bufm, bufx)));
+				 errmsg("setval: value %lld is out of bounds for sequence \"%s\" (%lld..%lld)",
+						(long long int)next, RelationGetRelationName(seqrel),
+						(long long int)minv, (long long int)maxv)));
 	}
 
 	/* Set the currval() state only if iscalled = true */
@@ -1469,14 +1456,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmax < PG_INT16_MIN || seqform->seqmax > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX)))
 	{
-		char		bufx[100];
-
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MAXVALUE (%s) is out of range for sequence data type %s",
-						bufx, format_type_be(seqform->seqtypid))));
+				 errmsg("MAXVALUE (%lld) is out of range for sequence data type %s",
+						(long long int)seqform->seqmax, format_type_be(seqform->seqtypid))));
 	}
 
 	/* MINVALUE (null arg means NO MINVALUE) */
@@ -1506,28 +1489,19 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmin < PG_INT16_MIN || seqform->seqmin > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX)))
 	{
-		char		bufm[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) is out of range for sequence data type %s",
-						bufm, format_type_be(seqform->seqtypid))));
+				 errmsg("MINVALUE (%lld) is out of range for sequence data type %s",
+						(long long int)seqform->seqmin, format_type_be(seqform->seqtypid))));
 	}
 
 	/* crosscheck min/max */
 	if (seqform->seqmin >= seqform->seqmax)
 	{
-		char		bufm[100],
-					bufx[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) must be less than MAXVALUE (%s)",
-						bufm, bufx)));
+				 errmsg("MINVALUE (%lld) must be less than MAXVALUE (%lld)",
+						(long long int)seqform->seqmin, (long long int)seqform->seqmax)));
 	}
 
 	/* START WITH */
@@ -1546,27 +1520,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck START */
 	if (seqform->seqstart < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long int)seqform->seqstart, (long long int)seqform->seqmin)));
 	}
 	if (seqform->seqstart > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long int)seqform->seqstart, (long long int)seqform->seqmax)));
 	}
 
 	/* RESTART [WITH] */
@@ -1588,27 +1552,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck RESTART (or current value, if changing MIN/MAX) */
 	if (seqdataform->last_value < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long int)seqdataform->last_value, (long long int)seqform->seqmin)));
 	}
 	if (seqdataform->last_value > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long int)seqdataform->last_value, (long long int)seqform->seqmax)));
 	}
 
 	/* CACHE */
@@ -1617,13 +1571,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 		seqform->seqcache = defGetInt64(cache_value);
 		if (seqform->seqcache <= 0)
 		{
-			char		buf[100];
-
-			snprintf(buf, sizeof(buf), INT64_FORMAT, seqform->seqcache);
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("CACHE (%s) must be greater than zero",
-							buf)));
+					 errmsg("CACHE (%lld) must be greater than zero",
+							(long long int)seqform->seqcache)));
 		}
 		seqdataform->log_cnt = 0;
 	}
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index 60e57876de..e321a70138 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -113,9 +113,8 @@ TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
 	if (!FullTransactionIdPrecedes(fxid, now_fullxid))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("transaction ID %s is in the future",
-						psprintf(UINT64_FORMAT,
-								 U64FromFullTransactionId(fxid)))));
+				 errmsg("transaction ID %llu is in the future",
+						(unsigned long long)U64FromFullTransactionId(fxid))));
 
 	/*
 	 * ShmemVariableCache->oldestClogXid is protected by XactTruncationLock,
-- 
2.35.1

#2Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Aleksander Alekseev (#1)
Re: [PATCH] Remove workarounds to format [u]int64's

пн, 21 мар. 2022 г. в 12:52, Aleksander Alekseev <aleksander@timescale.com>:

Hi hackers,

I learned from Tom [1] that we can simplify the code like:

```
char buff[32];
snprintf(buf, sizeof(buf), INT64_FORMAT, ...)
ereport(WARNING, (errmsg("%s ...", buf)));
```

... and rely on %lld/%llu now as long as we explicitly cast the
argument to long long int / unsigned long long. This was previously
addressed in 6a1cd8b9 and d914eb34, but I see more places where we
still use an old approach.

Suggested patch fixes this. Tested locally - no warnings; passes all the
tests.

[1]
/messages/by-id/771048.1647528068@sss.pgh.pa.us

Hi, Alexander!

Probably you can do (long long) instead of (long long int). It is shorter
and this is used elsewhere in the code.

--
Best regards,
Pavel Borisov

Postgres Professional: http://postgrespro.com <http://www.postgrespro.com&gt;

#3Aleksander Alekseev
aleksander@timescale.com
In reply to: Pavel Borisov (#2)
1 attachment(s)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Pavel,

Probably you can do (long long) instead of (long long int). It is shorter and this is used elsewhere in the code.

Thanks! Here is the updated patch. I also added Reviewed-by: and
Discussion: to the commit message.

--
Best regards,
Aleksander Alekseev

Attachments:

v2-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v2-0001-Remove-workarounds-to-format-u-int64-s.patchDownload
From 28aca9bf0d8724b03af3d726242b31bb4b1c4010 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Mon, 21 Mar 2022 11:01:57 +0300
Subject: [PATCH v2] Remove workarounds to format [u]int64's

Now that we always use our own sprintf(), we can rely on %lld and %llu to be
portable, so we can use those. This reduces the code verbosity.

Author: Aleksander Alekseev <aleksander@timescale.com>
Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com>
Discussion: https://postgr.es/m/CAJ7c6TMSKi3Xs8h5MP38XOnQQpBLazJvVxVfPn%2B%2BroitDJcR7g%40mail.gmail.com
---
 src/backend/access/brin/brin.c    |  8 +--
 src/backend/commands/copyfrom.c   | 28 ++++-----
 src/backend/commands/sequence.c   | 95 ++++++++-----------------------
 src/backend/utils/adt/xid8funcs.c |  5 +-
 4 files changed, 39 insertions(+), 97 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index ba78ecff66..07c7c5a25d 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -1018,11 +1018,9 @@ brin_summarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
@@ -1095,11 +1093,9 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 7b3f5a84b8..bab40c4201 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -115,21 +115,17 @@ void
 CopyFromErrorCallback(void *arg)
 {
 	CopyFromState cstate = (CopyFromState) arg;
-	char		curlineno_str[32];
-
-	snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
-			 cstate->cur_lineno);
 
 	if (cstate->opts.binary)
 	{
 		/* can't usefully display the data */
 		if (cstate->cur_attname)
-			errcontext("COPY %s, line %s, column %s",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		else
-			errcontext("COPY %s, line %s",
-					   cstate->cur_relname, curlineno_str);
+			errcontext("COPY %s, line %llu",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 	}
 	else
 	{
@@ -139,16 +135,16 @@ CopyFromErrorCallback(void *arg)
 			char	   *attval;
 
 			attval = limit_printout_length(cstate->cur_attval);
-			errcontext("COPY %s, line %s, column %s: \"%s\"",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: \"%s\"",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname, attval);
 			pfree(attval);
 		}
 		else if (cstate->cur_attname)
 		{
 			/* error is relevant to a particular column, value is NULL */
-			errcontext("COPY %s, line %s, column %s: null input",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: null input",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		}
 		else
@@ -163,14 +159,14 @@ CopyFromErrorCallback(void *arg)
 				char	   *lineval;
 
 				lineval = limit_printout_length(cstate->line_buf.data);
-				errcontext("COPY %s, line %s: \"%s\"",
-						   cstate->cur_relname, curlineno_str, lineval);
+				errcontext("COPY %s, line %llu: \"%s\"",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno, lineval);
 				pfree(lineval);
 			}
 			else
 			{
-				errcontext("COPY %s, line %s",
-						   cstate->cur_relname, curlineno_str);
+				errcontext("COPY %s, line %llu",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 			}
 		}
 	}
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index ab592ce2f1..992de3a9d3 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -707,13 +707,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached maximum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached maximum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long)maxv)));
 				}
 				next = minv;
 			}
@@ -730,13 +727,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached minimum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached minimum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long)minv)));
 				}
 				next = maxv;
 			}
@@ -976,18 +970,11 @@ do_setval(Oid relid, int64 next, bool iscalled)
 
 	if ((next < minv) || (next > maxv))
 	{
-		char		bufv[100],
-					bufm[100],
-					bufx[100];
-
-		snprintf(bufv, sizeof(bufv), INT64_FORMAT, next);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, minv);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, maxv);
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("setval: value %s is out of bounds for sequence \"%s\" (%s..%s)",
-						bufv, RelationGetRelationName(seqrel),
-						bufm, bufx)));
+				 errmsg("setval: value %lld is out of bounds for sequence \"%s\" (%lld..%lld)",
+						(long long)next, RelationGetRelationName(seqrel),
+						(long long)minv, (long long)maxv)));
 	}
 
 	/* Set the currval() state only if iscalled = true */
@@ -1469,14 +1456,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmax < PG_INT16_MIN || seqform->seqmax > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX)))
 	{
-		char		bufx[100];
-
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MAXVALUE (%s) is out of range for sequence data type %s",
-						bufx, format_type_be(seqform->seqtypid))));
+				 errmsg("MAXVALUE (%lld) is out of range for sequence data type %s",
+						(long long)seqform->seqmax, format_type_be(seqform->seqtypid))));
 	}
 
 	/* MINVALUE (null arg means NO MINVALUE) */
@@ -1506,28 +1489,19 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmin < PG_INT16_MIN || seqform->seqmin > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX)))
 	{
-		char		bufm[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) is out of range for sequence data type %s",
-						bufm, format_type_be(seqform->seqtypid))));
+				 errmsg("MINVALUE (%lld) is out of range for sequence data type %s",
+						(long long)seqform->seqmin, format_type_be(seqform->seqtypid))));
 	}
 
 	/* crosscheck min/max */
 	if (seqform->seqmin >= seqform->seqmax)
 	{
-		char		bufm[100],
-					bufx[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) must be less than MAXVALUE (%s)",
-						bufm, bufx)));
+				 errmsg("MINVALUE (%lld) must be less than MAXVALUE (%lld)",
+						(long long)seqform->seqmin, (long long)seqform->seqmax)));
 	}
 
 	/* START WITH */
@@ -1546,27 +1520,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck START */
 	if (seqform->seqstart < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long)seqform->seqstart, (long long)seqform->seqmin)));
 	}
 	if (seqform->seqstart > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long)seqform->seqstart, (long long)seqform->seqmax)));
 	}
 
 	/* RESTART [WITH] */
@@ -1588,27 +1552,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck RESTART (or current value, if changing MIN/MAX) */
 	if (seqdataform->last_value < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long)seqdataform->last_value, (long long)seqform->seqmin)));
 	}
 	if (seqdataform->last_value > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long)seqdataform->last_value, (long long)seqform->seqmax)));
 	}
 
 	/* CACHE */
@@ -1617,13 +1571,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 		seqform->seqcache = defGetInt64(cache_value);
 		if (seqform->seqcache <= 0)
 		{
-			char		buf[100];
-
-			snprintf(buf, sizeof(buf), INT64_FORMAT, seqform->seqcache);
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("CACHE (%s) must be greater than zero",
-							buf)));
+					 errmsg("CACHE (%lld) must be greater than zero",
+							(long long)seqform->seqcache)));
 		}
 		seqdataform->log_cnt = 0;
 	}
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index 60e57876de..e321a70138 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -113,9 +113,8 @@ TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
 	if (!FullTransactionIdPrecedes(fxid, now_fullxid))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("transaction ID %s is in the future",
-						psprintf(UINT64_FORMAT,
-								 U64FromFullTransactionId(fxid)))));
+				 errmsg("transaction ID %llu is in the future",
+						(unsigned long long)U64FromFullTransactionId(fxid))));
 
 	/*
 	 * ShmemVariableCache->oldestClogXid is protected by XactTruncationLock,
-- 
2.35.1

#4Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Aleksander Alekseev (#3)
Re: [PATCH] Remove workarounds to format [u]int64's

Probably you can do (long long) instead of (long long int). It is

shorter and this is used elsewhere in the code.

Thanks! Here is the updated patch. I also added Reviewed-by: and
Discussion: to the commit message.

Thanks, Alexander!
I suggest the patch is in a good shape to be committed.
(
Maybe some strings that don't fit screen cloud be reflowed:
(long long int)seqdataform->last_value, (long long int)seqform->seqmax)));
)

--
Best regards,
Pavel Borisov

Postgres Professional: http://postgrespro.com <http://www.postgrespro.com&gt;

#5Japin Li
japinli@hotmail.com
In reply to: Aleksander Alekseev (#3)
1 attachment(s)
Re: [PATCH] Remove workarounds to format [u]int64's

On Mon, 21 Mar 2022 at 17:23, Aleksander Alekseev <aleksander@timescale.com> wrote:

Hi Pavel,

Probably you can do (long long) instead of (long long int). It is shorter and this is used elsewhere in the code.

Thanks! Here is the updated patch. I also added Reviewed-by: and
Discussion: to the commit message.

Hi,

After apply the patch, I found pg_checksums.c also has the similar code.

In progress_report(), I'm not sure we can do this replace for this code.

snprintf(total_size_str, sizeof(total_size_str), INT64_FORMAT,
total_size / (1024 * 1024));
snprintf(current_size_str, sizeof(current_size_str), INT64_FORMAT,
current_size / (1024 * 1024));

fprintf(stderr, _("%*s/%s MB (%d%%) computed"),
(int) strlen(current_size_str), current_size_str, total_size_str,
percent);

--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.

Attachments:

remove-workarounds-int64-format-in-pg_checksums.patchtext/x-patchDownload
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 7e69475947..e45e436683 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -657,11 +657,11 @@ main(int argc, char *argv[])
 			progress_report(true);
 
 		printf(_("Checksum operation completed\n"));
-		printf(_("Files scanned:   %s\n"), psprintf(INT64_FORMAT, files_scanned));
-		printf(_("Blocks scanned:  %s\n"), psprintf(INT64_FORMAT, blocks_scanned));
+		printf(_("Files scanned:   %lld\n"), files_scanned);
+		printf(_("Blocks scanned:  %lld\n"), blocks_scanned);
 		if (mode == PG_MODE_CHECK)
 		{
-			printf(_("Bad checksums:  %s\n"), psprintf(INT64_FORMAT, badblocks));
+			printf(_("Bad checksums:  %lld\n"), badblocks);
 			printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
 
 			if (badblocks > 0)
@@ -669,8 +669,8 @@ main(int argc, char *argv[])
 		}
 		else if (mode == PG_MODE_ENABLE)
 		{
-			printf(_("Files written:  %s\n"), psprintf(INT64_FORMAT, files_written));
-			printf(_("Blocks written: %s\n"), psprintf(INT64_FORMAT, blocks_written));
+			printf(_("Files written:  %lld\n"), files_written);
+			printf(_("Blocks written: %lld\n"), blocks_written);
 		}
 	}
 
#6Aleksander Alekseev
aleksander@timescale.com
In reply to: Japin Li (#5)
2 attachment(s)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Japin,

After apply the patch, I found pg_checksums.c also has the similar code.

Thanks for noticing it.

In progress_report(), I'm not sure we can do this replace for this code.

I added the corresponding change as a separate commit so it can be
easily reverted if necessary.

Here is a complete patchset with some additional changes by me.

--
Best regards,
Aleksander Alekseev

Attachments:

v3-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v3-0001-Remove-workarounds-to-format-u-int64-s.patchDownload
From 28aca9bf0d8724b03af3d726242b31bb4b1c4010 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Mon, 21 Mar 2022 11:01:57 +0300
Subject: [PATCH v3 1/2] Remove workarounds to format [u]int64's

Now that we always use our own sprintf(), we can rely on %lld and %llu to be
portable, so we can use those. This reduces the code verbosity.

Author: Aleksander Alekseev <aleksander@timescale.com>
Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com>
Discussion: https://postgr.es/m/CAJ7c6TMSKi3Xs8h5MP38XOnQQpBLazJvVxVfPn%2B%2BroitDJcR7g%40mail.gmail.com
---
 src/backend/access/brin/brin.c    |  8 +--
 src/backend/commands/copyfrom.c   | 28 ++++-----
 src/backend/commands/sequence.c   | 95 ++++++++-----------------------
 src/backend/utils/adt/xid8funcs.c |  5 +-
 4 files changed, 39 insertions(+), 97 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index ba78ecff66..07c7c5a25d 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -1018,11 +1018,9 @@ brin_summarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
@@ -1095,11 +1093,9 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 7b3f5a84b8..bab40c4201 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -115,21 +115,17 @@ void
 CopyFromErrorCallback(void *arg)
 {
 	CopyFromState cstate = (CopyFromState) arg;
-	char		curlineno_str[32];
-
-	snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
-			 cstate->cur_lineno);
 
 	if (cstate->opts.binary)
 	{
 		/* can't usefully display the data */
 		if (cstate->cur_attname)
-			errcontext("COPY %s, line %s, column %s",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		else
-			errcontext("COPY %s, line %s",
-					   cstate->cur_relname, curlineno_str);
+			errcontext("COPY %s, line %llu",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 	}
 	else
 	{
@@ -139,16 +135,16 @@ CopyFromErrorCallback(void *arg)
 			char	   *attval;
 
 			attval = limit_printout_length(cstate->cur_attval);
-			errcontext("COPY %s, line %s, column %s: \"%s\"",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: \"%s\"",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname, attval);
 			pfree(attval);
 		}
 		else if (cstate->cur_attname)
 		{
 			/* error is relevant to a particular column, value is NULL */
-			errcontext("COPY %s, line %s, column %s: null input",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: null input",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		}
 		else
@@ -163,14 +159,14 @@ CopyFromErrorCallback(void *arg)
 				char	   *lineval;
 
 				lineval = limit_printout_length(cstate->line_buf.data);
-				errcontext("COPY %s, line %s: \"%s\"",
-						   cstate->cur_relname, curlineno_str, lineval);
+				errcontext("COPY %s, line %llu: \"%s\"",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno, lineval);
 				pfree(lineval);
 			}
 			else
 			{
-				errcontext("COPY %s, line %s",
-						   cstate->cur_relname, curlineno_str);
+				errcontext("COPY %s, line %llu",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 			}
 		}
 	}
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index ab592ce2f1..992de3a9d3 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -707,13 +707,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached maximum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached maximum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long)maxv)));
 				}
 				next = minv;
 			}
@@ -730,13 +727,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached minimum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached minimum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long)minv)));
 				}
 				next = maxv;
 			}
@@ -976,18 +970,11 @@ do_setval(Oid relid, int64 next, bool iscalled)
 
 	if ((next < minv) || (next > maxv))
 	{
-		char		bufv[100],
-					bufm[100],
-					bufx[100];
-
-		snprintf(bufv, sizeof(bufv), INT64_FORMAT, next);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, minv);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, maxv);
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("setval: value %s is out of bounds for sequence \"%s\" (%s..%s)",
-						bufv, RelationGetRelationName(seqrel),
-						bufm, bufx)));
+				 errmsg("setval: value %lld is out of bounds for sequence \"%s\" (%lld..%lld)",
+						(long long)next, RelationGetRelationName(seqrel),
+						(long long)minv, (long long)maxv)));
 	}
 
 	/* Set the currval() state only if iscalled = true */
@@ -1469,14 +1456,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmax < PG_INT16_MIN || seqform->seqmax > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX)))
 	{
-		char		bufx[100];
-
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MAXVALUE (%s) is out of range for sequence data type %s",
-						bufx, format_type_be(seqform->seqtypid))));
+				 errmsg("MAXVALUE (%lld) is out of range for sequence data type %s",
+						(long long)seqform->seqmax, format_type_be(seqform->seqtypid))));
 	}
 
 	/* MINVALUE (null arg means NO MINVALUE) */
@@ -1506,28 +1489,19 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmin < PG_INT16_MIN || seqform->seqmin > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX)))
 	{
-		char		bufm[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) is out of range for sequence data type %s",
-						bufm, format_type_be(seqform->seqtypid))));
+				 errmsg("MINVALUE (%lld) is out of range for sequence data type %s",
+						(long long)seqform->seqmin, format_type_be(seqform->seqtypid))));
 	}
 
 	/* crosscheck min/max */
 	if (seqform->seqmin >= seqform->seqmax)
 	{
-		char		bufm[100],
-					bufx[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) must be less than MAXVALUE (%s)",
-						bufm, bufx)));
+				 errmsg("MINVALUE (%lld) must be less than MAXVALUE (%lld)",
+						(long long)seqform->seqmin, (long long)seqform->seqmax)));
 	}
 
 	/* START WITH */
@@ -1546,27 +1520,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck START */
 	if (seqform->seqstart < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long)seqform->seqstart, (long long)seqform->seqmin)));
 	}
 	if (seqform->seqstart > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long)seqform->seqstart, (long long)seqform->seqmax)));
 	}
 
 	/* RESTART [WITH] */
@@ -1588,27 +1552,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck RESTART (or current value, if changing MIN/MAX) */
 	if (seqdataform->last_value < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long)seqdataform->last_value, (long long)seqform->seqmin)));
 	}
 	if (seqdataform->last_value > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long)seqdataform->last_value, (long long)seqform->seqmax)));
 	}
 
 	/* CACHE */
@@ -1617,13 +1571,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 		seqform->seqcache = defGetInt64(cache_value);
 		if (seqform->seqcache <= 0)
 		{
-			char		buf[100];
-
-			snprintf(buf, sizeof(buf), INT64_FORMAT, seqform->seqcache);
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("CACHE (%s) must be greater than zero",
-							buf)));
+					 errmsg("CACHE (%lld) must be greater than zero",
+							(long long)seqform->seqcache)));
 		}
 		seqdataform->log_cnt = 0;
 	}
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index 60e57876de..e321a70138 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -113,9 +113,8 @@ TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
 	if (!FullTransactionIdPrecedes(fxid, now_fullxid))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("transaction ID %s is in the future",
-						psprintf(UINT64_FORMAT,
-								 U64FromFullTransactionId(fxid)))));
+				 errmsg("transaction ID %llu is in the future",
+						(unsigned long long)U64FromFullTransactionId(fxid))));
 
 	/*
 	 * ShmemVariableCache->oldestClogXid is protected by XactTruncationLock,
-- 
2.35.1

v3-0002-Remove-workarounds-to-format-int64-s-in-pg_checks.patchapplication/octet-stream; name=v3-0002-Remove-workarounds-to-format-int64-s-in-pg_checks.patchDownload
From 29278ad33a5c6861ba6b955c411b670ce3a7f493 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Mon, 21 Mar 2022 13:37:35 +0300
Subject: [PATCH v3 2/2] Remove workarounds to format int64's in pg_checksums.c

Now we can rely on %lld and %llu to be portable, so we can use those in order
to reduce code verbosity. The patch also modifies the corresponding .po files.

Author: Aleksander Alekseev <aleksander@timescale.com>
Author:	Japin Li <japinli@hotmail.com>
Reviewed-by: Aleksander Alekseev, Japin Li
Discussion: https://postgr.es/m/CAJ7c6TMSKi3Xs8h5MP38XOnQQpBLazJvVxVfPn%2B%2BroitDJcR7g%40mail.gmail.com
---
 src/bin/pg_checksums/pg_checksums.c | 27 ++++++++-------------------
 src/bin/pg_checksums/po/cs.po       |  8 ++++----
 src/bin/pg_checksums/po/de.po       |  8 ++++----
 src/bin/pg_checksums/po/el.po       |  8 ++++----
 src/bin/pg_checksums/po/es.po       |  8 ++++----
 src/bin/pg_checksums/po/fr.po       |  8 ++++----
 src/bin/pg_checksums/po/ja.po       |  8 ++++----
 src/bin/pg_checksums/po/ko.po       |  8 ++++----
 src/bin/pg_checksums/po/ru.po       |  8 ++++----
 src/bin/pg_checksums/po/sv.po       |  8 ++++----
 src/bin/pg_checksums/po/tr.po       |  8 ++++----
 src/bin/pg_checksums/po/uk.po       |  8 ++++----
 src/bin/pg_checksums/po/zh_CN.po    |  6 +++---
 13 files changed, 55 insertions(+), 66 deletions(-)

diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 7e69475947..8509bd15e8 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -132,8 +132,6 @@ static void
 progress_report(bool finished)
 {
 	int			percent;
-	char		total_size_str[32];
-	char		current_size_str[32];
 	pg_time_t	now;
 
 	Assert(showprogress);
@@ -152,18 +150,9 @@ progress_report(bool finished)
 	/* Calculate current percentage of size done */
 	percent = total_size ? (int) ((current_size) * 100 / total_size) : 0;
 
-	/*
-	 * Separate step to keep platform-dependent format code out of
-	 * translatable strings.  And we only test for INT64_FORMAT availability
-	 * in snprintf, not fprintf.
-	 */
-	snprintf(total_size_str, sizeof(total_size_str), INT64_FORMAT,
-			 total_size / (1024 * 1024));
-	snprintf(current_size_str, sizeof(current_size_str), INT64_FORMAT,
-			 current_size / (1024 * 1024));
-
-	fprintf(stderr, _("%*s/%s MB (%d%%) computed"),
-			(int) strlen(current_size_str), current_size_str, total_size_str,
+	fprintf(stderr, _("%lld/%lld MB (%d%%) computed"),
+			(long long)(current_size / (1024 * 1024)),
+			(long long)(total_size / (1024 * 1024)),
 			percent);
 
 	/*
@@ -657,11 +646,11 @@ main(int argc, char *argv[])
 			progress_report(true);
 
 		printf(_("Checksum operation completed\n"));
-		printf(_("Files scanned:   %s\n"), psprintf(INT64_FORMAT, files_scanned));
-		printf(_("Blocks scanned:  %s\n"), psprintf(INT64_FORMAT, blocks_scanned));
+		printf(_("Files scanned:   %lld\n"), (long long)files_scanned);
+		printf(_("Blocks scanned:  %lld\n"), (long long)blocks_scanned);
 		if (mode == PG_MODE_CHECK)
 		{
-			printf(_("Bad checksums:  %s\n"), psprintf(INT64_FORMAT, badblocks));
+			printf(_("Bad checksums:  %lld\n"), (long long)badblocks);
 			printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
 
 			if (badblocks > 0)
@@ -669,8 +658,8 @@ main(int argc, char *argv[])
 		}
 		else if (mode == PG_MODE_ENABLE)
 		{
-			printf(_("Files written:  %s\n"), psprintf(INT64_FORMAT, files_written));
-			printf(_("Blocks written: %s\n"), psprintf(INT64_FORMAT, blocks_written));
+			printf(_("Files written:  %lld\n"), (long long)files_written);
+			printf(_("Blocks written: %lld\n"), (long long)blocks_written);
 		}
 	}
 
diff --git a/src/bin/pg_checksums/po/cs.po b/src/bin/pg_checksums/po/cs.po
index df56be82bb..071bdc17a5 100644
--- a/src/bin/pg_checksums/po/cs.po
+++ b/src/bin/pg_checksums/po/cs.po
@@ -135,8 +135,8 @@ msgstr "%s domácí stránka: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) zpracováno"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) zpracováno"
 
 #: pg_checksums.c:207
 #, c-format
@@ -280,8 +280,8 @@ msgstr "Přečtené datové bloky: %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Chybné kontrolní součty:  %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Chybné kontrolní součty:  %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/de.po b/src/bin/pg_checksums/po/de.po
index d30fb2bd55..2b876c722c 100644
--- a/src/bin/pg_checksums/po/de.po
+++ b/src/bin/pg_checksums/po/de.po
@@ -136,8 +136,8 @@ msgstr "%s Homepage: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) berechnet"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%ldd/%lld MB (%d%%) berechnet"
 
 #: pg_checksums.c:207
 #, c-format
@@ -281,8 +281,8 @@ msgstr "Überprüfte Blöcke:      %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Falsche Prüfsummen:     %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Falsche Prüfsummen:     %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/el.po b/src/bin/pg_checksums/po/el.po
index cb72eabcbe..994abe40e0 100644
--- a/src/bin/pg_checksums/po/el.po
+++ b/src/bin/pg_checksums/po/el.po
@@ -135,8 +135,8 @@ msgstr "%s αρχική σελίδα: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) Υπολογίζεται"
+msgid "%lld/%ldd MB (%d%%) computed"
+msgstr "%ldd/%lld MB (%d%%) Υπολογίζεται"
 
 #: pg_checksums.c:207
 #, c-format
@@ -280,8 +280,8 @@ msgstr "Μπλοκ που σαρώθηκαν: %s\n"
 
 #: pg_checksums.c:644
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Εσφαλμένα αθροίσματα ελέγχου: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Εσφαλμένα αθροίσματα ελέγχου: %lld\n"
 
 #: pg_checksums.c:645 pg_checksums.c:672
 #, c-format
diff --git a/src/bin/pg_checksums/po/es.po b/src/bin/pg_checksums/po/es.po
index 3b7e35c8cd..4f3efe76eb 100644
--- a/src/bin/pg_checksums/po/es.po
+++ b/src/bin/pg_checksums/po/es.po
@@ -138,8 +138,8 @@ msgstr "Sitio web de %s: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) calculado"
+msgid "%ldd/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) calculado"
 
 #: pg_checksums.c:207
 #, c-format
@@ -283,8 +283,8 @@ msgstr "Bloques recorridos:    %s\n"
 
 #: pg_checksums.c:644
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Checksums incorrectos: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Checksums incorrectos: %lld\n"
 
 #: pg_checksums.c:645 pg_checksums.c:672
 #, c-format
diff --git a/src/bin/pg_checksums/po/fr.po b/src/bin/pg_checksums/po/fr.po
index 40633c5166..02523287fb 100644
--- a/src/bin/pg_checksums/po/fr.po
+++ b/src/bin/pg_checksums/po/fr.po
@@ -135,8 +135,8 @@ msgstr "page d'accueil de %s : <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s Mo (%d%%) traités"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%ldd Mo (%d%%) traités"
 
 #: pg_checksums.c:207
 #, c-format
@@ -280,8 +280,8 @@ msgstr "Blocs parcourus : %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Mauvaises sommes de contrôle : %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Mauvaises sommes de contrôle : %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/ja.po b/src/bin/pg_checksums/po/ja.po
index f43465a0e4..f4a1820438 100644
--- a/src/bin/pg_checksums/po/ja.po
+++ b/src/bin/pg_checksums/po/ja.po
@@ -133,8 +133,8 @@ msgstr "%s ホームページ: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) 完了"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) 完了"
 
 #: pg_checksums.c:207
 #, c-format
@@ -278,8 +278,8 @@ msgstr "スキャンしたブロック数: %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "不正なチェックサム数:  %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "不正なチェックサム数:  %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/ko.po b/src/bin/pg_checksums/po/ko.po
index ff767cfc7d..a8edd966e5 100644
--- a/src/bin/pg_checksums/po/ko.po
+++ b/src/bin/pg_checksums/po/ko.po
@@ -141,8 +141,8 @@ msgstr "%s 홈페이지: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) 계산됨"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) 계산됨"
 
 #: pg_checksums.c:207
 #, c-format
@@ -294,8 +294,8 @@ msgstr "조사한 블럭수: %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "잘못된 체크섬: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "잘못된 체크섬: %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/ru.po b/src/bin/pg_checksums/po/ru.po
index 8e48580af0..a533279843 100644
--- a/src/bin/pg_checksums/po/ru.po
+++ b/src/bin/pg_checksums/po/ru.po
@@ -141,8 +141,8 @@ msgstr "Домашняя страница %s: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s МБ (%d%%) обработано"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld МБ (%d%%) обработано"
 
 #: pg_checksums.c:207
 #, c-format
@@ -294,8 +294,8 @@ msgstr "Просканировано блоков: %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Неверные контрольные суммы: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Неверные контрольные суммы: %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/sv.po b/src/bin/pg_checksums/po/sv.po
index ce92a966e3..8b2f422531 100644
--- a/src/bin/pg_checksums/po/sv.po
+++ b/src/bin/pg_checksums/po/sv.po
@@ -135,8 +135,8 @@ msgstr "hemsida för %s: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) beräknad"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) beräknad"
 
 #: pg_checksums.c:204
 #, c-format
@@ -280,8 +280,8 @@ msgstr "Skannade block:           %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Felaktiga kontrollsummor: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Felaktiga kontrollsummor: %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/tr.po b/src/bin/pg_checksums/po/tr.po
index 3bd40db784..b07e517aa3 100644
--- a/src/bin/pg_checksums/po/tr.po
+++ b/src/bin/pg_checksums/po/tr.po
@@ -131,8 +131,8 @@ msgstr "Hataları <pgsql-bugs@lists.postgresql.org> adresine bildirebilirsiniz.\
 
 #: pg_checksums.c:149
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) hesaplandı"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) hesaplandı"
 
 #: pg_checksums.c:186
 #, c-format
@@ -266,8 +266,8 @@ msgstr "Taranan bloklar: %s\n"
 
 #: pg_checksums.c:563
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Yanlış sağlama toplamları: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Yanlış sağlama toplamları: %lld\n"
 
 #: pg_checksums.c:564 pg_checksums.c:591
 #, c-format
diff --git a/src/bin/pg_checksums/po/uk.po b/src/bin/pg_checksums/po/uk.po
index 15bfe6a47c..f005db85a9 100644
--- a/src/bin/pg_checksums/po/uk.po
+++ b/src/bin/pg_checksums/po/uk.po
@@ -124,8 +124,8 @@ msgstr "Домашня сторінка %s: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "%*s/%s MB (%d%%) обчислено"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "%lld/%lld MB (%d%%) обчислено"
 
 #: pg_checksums.c:207
 #, c-format
@@ -269,8 +269,8 @@ msgstr "Блоків відскановано: %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
-msgstr "Неправильні контрольні суми: %s\n"
+msgid "Bad checksums:  %lld\n"
+msgstr "Неправильні контрольні суми: %lld\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
 #, c-format
diff --git a/src/bin/pg_checksums/po/zh_CN.po b/src/bin/pg_checksums/po/zh_CN.po
index 012872b87a..5c621fc013 100644
--- a/src/bin/pg_checksums/po/zh_CN.po
+++ b/src/bin/pg_checksums/po/zh_CN.po
@@ -134,8 +134,8 @@ msgstr "%s 主页: <%s>\n"
 
 #: pg_checksums.c:161
 #, c-format
-msgid "%*s/%s MB (%d%%) computed"
-msgstr "已计算%*s/%s MB (%d%%)"
+msgid "%lld/%lld MB (%d%%) computed"
+msgstr "已计算%lld/%lld MB (%d%%)"
 
 #: pg_checksums.c:204
 #, c-format
@@ -279,7 +279,7 @@ msgstr "扫描的块: %s\n"
 
 #: pg_checksums.c:637
 #, c-format
-msgid "Bad checksums:  %s\n"
+msgid "Bad checksums:  %lld\n"
 msgstr "坏校验和:  %s\n"
 
 #: pg_checksums.c:638 pg_checksums.c:665
-- 
2.35.1

#7Aleksander Alekseev
aleksander@timescale.com
In reply to: Aleksander Alekseev (#1)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Japin,

As Tom said in [1], we don't need to touch the *.po files, since those

files

are managed by the translation team.

[1]

/messages/by-id/1110708.1647623560@sss.pgh.pa.us

True, but I figured that simplifying the work of the translation team would
not harm either. In any case, the committer can easily exclude these
changes from the patch, if necessary.

--
Best regards,
Aleksander Alekseev

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Aleksander Alekseev (#7)
Re: [PATCH] Remove workarounds to format [u]int64's

Aleksander Alekseev <aleksander@timescale.com> writes:

As Tom said in [1], we don't need to touch the *.po files, since those
files are managed by the translation team.

True, but I figured that simplifying the work of the translation team would
not harm either.

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

https://www.postgresql.org/docs/devel/nls.html

https://wiki.postgresql.org/wiki/NLS

regards, tom lane

#9Aleksander Alekseev
aleksander@timescale.com
In reply to: Tom Lane (#8)
1 attachment(s)
Re: [PATCH] Remove workarounds to format [u]int64's

Hi Tom,

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

--
Best regards,
Aleksander Alekseev

Attachments:

v4-0001-Remove-workarounds-to-format-u-int64-s.patchapplication/octet-stream; name=v4-0001-Remove-workarounds-to-format-u-int64-s.patchDownload
From 2a77c3120a5edc32cdb7d716eed051bf2ff86c0e Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@timescale.com>
Date: Mon, 21 Mar 2022 11:01:57 +0300
Subject: [PATCH v4] Remove workarounds to format [u]int64's

Now that we always use our own sprintf(), we can rely on %lld and %llu to be
portable, so we can use those. This reduces the code verbosity.

Author: Aleksander Alekseev <aleksander@timescale.com>
Author:	Japin Li <japinli@hotmail.com>
Reviewed-by: Pavel Borisov <pashkin.elfe@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CAJ7c6TMSKi3Xs8h5MP38XOnQQpBLazJvVxVfPn%2B%2BroitDJcR7g%40mail.gmail.com
---
 src/backend/access/brin/brin.c      |  8 +--
 src/backend/commands/copyfrom.c     | 28 ++++-----
 src/backend/commands/sequence.c     | 95 +++++++----------------------
 src/backend/utils/adt/xid8funcs.c   |  5 +-
 src/bin/pg_checksums/pg_checksums.c | 27 +++-----
 5 files changed, 47 insertions(+), 116 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index ba78ecff66..07c7c5a25d 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -1018,11 +1018,9 @@ brin_summarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > BRIN_ALL_BLOCKRANGES || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
@@ -1095,11 +1093,9 @@ brin_desummarize_range(PG_FUNCTION_ARGS)
 
 	if (heapBlk64 > MaxBlockNumber || heapBlk64 < 0)
 	{
-		char	   *blk = psprintf(INT64_FORMAT, heapBlk64);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("block number out of range: %s", blk)));
+				 errmsg("block number out of range: %lld", (long long)heapBlk64)));
 	}
 	heapBlk = (BlockNumber) heapBlk64;
 
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 7b3f5a84b8..bab40c4201 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -115,21 +115,17 @@ void
 CopyFromErrorCallback(void *arg)
 {
 	CopyFromState cstate = (CopyFromState) arg;
-	char		curlineno_str[32];
-
-	snprintf(curlineno_str, sizeof(curlineno_str), UINT64_FORMAT,
-			 cstate->cur_lineno);
 
 	if (cstate->opts.binary)
 	{
 		/* can't usefully display the data */
 		if (cstate->cur_attname)
-			errcontext("COPY %s, line %s, column %s",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		else
-			errcontext("COPY %s, line %s",
-					   cstate->cur_relname, curlineno_str);
+			errcontext("COPY %s, line %llu",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 	}
 	else
 	{
@@ -139,16 +135,16 @@ CopyFromErrorCallback(void *arg)
 			char	   *attval;
 
 			attval = limit_printout_length(cstate->cur_attval);
-			errcontext("COPY %s, line %s, column %s: \"%s\"",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: \"%s\"",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname, attval);
 			pfree(attval);
 		}
 		else if (cstate->cur_attname)
 		{
 			/* error is relevant to a particular column, value is NULL */
-			errcontext("COPY %s, line %s, column %s: null input",
-					   cstate->cur_relname, curlineno_str,
+			errcontext("COPY %s, line %llu, column %s: null input",
+					   cstate->cur_relname, (unsigned long long)cstate->cur_lineno,
 					   cstate->cur_attname);
 		}
 		else
@@ -163,14 +159,14 @@ CopyFromErrorCallback(void *arg)
 				char	   *lineval;
 
 				lineval = limit_printout_length(cstate->line_buf.data);
-				errcontext("COPY %s, line %s: \"%s\"",
-						   cstate->cur_relname, curlineno_str, lineval);
+				errcontext("COPY %s, line %llu: \"%s\"",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno, lineval);
 				pfree(lineval);
 			}
 			else
 			{
-				errcontext("COPY %s, line %s",
-						   cstate->cur_relname, curlineno_str);
+				errcontext("COPY %s, line %llu",
+						   cstate->cur_relname, (unsigned long long)cstate->cur_lineno);
 			}
 		}
 	}
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index ab592ce2f1..992de3a9d3 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -707,13 +707,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, maxv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached maximum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached maximum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long)maxv)));
 				}
 				next = minv;
 			}
@@ -730,13 +727,10 @@ nextval_internal(Oid relid, bool check_permissions)
 					break;		/* stop fetching */
 				if (!cycle)
 				{
-					char		buf[100];
-
-					snprintf(buf, sizeof(buf), INT64_FORMAT, minv);
 					ereport(ERROR,
 							(errcode(ERRCODE_SEQUENCE_GENERATOR_LIMIT_EXCEEDED),
-							 errmsg("nextval: reached minimum value of sequence \"%s\" (%s)",
-									RelationGetRelationName(seqrel), buf)));
+							 errmsg("nextval: reached minimum value of sequence \"%s\" (%lld)",
+									RelationGetRelationName(seqrel), (long long)minv)));
 				}
 				next = maxv;
 			}
@@ -976,18 +970,11 @@ do_setval(Oid relid, int64 next, bool iscalled)
 
 	if ((next < minv) || (next > maxv))
 	{
-		char		bufv[100],
-					bufm[100],
-					bufx[100];
-
-		snprintf(bufv, sizeof(bufv), INT64_FORMAT, next);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, minv);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, maxv);
 		ereport(ERROR,
 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-				 errmsg("setval: value %s is out of bounds for sequence \"%s\" (%s..%s)",
-						bufv, RelationGetRelationName(seqrel),
-						bufm, bufx)));
+				 errmsg("setval: value %lld is out of bounds for sequence \"%s\" (%lld..%lld)",
+						(long long)next, RelationGetRelationName(seqrel),
+						(long long)minv, (long long)maxv)));
 	}
 
 	/* Set the currval() state only if iscalled = true */
@@ -1469,14 +1456,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmax < PG_INT16_MIN || seqform->seqmax > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmax < PG_INT32_MIN || seqform->seqmax > PG_INT32_MAX)))
 	{
-		char		bufx[100];
-
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MAXVALUE (%s) is out of range for sequence data type %s",
-						bufx, format_type_be(seqform->seqtypid))));
+				 errmsg("MAXVALUE (%lld) is out of range for sequence data type %s",
+						(long long)seqform->seqmax, format_type_be(seqform->seqtypid))));
 	}
 
 	/* MINVALUE (null arg means NO MINVALUE) */
@@ -1506,28 +1489,19 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	if ((seqform->seqtypid == INT2OID && (seqform->seqmin < PG_INT16_MIN || seqform->seqmin > PG_INT16_MAX))
 		|| (seqform->seqtypid == INT4OID && (seqform->seqmin < PG_INT32_MIN || seqform->seqmin > PG_INT32_MAX)))
 	{
-		char		bufm[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) is out of range for sequence data type %s",
-						bufm, format_type_be(seqform->seqtypid))));
+				 errmsg("MINVALUE (%lld) is out of range for sequence data type %s",
+						(long long)seqform->seqmin, format_type_be(seqform->seqtypid))));
 	}
 
 	/* crosscheck min/max */
 	if (seqform->seqmin >= seqform->seqmax)
 	{
-		char		bufm[100],
-					bufx[100];
-
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
-		snprintf(bufx, sizeof(bufx), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("MINVALUE (%s) must be less than MAXVALUE (%s)",
-						bufm, bufx)));
+				 errmsg("MINVALUE (%lld) must be less than MAXVALUE (%lld)",
+						(long long)seqform->seqmin, (long long)seqform->seqmax)));
 	}
 
 	/* START WITH */
@@ -1546,27 +1520,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck START */
 	if (seqform->seqstart < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long)seqform->seqstart, (long long)seqform->seqmin)));
 	}
 	if (seqform->seqstart > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqform->seqstart);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("START value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("START value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long)seqform->seqstart, (long long)seqform->seqmax)));
 	}
 
 	/* RESTART [WITH] */
@@ -1588,27 +1552,17 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 	/* crosscheck RESTART (or current value, if changing MIN/MAX) */
 	if (seqdataform->last_value < seqform->seqmin)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmin);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be less than MINVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be less than MINVALUE (%lld)",
+						(long long)seqdataform->last_value, (long long)seqform->seqmin)));
 	}
 	if (seqdataform->last_value > seqform->seqmax)
 	{
-		char		bufs[100],
-					bufm[100];
-
-		snprintf(bufs, sizeof(bufs), INT64_FORMAT, seqdataform->last_value);
-		snprintf(bufm, sizeof(bufm), INT64_FORMAT, seqform->seqmax);
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("RESTART value (%s) cannot be greater than MAXVALUE (%s)",
-						bufs, bufm)));
+				 errmsg("RESTART value (%lld) cannot be greater than MAXVALUE (%lld)",
+						(long long)seqdataform->last_value, (long long)seqform->seqmax)));
 	}
 
 	/* CACHE */
@@ -1617,13 +1571,10 @@ init_params(ParseState *pstate, List *options, bool for_identity,
 		seqform->seqcache = defGetInt64(cache_value);
 		if (seqform->seqcache <= 0)
 		{
-			char		buf[100];
-
-			snprintf(buf, sizeof(buf), INT64_FORMAT, seqform->seqcache);
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("CACHE (%s) must be greater than zero",
-							buf)));
+					 errmsg("CACHE (%lld) must be greater than zero",
+							(long long)seqform->seqcache)));
 		}
 		seqdataform->log_cnt = 0;
 	}
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index 60e57876de..e321a70138 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -113,9 +113,8 @@ TransactionIdInRecentPast(FullTransactionId fxid, TransactionId *extracted_xid)
 	if (!FullTransactionIdPrecedes(fxid, now_fullxid))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("transaction ID %s is in the future",
-						psprintf(UINT64_FORMAT,
-								 U64FromFullTransactionId(fxid)))));
+				 errmsg("transaction ID %llu is in the future",
+						(unsigned long long)U64FromFullTransactionId(fxid))));
 
 	/*
 	 * ShmemVariableCache->oldestClogXid is protected by XactTruncationLock,
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 7e69475947..8509bd15e8 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -132,8 +132,6 @@ static void
 progress_report(bool finished)
 {
 	int			percent;
-	char		total_size_str[32];
-	char		current_size_str[32];
 	pg_time_t	now;
 
 	Assert(showprogress);
@@ -152,18 +150,9 @@ progress_report(bool finished)
 	/* Calculate current percentage of size done */
 	percent = total_size ? (int) ((current_size) * 100 / total_size) : 0;
 
-	/*
-	 * Separate step to keep platform-dependent format code out of
-	 * translatable strings.  And we only test for INT64_FORMAT availability
-	 * in snprintf, not fprintf.
-	 */
-	snprintf(total_size_str, sizeof(total_size_str), INT64_FORMAT,
-			 total_size / (1024 * 1024));
-	snprintf(current_size_str, sizeof(current_size_str), INT64_FORMAT,
-			 current_size / (1024 * 1024));
-
-	fprintf(stderr, _("%*s/%s MB (%d%%) computed"),
-			(int) strlen(current_size_str), current_size_str, total_size_str,
+	fprintf(stderr, _("%lld/%lld MB (%d%%) computed"),
+			(long long)(current_size / (1024 * 1024)),
+			(long long)(total_size / (1024 * 1024)),
 			percent);
 
 	/*
@@ -657,11 +646,11 @@ main(int argc, char *argv[])
 			progress_report(true);
 
 		printf(_("Checksum operation completed\n"));
-		printf(_("Files scanned:   %s\n"), psprintf(INT64_FORMAT, files_scanned));
-		printf(_("Blocks scanned:  %s\n"), psprintf(INT64_FORMAT, blocks_scanned));
+		printf(_("Files scanned:   %lld\n"), (long long)files_scanned);
+		printf(_("Blocks scanned:  %lld\n"), (long long)blocks_scanned);
 		if (mode == PG_MODE_CHECK)
 		{
-			printf(_("Bad checksums:  %s\n"), psprintf(INT64_FORMAT, badblocks));
+			printf(_("Bad checksums:  %lld\n"), (long long)badblocks);
 			printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
 
 			if (badblocks > 0)
@@ -669,8 +658,8 @@ main(int argc, char *argv[])
 		}
 		else if (mode == PG_MODE_ENABLE)
 		{
-			printf(_("Files written:  %s\n"), psprintf(INT64_FORMAT, files_written));
-			printf(_("Blocks written: %s\n"), psprintf(INT64_FORMAT, blocks_written));
+			printf(_("Files written:  %lld\n"), (long long)files_written);
+			printf(_("Blocks written: %lld\n"), (long long)blocks_written);
 		}
 	}
 
-- 
2.35.1

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Aleksander Alekseev (#9)
Re: [PATCH] Remove workarounds to format [u]int64's

Aleksander Alekseev <aleksander@timescale.com> writes:

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

Pushed. I removed now-unnecessary braces, reflowed some lines
as suggested by Pavel, and pgindent'ed (which insisted on adding
spaces after the casts, as is project style).

regards, tom lane

#11Peter Eisentraut
peter.eisentraut@enterprisedb.com
In reply to: Aleksander Alekseev (#9)
1 attachment(s)
Re: [PATCH] Remove workarounds to format [u]int64's

On 21.03.22 15:37, Aleksander Alekseev wrote:

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

I think in some cases we can make this even simpler (and cast-free) by
changing the underlying variable to be long long instead of int64.
Especially in cases where the whole point of the variable is to be some
counter that ends up being printed, there isn't a need to use int64 in
the first place. See attached patch for examples.

Attachments:

0001-Change-some-variables-to-long-long-from-int64.patchtext/plain; charset=UTF-8; name=0001-Change-some-variables-to-long-long-from-int64.patchDownload
From 9c5109c32b8702d14e42324cba027ee987df6cd0 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Wed, 23 Mar 2022 21:44:48 +0100
Subject: [PATCH] Change some variables to long long from int64

This makes printing them simpler.
---
 src/backend/commands/copy.c              |  2 +-
 src/backend/commands/copyfrom.c          | 20 ++++++++--------
 src/backend/commands/copyto.c            |  2 +-
 src/backend/tcop/utility.c               |  2 +-
 src/bin/pg_checksums/pg_checksums.c      | 30 ++++++++++++------------
 src/include/commands/copy.h              |  6 ++---
 src/include/commands/copyfrom_internal.h |  2 +-
 7 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 7da7105d44..8bcfc305ee 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -63,7 +63,7 @@
 void
 DoCopy(ParseState *pstate, const CopyStmt *stmt,
 	   int stmt_location, int stmt_len,
-	   uint64 *processed)
+	   unsigned long long *processed)
 {
 	bool		is_from = stmt->is_from;
 	bool		pipe = (stmt->filename == NULL);
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index db6eb6fae7..5696c7e857 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -80,7 +80,7 @@ typedef struct CopyMultiInsertBuffer
 	ResultRelInfo *resultRelInfo;	/* ResultRelInfo for 'relid' */
 	BulkInsertState bistate;	/* BulkInsertState for this rel */
 	int			nused;			/* number of 'slots' containing tuples */
-	uint64		linenos[MAX_BUFFERED_TUPLES];	/* Line # of tuple in copy
+	unsigned long long linenos[MAX_BUFFERED_TUPLES];	/* Line # of tuple in copy
 												 * stream */
 } CopyMultiInsertBuffer;
 
@@ -122,12 +122,12 @@ CopyFromErrorCallback(void *arg)
 		if (cstate->cur_attname)
 			errcontext("COPY %s, line %llu, column %s",
 					   cstate->cur_relname,
-					   (unsigned long long) cstate->cur_lineno,
+					   cstate->cur_lineno,
 					   cstate->cur_attname);
 		else
 			errcontext("COPY %s, line %llu",
 					   cstate->cur_relname,
-					   (unsigned long long) cstate->cur_lineno);
+					   cstate->cur_lineno);
 	}
 	else
 	{
@@ -139,7 +139,7 @@ CopyFromErrorCallback(void *arg)
 			attval = limit_printout_length(cstate->cur_attval);
 			errcontext("COPY %s, line %llu, column %s: \"%s\"",
 					   cstate->cur_relname,
-					   (unsigned long long) cstate->cur_lineno,
+					   cstate->cur_lineno,
 					   cstate->cur_attname,
 					   attval);
 			pfree(attval);
@@ -149,7 +149,7 @@ CopyFromErrorCallback(void *arg)
 			/* error is relevant to a particular column, value is NULL */
 			errcontext("COPY %s, line %llu, column %s: null input",
 					   cstate->cur_relname,
-					   (unsigned long long) cstate->cur_lineno,
+					   cstate->cur_lineno,
 					   cstate->cur_attname);
 		}
 		else
@@ -166,14 +166,14 @@ CopyFromErrorCallback(void *arg)
 				lineval = limit_printout_length(cstate->line_buf.data);
 				errcontext("COPY %s, line %llu: \"%s\"",
 						   cstate->cur_relname,
-						   (unsigned long long) cstate->cur_lineno, lineval);
+						   cstate->cur_lineno, lineval);
 				pfree(lineval);
 			}
 			else
 			{
 				errcontext("COPY %s, line %llu",
 						   cstate->cur_relname,
-						   (unsigned long long) cstate->cur_lineno);
+						   cstate->cur_lineno);
 			}
 		}
 	}
@@ -303,7 +303,7 @@ CopyMultiInsertBufferFlush(CopyMultiInsertInfo *miinfo,
 {
 	MemoryContext oldcontext;
 	int			i;
-	uint64		save_cur_lineno;
+	unsigned long long save_cur_lineno;
 	CopyFromState cstate = miinfo->cstate;
 	EState	   *estate = miinfo->estate;
 	CommandId	mycid = miinfo->mycid;
@@ -503,7 +503,7 @@ CopyMultiInsertInfoNextFreeSlot(CopyMultiInsertInfo *miinfo,
  */
 static inline void
 CopyMultiInsertInfoStore(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri,
-						 TupleTableSlot *slot, int tuplen, uint64 lineno)
+						 TupleTableSlot *slot, int tuplen, unsigned long long lineno)
 {
 	CopyMultiInsertBuffer *buffer = rri->ri_CopyMultiInsertBuffer;
 
@@ -524,7 +524,7 @@ CopyMultiInsertInfoStore(CopyMultiInsertInfo *miinfo, ResultRelInfo *rri,
 /*
  * Copy FROM file to relation.
  */
-uint64
+unsigned long long
 CopyFrom(CopyFromState cstate)
 {
 	ResultRelInfo *resultRelInfo;
diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c
index 55c38b04c4..6debafec17 100644
--- a/src/backend/commands/copyto.c
+++ b/src/backend/commands/copyto.c
@@ -767,7 +767,7 @@ EndCopyTo(CopyToState cstate)
 /*
  * Copy from relation or query TO file.
  */
-uint64
+unsigned long long
 DoCopyTo(CopyToState cstate)
 {
 	bool		pipe = (cstate->filename == NULL);
diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 3780c6e812..db8f2a191d 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -736,7 +736,7 @@ standard_ProcessUtility(PlannedStmt *pstmt,
 
 		case T_CopyStmt:
 			{
-				uint64		processed;
+				unsigned long long processed;
 
 				DoCopy(pstate, (CopyStmt *) parsetree,
 					   pstmt->stmt_location, pstmt->stmt_len,
diff --git a/src/bin/pg_checksums/pg_checksums.c b/src/bin/pg_checksums/pg_checksums.c
index 5f0f5ee62d..82ecab8483 100644
--- a/src/bin/pg_checksums/pg_checksums.c
+++ b/src/bin/pg_checksums/pg_checksums.c
@@ -33,11 +33,11 @@
 #include "storage/checksum_impl.h"
 
 
-static int64 files_scanned = 0;
-static int64 files_written = 0;
-static int64 blocks_scanned = 0;
-static int64 blocks_written = 0;
-static int64 badblocks = 0;
+static long long files_scanned = 0;
+static long long files_written = 0;
+static long long blocks_scanned = 0;
+static long long blocks_written = 0;
+static long long badblocks = 0;
 static ControlFileData *ControlFile;
 
 static char *only_filenode = NULL;
@@ -69,8 +69,8 @@ static const char *progname;
 /*
  * Progress status information.
  */
-int64		total_size = 0;
-int64		current_size = 0;
+long long	total_size = 0;
+long long	current_size = 0;
 static pg_time_t last_progress_report = 0;
 
 static void
@@ -151,8 +151,8 @@ progress_report(bool finished)
 	percent = total_size ? (int) ((current_size) * 100 / total_size) : 0;
 
 	fprintf(stderr, _("%lld/%lld MB (%d%%) computed"),
-			(long long) (current_size / (1024 * 1024)),
-			(long long) (total_size / (1024 * 1024)),
+			current_size / (1024 * 1024),
+			total_size / (1024 * 1024),
 			percent);
 
 	/*
@@ -188,7 +188,7 @@ scan_file(const char *fn, int segmentno)
 	int			f;
 	BlockNumber blockno;
 	int			flags;
-	int64		blocks_written_in_file = 0;
+	long long	blocks_written_in_file = 0;
 
 	Assert(mode == PG_MODE_ENABLE ||
 		   mode == PG_MODE_CHECK);
@@ -646,11 +646,11 @@ main(int argc, char *argv[])
 			progress_report(true);
 
 		printf(_("Checksum operation completed\n"));
-		printf(_("Files scanned:   %lld\n"), (long long) files_scanned);
-		printf(_("Blocks scanned:  %lld\n"), (long long) blocks_scanned);
+		printf(_("Files scanned:   %lld\n"), files_scanned);
+		printf(_("Blocks scanned:  %lld\n"), blocks_scanned);
 		if (mode == PG_MODE_CHECK)
 		{
-			printf(_("Bad checksums:  %lld\n"), (long long) badblocks);
+			printf(_("Bad checksums:  %lld\n"), badblocks);
 			printf(_("Data checksum version: %u\n"), ControlFile->data_checksum_version);
 
 			if (badblocks > 0)
@@ -658,8 +658,8 @@ main(int argc, char *argv[])
 		}
 		else if (mode == PG_MODE_ENABLE)
 		{
-			printf(_("Files written:  %lld\n"), (long long) files_written);
-			printf(_("Blocks written: %lld\n"), (long long) blocks_written);
+			printf(_("Files written:  %lld\n"), files_written);
+			printf(_("Blocks written: %lld\n"), blocks_written);
 		}
 	}
 
diff --git a/src/include/commands/copy.h b/src/include/commands/copy.h
index 8694da5004..a685ed3fbb 100644
--- a/src/include/commands/copy.h
+++ b/src/include/commands/copy.h
@@ -58,7 +58,7 @@ typedef int (*copy_data_source_cb) (void *outbuf, int minread, int maxread);
 
 extern void DoCopy(ParseState *state, const CopyStmt *stmt,
 				   int stmt_location, int stmt_len,
-				   uint64 *processed);
+				   unsigned long long *processed);
 
 extern void ProcessCopyOptions(ParseState *pstate, CopyFormatOptions *ops_out, bool is_from, List *options);
 extern CopyFromState BeginCopyFrom(ParseState *pstate, Relation rel, Node *whereClause,
@@ -71,7 +71,7 @@ extern bool NextCopyFromRawFields(CopyFromState cstate,
 								  char ***fields, int *nfields);
 extern void CopyFromErrorCallback(void *arg);
 
-extern uint64 CopyFrom(CopyFromState cstate);
+extern unsigned long long CopyFrom(CopyFromState cstate);
 
 extern DestReceiver *CreateCopyDestReceiver(void);
 
@@ -82,7 +82,7 @@ extern CopyToState BeginCopyTo(ParseState *pstate, Relation rel, RawStmt *query,
 							   Oid queryRelId, const char *filename, bool is_program,
 							   List *attnamelist, List *options);
 extern void EndCopyTo(CopyToState cstate);
-extern uint64 DoCopyTo(CopyToState cstate);
+extern unsigned long long DoCopyTo(CopyToState cstate);
 extern List *CopyGetAttnums(TupleDesc tupDesc, Relation rel,
 							List *attnamelist);
 
diff --git a/src/include/commands/copyfrom_internal.h b/src/include/commands/copyfrom_internal.h
index 3df1c5a97c..bec8f97bc6 100644
--- a/src/include/commands/copyfrom_internal.h
+++ b/src/include/commands/copyfrom_internal.h
@@ -78,7 +78,7 @@ typedef struct CopyFromStateData
 
 	/* these are just for error messages, see CopyFromErrorCallback */
 	const char *cur_relname;	/* table name for error messages */
-	uint64		cur_lineno;		/* line number for error messages */
+	unsigned long long cur_lineno;		/* line number for error messages */
 	const char *cur_attname;	/* current att for error messages */
 	const char *cur_attval;		/* current att value for error messages */
 
-- 
2.35.1

#12Pavel Borisov
pashkin.elfe@gmail.com
In reply to: Peter Eisentraut (#11)
Re: [PATCH] Remove workarounds to format [u]int64's

On 21.03.22 15:37, Aleksander Alekseev wrote:

It would not simplify things for them at all, just mess it up.
The master copies of the .po files are kept in a different repo.
Also, I believe that extraction of new message strings is automated
already.

Got it, thanks. Here is the corrected patch. It includes all the
changes by me and Japin, and doesn't touch PO files.

I think in some cases we can make this even simpler (and cast-free) by
changing the underlying variable to be long long instead of int64.
Especially in cases where the whole point of the variable is to be some
counter that ends up being printed, there isn't a need to use int64 in
the first place. See attached patch for examples.

Yes, this will work, when we can define a variable itself as *long long*.
But for some applications: [1]/messages/by-id/CACG=ezYV3FM5i9ws2QLyF+rz5WHTqheL59VRsHGsgAwfx8gh4g@mail.gmail.com, [2]/messages/by-id/CACG=ezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe=pyyjVWA@mail.gmail.com, I suppose we'll need exactly uint64 to
represent TransactionId. uint64 is warrantied to fit into *unsigned long
long*, but on most of archs it is just *unsigned long*. Defining what we
need to be uint64 as *unsigned long long* on these archs will mean it
become uint128, which we may not like.

In my opinion, in many places, it's better to have casts when it's for
printing fixed-width int/uint variables than the alternative.

[1]: /messages/by-id/CACG=ezYV3FM5i9ws2QLyF+rz5WHTqheL59VRsHGsgAwfx8gh4g@mail.gmail.com
/messages/by-id/CACG=ezYV3FM5i9ws2QLyF+rz5WHTqheL59VRsHGsgAwfx8gh4g@mail.gmail.com
[2]: /messages/by-id/CACG=ezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe=pyyjVWA@mail.gmail.com
/messages/by-id/CACG=ezZe1NQSCnfHOr78AtAZxJZeCvxrts0ygrxYwe=pyyjVWA@mail.gmail.com

--
Best regards,
Pavel Borisov

Postgres Professional: http://postgrespro.com <http://www.postgrespro.com&gt;