Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1852aea

Browse files
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
1 parent 7fb8801 commit 1852aea

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
@@ -525,9 +525,6 @@ main(int argc, char **argv)
525525
pg_logging_set_level(PG_LOG_WARNING);
526526
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
527527

528-
/* ensure that locale does not affect floating point interpretation */
529-
setlocale(LC_NUMERIC, "C");
530-
531528
/*
532529
* Initialize what we need for parallel execution, especially for thread
533530
* support on Windows.
@@ -6816,10 +6813,12 @@ getFuncs(Archive *fout)
68166813
* getRelationStatistics
68176814
* register the statistics object as a dependent of the relation.
68186815
*
6816+
* reltuples is passed as a string to avoid complexities in converting from/to
6817+
* floating point.
68196818
*/
68206819
static RelStatsInfo *
68216820
getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
6822-
float reltuples, int32 relallvisible, char relkind,
6821+
char *reltuples, int32 relallvisible, char relkind,
68236822
char **indAttNames, int nindAttNames)
68246823
{
68256824
if (!fout->dopt->dumpStatistics)
@@ -6846,7 +6845,7 @@ getRelationStatistics(Archive *fout, DumpableObject *rel, int32 relpages,
68466845
dobj->name = pg_strdup(rel->name);
68476846
dobj->namespace = rel->namespace;
68486847
info->relpages = relpages;
6849-
info->reltuples = reltuples;
6848+
info->reltuples = pstrdup(reltuples);
68506849
info->relallvisible = relallvisible;
68516850
info->relkind = relkind;
68526851
info->indAttNames = indAttNames;
@@ -7149,7 +7148,6 @@ getTables(Archive *fout, int *numTables)
71497148

71507149
for (i = 0; i < ntups; i++)
71517150
{
7152-
float reltuples = strtof(PQgetvalue(res, i, i_reltuples), NULL);
71537151
int32 relallvisible = atoi(PQgetvalue(res, i, i_relallvisible));
71547152

71557153
tblinfo[i].dobj.objType = DO_TABLE;
@@ -7252,8 +7250,8 @@ getTables(Archive *fout, int *numTables)
72527250
/* Add statistics */
72537251
if (tblinfo[i].interesting)
72547252
getRelationStatistics(fout, &tblinfo[i].dobj, tblinfo[i].relpages,
7255-
reltuples, relallvisible, tblinfo[i].relkind,
7256-
NULL, 0);
7253+
PQgetvalue(res, i, i_reltuples),
7254+
relallvisible, tblinfo[i].relkind, NULL, 0);
72577255

72587256
/*
72597257
* Read-lock target tables to make sure they aren't DROPPED or altered
@@ -7762,7 +7760,6 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
77627760
char indexkind;
77637761
RelStatsInfo *relstats;
77647762
int32 relpages = atoi(PQgetvalue(res, j, i_relpages));
7765-
float reltuples = strtof(PQgetvalue(res, j, i_reltuples), NULL);
77667763
int32 relallvisible = atoi(PQgetvalue(res, j, i_relallvisible));
77677764

77687765
indxinfo[j].dobj.objType = DO_INDEX;
@@ -7805,7 +7802,8 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
78057802
}
78067803

78077804
relstats = getRelationStatistics(fout, &indxinfo[j].dobj, relpages,
7808-
reltuples, relallvisible, indexkind,
7805+
PQgetvalue(res, j, i_reltuples),
7806+
relallvisible, indexkind,
78097807
indAttNames, nindAttNames);
78107808

78117809
contype = *(PQgetvalue(res, j, i_contype));
@@ -10493,7 +10491,6 @@ dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
1049310491
DumpId *deps = NULL;
1049410492
int ndeps = 0;
1049510493
char *qualified_name;
10496-
char reltuples_str[FLOAT_SHORTEST_DECIMAL_LEN];
1049710494
int i_attname;
1049810495
int i_inherited;
1049910496
int i_null_frac;
@@ -10568,8 +10565,7 @@ dumpRelationStats(Archive *fout, const RelStatsInfo *rsinfo)
1056810565
appendStringLiteralAH(out, qualified_name, fout);
1056910566
appendPQExpBufferStr(out, "::regclass,\n");
1057010567
appendPQExpBuffer(out, "\t'relpages', '%d'::integer,\n", rsinfo->relpages);
10571-
float_to_shortest_decimal_buf(rsinfo->reltuples, reltuples_str);
10572-
appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", reltuples_str);
10568+
appendPQExpBuffer(out, "\t'reltuples', '%s'::real,\n", rsinfo->reltuples);
1057310569
appendPQExpBuffer(out, "\t'relallvisible', '%d'::integer\n);\n",
1057410570
rsinfo->relallvisible);
1057510571

src/bin/pg_dump/pg_dump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ typedef struct _relStatsInfo
439439
{
440440
DumpableObject dobj;
441441
int32 relpages;
442-
float reltuples;
442+
char *reltuples;
443443
int32 relallvisible;
444444
char relkind; /* 'r', 'm', 'i', etc */
445445

0 commit comments

Comments
 (0)