Skip to content

Commit ef9c972

Browse files
jeff-daviscoreyhuinker
authored andcommitted
Don't convert to and from floats in pg_dump.
Commit 8f42718 improved performance by remembering relation stats as native types rather than issuing a new query for each relation. Using native types is fine for integers like relpages; but reltuples is floating point. The commit controllled for that complexity by using setlocale(LC_NUMERIC, "C"). After that, Alexander Lakhin found a problem in pg_strtof(), fixed in 00d61a0. While we aren't aware of any more problems with that approach, it seems wise to just use a string the whole way for floating point values, as Corey's original patch did, and get rid of the setlocale(). Integers are still converted to native types to avoid wasting memory. Co-authored-by: Corey Huinker <corey.huinker@gmail.com> Discussion: https://postgr.es/m/3049348.1740855411@sss.pgh.pa.us Discussion: https://postgr.es/m/560cca3781740bd69881bb07e26eb8f65b09792c.camel%40j-davis.com (cherry picked from commit 1852aea)
1 parent 7f9f8d9 commit ef9c972

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,6 @@ main(int argc, char **argv)
433433
pg_logging_set_level(PG_LOG_WARNING);
434434
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
435435

436-
/* ensure that locale does not affect floating point interpretation */
437-
setlocale(LC_NUMERIC, "C");
438-
439436
/*
440437
* Initialize what we need for parallel execution, especially for thread
441438
* support on Windows.
@@ -6291,10 +6288,12 @@ getFuncs(Archive *fout, int *numFuncs)
62916288
* getRelationStatistics
62926289
* register the statistics object as a dependent of the relation.
62936290
*
6291+
* reltuples is passed as a string to avoid complexities in converting from/to
6292+
* floating point.
62946293
*/
62956294
static RelStatsInfo *
62966295
getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
6297-
float reltuples, int32 relallvisible, char relkind,
6296+
char *reltuples, int32 relallvisible, char relkind,
62986297
char **indAttNames, int nindAttNames)
62996298
{
63006299
if (!fout->dopt->dumpStatistics)
@@ -6321,7 +6320,7 @@ getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
63216320
dobj->name = pg_strdup(rel->name);
63226321
dobj->namespace = rel->namespace;
63236322
info->relpages = relpages;
6324-
info->reltuples = reltuples;
6323+
info->reltuples = pstrdup(reltuples);
63256324
info->relallvisible = relallvisible;
63266325
info->relkind = relkind;
63276326
info->indAttNames = indAttNames;
@@ -6622,7 +6621,6 @@ getTables(Archive *fout, int *numTables)
66226621

66236622
for (i = 0; i < ntups; i++)
66246623
{
6625-
float reltuples = strtof(PQgetvalue(res, i, i_reltuples), NULL);
66266624
int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
66276625

66286626
tblinfo[i].dobj.objType = DO_TABLE;
@@ -6725,8 +6723,8 @@ getTables(Archive *fout, int *numTables)
67256723
/* Add statistics */
67266724
if (tblinfo[i].interesting)
67276725
getRelationStatistics(fout, &tblinfo[i].dobj, tblinfo[i].relpages,
6728-
reltuples, relallvisible, tblinfo[i].relkind,
6729-
NULL, 0);
6726+
PQgetvalue(res, i, i_reltuples),
6727+
relallvisible, tblinfo[i].relkind, NULL, 0);
67306728

67316729
/*
67326730
* Read-lock target tables to make sure they aren't DROPPED or altered
@@ -7202,7 +7200,6 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
72027200
char indexkind;
72037201
RelStatsInfo *relstats;
72047202
int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
7205-
float reltuples = strtof(PQgetvalue(res, j, i_reltuples), NULL);
72067203
int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
72077204

72087205
indxinfo[j].dobj.objType = DO_INDEX;
@@ -7245,7 +7242,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
72457242
}
72467243

72477244
relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
7248-
reltuples, relallvisible, indexkind,
7245+
PQgetvalue(res, j, i_reltuples),
7246+
relallvisible, indexkind,
72497247
indAttNames, nindAttNames);
72507248

72517249
contype = *(PQgetvalue(res, j, i_contype));
@@ -9936,7 +9934,6 @@ dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
99369934
DumpId *deps = NULL;
99379935
int ndeps = 0;
99389936
char *qualified_name;
9939-
char reltuples_str[FLOAT_SHORTEST_DECIMAL_LEN];
99409937
int i_attname;
99419938
int i_inherited;
99429939
int i_null_frac;
@@ -10011,8 +10008,7 @@ dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
1001110008
appendStringLiteralAH(out, qualified_name, fout);
1001210009
appendPQExpBufferStr(out, "::regclass,\n");
1001310010
appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
10014-
float_to_shortest_decimal_buf(rsinfo->reltuples, reltuples_str);
10015-
appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", reltuples_str);
10011+
appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
1001610012
appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer\n);\n",
1001710013
rsinfo->relallvisible);
1001810014

src/bin/pg_dump/pg_dump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ typedef struct _relStatsInfo
432432
{
433433
DumpableObject dobj;
434434
int32 relpages;
435-
float reltuples;
435+
char *reltuples;
436436
int32 relallvisible;
437437
char relkind; /* 'r', 'm', 'i', etc */
438438

0 commit comments

Comments
 (0)