8000 Merge pull request #506 from postgrespro/PBCKP-216-float_locale · postgrespro/pg_probackup@9f2ded0 · GitHub
[go: up one dir, main page]

Skip to content

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

Provide feedback

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

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f2ded0

Browse files
authored
Merge pull request #506 from postgrespro/PBCKP-216-float_locale
Pbckp 216 float locale
2 parents 88b50da + c0c07ac commit 9f2ded0

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/pg_probackup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ main(int argc, char *argv[])
315315
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_probackup"));
316316
PROGRAM_FULL_PATH = palloc0(MAXPGPATH);
317317

318+
// Setting C locale for numeric values in order to impose dot-based floating-point representation
319+
memorize_environment_locale();
320+
setlocale(LC_NUMERIC, "C");
321+
318322
/* Get current time */
319323
current_time = time(NULL);
320324

@@ -1050,6 +1054,8 @@ main(int argc, char *argv[])
10501054
break;
10511055
}
10521056

1057+
free_environment_locale();
1058+
10531059
return 0;
10541060
}
10551061

src/pg_probackup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ extern InstanceConfig *readInstanceConfigFile(InstanceState *instanceState);
907907
/* in show.c */
908908
extern int do_show(CatalogState *catalogState, InstanceState *instanceState,
909909
time_t requested_backup_id, bool show_archive);
910+
extern void memorize_environment_locale(void);
911+
extern void free_environment_locale(void);
910912

911913
/* in delete.c */
912914
extern void do_delete(InstanceState *instanceState, time_t backup_id);

src/show.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* show.c: show backup information.
44
*
55
* Portions Copyright (c) 2009-2011, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
6-
* Portions Copyright (c) 2015-2019, Postgres Professional
6+
* Portions Copyright (c) 2015-2022, Postgres Professional
77
*
88
*-------------------------------------------------------------------------
99
*/
@@ -12,6 +12,7 @@
1212

1313
#include <time.h>
1414
#include <dirent.h>
15+
#include <locale.h>
1516
#include <sys/stat.h>
1617

1718
#include "utils/json.h"
@@ -71,6 +72,43 @@ static PQExpBufferData show_buf;
7172
static bool first_instance = true;
7273
static int32 json_level = 0;
7374

75+
static const char* lc_env_locale;
76+
typedef enum {
77+
LOCALE_C, // Used for formatting output to unify the dot-based floating point representation
78+
LOCALE_ENV // Default environment locale
79+
} output_numeric_locale;
80+
81+
#ifdef HAVE_USELOCALE
82+
static locale_t env_locale, c_locale;
83+
#endif
84+
void memorize_environment_locale() {
85+
lc_env_locale = (const char *)getenv("LC_NUMERIC");
86+
lc_env_locale = lc_env_locale != NULL ? lc_env_locale : "C";
87+
#ifdef HAVE_USELOCALE
88+
env_locale = newlocale(LC_NUMERIC_MASK, lc_env_locale, (locale_t)0);
89+
c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
90+
#else
91+
#ifdef HAVE__CONFIGTHREADLOCALE
92+
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
93+
#endif
94+
#endif
95+
}
96+
97+
void free_environment_locale() {
98+
#ifdef HAVE_USELOCALE
99+
freelocale(env_locale);
100+
freelocale(c_locale);
101+
#endif
102+
}
103+
104+
static void set_output_numeric_locale(output_numeric_locale loc) {
105+
#ifdef HAVE_USELOCALE
106+
uselocale(loc == LOCALE_C ? c_locale : env_locale);
107+
#else
108+
setlocale(LC_NUMERIC, loc == LOCALE_C ? "C" : lc_env_locale);
109+
#endif
110+
}
111+
74112
/*
75113
* Entry point of pg_probackup SHOW subcommand.
76114
*/
@@ -513,6 +551,9 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
513551
ShowBackendRow *rows;
514552
TimeLineID parent_tli = 0;
515553

554+
// Since we've been printing a table, set LC_NUMERIC to its default environment value
555+
set_output_numeric_locale(LOCALE_ENV);
556+
516557
for (i = 0; i < SHOW_FIELDS_COUNT; i++)
517558
widths[i] = strlen(names[i]);
518559

@@ -726,6 +767,8 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
726767
}
727768

728769
pfree(rows);
770+
// Restore the C locale
771+
set_output_numeric_locale(LOCALE_C);
729772
}
730773

731774
/*
@@ -806,6 +849,9 @@ show_archive_plain(const char *instance_name, uint32 xlog_seg_size,
806849
uint32 widths_sum = 0;
807850
ShowArchiveRow *rows;
808851

852+
// Since we've been printing a table, set LC_NUMERIC to its default environment value
853+
set_output_numeric_locale(LOCALE_ENV);
854+
809855
for (i = 0; i < SHOW_ARCHIVE_FIELDS_COUNT; i++)
810856
widths[i] = strlen(names[i]);
811857

@@ -973,6 +1019,8 @@ show_archive_plain(const char *instance_name, uint32 xlog_seg_size,
9731019
}
9741020

9751021
pfree(rows);
1022+
// Restore the C locale
1023+
set_output_numeric_locale(LOCALE_C);
9761024
//TODO: free timelines
9771025
}
9781026

@@ -1045,8 +1093,9 @@ show_archive_json(const char *instance_name, uint32 xlog_seg_size,
10451093
appendPQExpBuffer(buf, "%lu", tlinfo->size);
10461094

10471095
json_add_key(buf, "zratio", json_level);
1096+
10481097
if (tlinfo->size != 0)
1049-
zratio = ((float)xlog_seg_size*tlinfo->n_xlog_files) / tlinfo->size;
1098+
zratio = ((float) xlog_seg_size * tlinfo->n_xlog_files) / tlinfo->size;
10501099
appendPQExpBuffer(buf, "%.2f", zratio);
10511100

10521101
if (tlinfo->closest_backup != NULL)

src/utils/configuration.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#include "getopt_long.h"
2020

21+
#ifndef WIN32
22+
#include <pwd.h>
23+
#endif
2124
#include <time.h>
2225

2326
#define MAXPG_LSNCOMPONENT 8

0 commit comments

Comments
 (0)
0