Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a1cd8b

Browse files
committed
Unwind some workarounds for lack of portable int64 format specifier
Because there is no portable int64/uint64 format specifier and we can't stick macros like INT64_FORMAT into the middle of a translatable string, we have been using various workarounds that put the number to be printed into a string buffer first. Now that we always use our own sprintf(), we can rely on %lld and %llu to work, so we can use those. This patch undoes this workaround in a few places where it was egregiously verbose. Reviewed-by: Tom Lane <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/CAH2-Wz%3DWbNxc5ob5NJ9yqo2RMJ0q4HXDS30GVCobeCvC9A1L9A%40mail.gmail.com
1 parent 7b925e1 commit 6a1cd8b

File tree

6 files changed

+15
-59
lines changed

6 files changed

+15
-59
lines changed

src/backend/access/transam/xlogreader.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -783,20 +783,10 @@ XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr,
783783
if (state->system_identifier &&
784784
longhdr->xlp_sysid != state->system_identifier)
785785
{
786-
char fhdrident_str[32];
787-
char sysident_str[32];
788-
789-
/*
790-
* Format sysids separately to keep platform-dependent format code
791-
* out of the translatable message string.
792-
*/
793-
snprintf(fhdrident_str, sizeof(fhdrident_str), UINT64_FORMAT,
794-
longhdr->xlp_sysid);
795-
snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
796-
state->system_identifier);
797786
report_invalid_record(state,
798-
"WAL file is from different database system: WAL file database system identifier is %s, pg_control database system identifier is %s",
799-
fhdrident_str, sysident_str);
787+
"WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
788+
(unsigned long long) longhdr->xlp_sysid,
789+
(unsigned long long) state->system_identifier);
800790
return false;
801791
}
802792
else if (longhdr->xlp_seg_size != state->wal_segment_size)

src/backend/replication/basebackup.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static TimestampTz throttled_last;
106106
static XLogRecPtr startptr;
107107

108108
/* Total number of checksum failures during base backup. */
109-
static int64 total_checksum_failures;
109+
static long long int total_checksum_failures;
110110

111111
/* Do not verify checksums. */
112112
static bool noverify_checksums = false;
@@ -607,14 +607,9 @@ perform_base_backup(basebackup_options *opt)
607607
if (total_checksum_failures)
608608
{
609609
if (total_checksum_failures > 1)
610-
{
611-
char buf[64];
612-
613-
snprintf(buf, sizeof(buf), INT64_FORMAT, total_checksum_failures);
614-
615610
ereport(WARNING,
616-
(errmsg("%s total checksum verification failures", buf)));
617-
}
611+
(errmsg("%lld total checksum verification failures", total_checksum_failures)));
612+
618613
ereport(ERROR,
619614
(errcode(ERRCODE_DATA_CORRUPTED),
620615
errmsg("checksum verification failure during base backup")));

src/bin/pg_controldata/pg_controldata.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ main(int argc, char *argv[])
9999
time_t time_tmp;
100100
char pgctime_str[128];
101101
char ckpttime_str[128];
102-
char sysident_str[32];
103102
char mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1];
104103
const char *strftime_fmt = "%c";
105104
const char *progname;
@@ -222,13 +221,6 @@ main(int argc, char *argv[])
222221
else
223222
strcpy(xlogfilename, _("???"));
224223

225-
/*
226-
* Format system_identifier and mock_authentication_nonce separately to
227-
* keep platform-dependent format code out of the translatable message
228-
* string.
229-
*/
230-
snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
231-
ControlFile->system_identifier);
232224
for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
233225
snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
234226
(unsigned char) ControlFile->mock_authentication_nonce[i]);
@@ -237,8 +229,8 @@ main(int argc, char *argv[])
237229
ControlFile->pg_control_version);
238230
printf(_("Catalog version number: %u\n"),
239231
ControlFile->catalog_version_no);
240-
printf(_("Database system identifier: %s\n"),
241-
sysident_str);
232+
printf(_("Database system identifier: %llu\n"),
233+
(unsigned long long) ControlFile->system_identifier);
242234
printf(_("Database cluster state: %s\n"),
243235
dbState(ControlFile->state));
244236
printf(_("pg_control last modified: %s\n"),

src/bin/pg_resetwal/pg_resetwal.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -748,26 +748,17 @@ GuessControlValues(void)
748748
static void
749749
PrintControlValues(bool guessed)
750750
{
751-
char sysident_str[32];
752-
753751
if (guessed)
754752
printf(_("Guessed pg_control values:\n\n"));
755753
else
756754
printf(_("Current pg_control values:\n\n"));
757755

758-
/*
759-
* Format system_identifier separately to keep platform-dependent format
760-
* code out of the translatable message string.
761-
*/
762-
snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
763-
ControlFile.system_identifier);
764-
765756
printf(_("pg_control version number: %u\n"),
766757
ControlFile.pg_control_version);
767758
printf(_("Catalog version number: %u\n"),
768759
ControlFile.catalog_version_no);
769-
printf(_("Database system identifier: %s\n"),
770-
sysident_str);
760+
printf(_("Database system identifier: %llu\n"),
761+
(unsigned long long) ControlFile.system_identifier);
771762
printf(_("Latest checkpoint's TimeLineID: %u\n"),
772763
ControlFile.checkPointCopy.ThisTimeLineID);
773764
printf(_("Latest checkpoint's full_page_writes: %s\n"),

src/bin/pg_rewind/libpq_fetch.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ receiveFileChunks(const char *sql)
251251
char *filename;
252252
int filenamelen;
253253
int64 chunkoff;
254-
char chunkoff_str[32];
255254
int chunksize;
256255
char *chunk;
257256

@@ -327,13 +326,8 @@ receiveFileChunks(const char *sql)
327326
continue;
328327
}
329328

330-
/*
331-
* Separate step to keep platform-dependent format code out of
332-
* translatable strings.
333-
*/
334-
snprintf(chunkoff_str, sizeof(chunkoff_str), INT64_FORMAT, chunkoff);
335-
pg_log_debug("received chunk for file \"%s\", offset %s, size %d",
336-
filename, chunkoff_str, chunksize);
329+
pg_log_debug("received chunk for file \"%s\", offset %lld, size %d",
330+
filename, (long long int) chunkoff, chunksize);
337331

338332
open_target_file(filename, false);
339333

src/bin/pg_test_timing/pg_test_timing.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static uint64 test_timing(int32);
1818
static void output(uint64 loop_count);
1919

2020
/* record duration in powers of 2 microseconds */
21-
int64 histogram[32];
21+
long long int histogram[32];
2222

2323
int
2424
main(int argc, char *argv[])
@@ -190,14 +190,8 @@ output(uint64 loop_count)
190190
Max(10, len3), header3);
191191

192192
for (i = 0; i <= max_bit; i++)
193-
{
194-
char buf[100];
195-
196-
/* lame hack to work around INT64_FORMAT deficiencies */
197-
snprintf(buf, sizeof(buf), INT64_FORMAT, histogram[i]);
198-
printf("%*ld %*.5f %*s\n",
193+
printf("%*ld %*.5f %*lld\n",
199194
Max(6, len1), 1l << i,
200195
Max(10, len2) - 1, (double) histogram[i] * 100 / loop_count,
201-
Max(10, len3), buf);
202-
}
196+
Max(10, len3), histogram[i]);
203197
}

0 commit comments

Comments
 (0)