Skip to content

Commit 7b6fba9

Browse files
committed
Handle OID's and unsigned values better in pg_autovacuum.
Matthew T. O'Connor
1 parent dea47ee commit 7b6fba9

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

contrib/pg_autovacuum/pg_autovacuum.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ init_table_info(PGresult *res, int row, db_info * dbi)
117117
atol(PQgetvalue(res, row, PQfnumber(res, "n_tup_upd"))));
118118
new_tbl->curr_vacuum_count = new_tbl->CountAtLastVacuum;
119119

120-
new_tbl->relid = atoi(PQgetvalue(res, row, PQfnumber(res, "oid")));
121-
new_tbl->reltuples = atoi(PQgetvalue(res, row, PQfnumber(res, "reltuples")));
122-
new_tbl->relpages = atoi(PQgetvalue(res, row, PQfnumber(res, "relpages")));
120+
new_tbl->relid = atooid(PQgetvalue(res, row, PQfnumber(res, "oid")));
121+
new_tbl->reltuples = atof(PQgetvalue(res, row, PQfnumber(res, "reltuples")));
122+
new_tbl->relpages = atooid(PQgetvalue(res, row, PQfnumber(res, "relpages")));
123123

124124
if (strcmp("t", PQgetvalue(res, row, PQfnumber(res, "relisshared"))))
125125
new_tbl->relisshared = 0;
@@ -159,8 +159,8 @@ update_table_thresholds(db_info * dbi, tbl_info * tbl, int vacuum_type)
159159
if (res != NULL)
160160
{
161161
tbl->reltuples =
162-
atoi(PQgetvalue(res, 0, PQfnumber(res, "reltuples")));
163-
tbl->relpages = atoi(PQgetvalue(res, 0, PQfnumber(res, "relpages")));
162+
atof(PQgetvalue(res, 0, PQfnumber(res, "reltuples")));
163+
tbl->relpages = atooid(PQgetvalue(res, 0, PQfnumber(res, "relpages")));
164164

165165
/*
166166
* update vacuum thresholds only of we just did a vacuum
@@ -237,7 +237,7 @@ update_table_list(db_info * dbi)
237237
for (i = 0; i < t; i++)
238238
{ /* loop through result set looking for a
239239
* match */
240-
if (tbl->relid == atoi(PQgetvalue(res, i, PQfnumber(res, "oid"))))
240+
if (tbl->relid == atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))))
241241
{
242242
found_match = 1;
243243
break;
@@ -267,7 +267,7 @@ update_table_list(db_info * dbi)
267267
while (tbl_elem != NULL)
268268
{
269269
tbl = ((tbl_info *) DLE_VAL(tbl_elem));
270-
if (tbl->relid == atoi(PQgetvalue(res, i, PQfnumber(res, "oid"))))
270+
if (tbl->relid == atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))))
271271
{
272272
found_match = 1;
273273
break;
@@ -361,9 +361,9 @@ print_table_info(tbl_info * tbl)
361361
{
362362
sprintf(logbuffer, " table name: %s.%s", tbl->dbi->dbname, tbl->table_name);
363363
log_entry(logbuffer);
364-
sprintf(logbuffer, " relid: %i; relisshared: %i", tbl->relid, tbl->relisshared);
364+
sprintf(logbuffer, " relid: %u; relisshared: %i", tbl->relid, tbl->relisshared);
365365
log_entry(logbuffer);
366-
sprintf(logbuffer, " reltuples: %i; relpages: %i", tbl->reltuples, tbl->relpages);
366+
sprintf(logbuffer, " reltuples: %f; relpages: %u", tbl->reltuples, tbl->relpages);
367367
log_entry(logbuffer);
368368
sprintf(logbuffer, " curr_analyze_count: %li; cur_delete_count: %li",
369369
tbl->curr_analyze_count, tbl->curr_vacuum_count);
@@ -407,8 +407,8 @@ init_db_list()
407407
if (dbs->conn != NULL)
408408
{
409409
res = send_query(FROZENOID_QUERY, dbs);
410-
dbs->oid = atoi(PQgetvalue(res, 0, PQfnumber(res, "oid")));
411-
dbs->age = atoi(PQgetvalue(res, 0, PQfnumber(res, "age")));
410+
dbs->oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
411+
dbs->age = atol(PQgetvalue(res, 0, PQfnumber(res, "age")));
412412
if (res)
413413
PQclear(res);
414414

@@ -421,7 +421,7 @@ init_db_list()
421421
/* Simple function to create an instance of the dbinfo struct
422422
Initalizes all the pointers and connects to the database */
423423
db_info *
424-
init_dbinfo(char *dbname, int oid, int age)
424+
init_dbinfo(char *dbname, Oid oid, long age)
425425
{
426426
db_info *newdbinfo = (db_info *) malloc(sizeof(db_info));
427427

@@ -500,15 +500,15 @@ update_db_list(Dllist *db_list)
500500
for (i = 0; i < t; i++)
501501
{ /* loop through result set looking for a
502502
* match */
503-
if (dbi->oid == atoi(PQgetvalue(res, i, PQfnumber(res, "oid"))))
503+
if (dbi->oid == atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))))
504504
{
505505
found_match = 1;
506506

507507
/*
508508
* update the dbi->age so that we ensure
509509
* xid_wraparound won't happen
510510
*/
511-
dbi->age = atoi(PQgetvalue(res, i, PQfnumber(res, "age")));
511+
dbi->age = atol(PQgetvalue(res, i, PQfnumber(res, "age")));
512512
break;
513513
}
514514
}
@@ -536,7 +536,7 @@ update_db_list(Dllist *db_list)
536536
while (db_elem != NULL)
537537
{
538538
dbi = ((db_info *) DLE_VAL(db_elem));
539-
if (dbi->oid == atoi(PQgetvalue(res, i, PQfnumber(res, "oid"))))
539+
if (dbi->oid == atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))))
540540
{
541541
found_match = 1;
542542
break;
@@ -548,8 +548,8 @@ update_db_list(Dllist *db_list)
548548
{
549549
DLAddTail(db_list, DLNewElem(init_dbinfo
550550
(PQgetvalue(res, i, PQfnumber(res, "datname")),
551-
atoi(PQgetvalue(res, i, PQfnumber(res, "oid"))),
552-
atoi(PQgetvalue(res, i, PQfnumber(res, "age"))))));
551+
atooid(PQgetvalue(res, i, PQfnumber(res, "oid"))),
552+
atol(PQgetvalue(res, i, PQfnumber(res, "age"))))));
553553
if (args->debug >= 1)
554554
{
555555
sprintf(logbuffer, "added database: %s", ((db_info *) DLE_VAL(DLGetTail(db_list)))->dbname);
@@ -681,7 +681,7 @@ print_db_info(db_info * dbi, int print_tbl_list)
681681
sprintf(logbuffer, "dbname: %s Username %s Passwd %s", dbi->dbname,
682682
dbi->username, dbi->password);
683683
log_entry(logbuffer);
684-
sprintf(logbuffer, " oid %i InsertThresh: %i DeleteThresh: %i", dbi->oid,
684+
sprintf(logbuffer, " oid %u InsertThresh: %li DeleteThresh: %li", dbi->oid,
685685
dbi->analyze_threshold, dbi->vacuum_threshold);
686686
log_entry(logbuffer);
687687
if (dbi->conn != NULL)
@@ -1072,7 +1072,7 @@ main(int argc, char *argv[])
10721072
{ /* Loop through tables in list */
10731073
tbl = ((tbl_info *) DLE_VAL(tbl_elem)); /* set tbl_info =
10741074
* current_table */
1075-
if (tbl->relid == atoi(PQgetvalue(res, j, PQfnumber(res, "oid"))))
1075+
if (tbl->relid == atooid(PQgetvalue(res, j, PQfnumber(res, "oid"))))
10761076
{
10771077
tbl->curr_analyze_count =
10781078
(atol(PQgetvalue(res, j, PQfnumber(res, "n_tup_ins"))) +

contrib/pg_autovacuum/pg_autovacuum.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
#define TABLE_STATS_QUERY "select a.oid,a.relname,a.relnamespace,a.relpages,a.relisshared,a.reltuples,b.schemaname,b.n_tup_ins,b.n_tup_upd,b.n_tup_del from pg_class a, pg_stat_all_tables b where a.oid=b.relid and a.relkind = 'r'"
3838

3939
#define FRONTEND
40-
#define PAGES_QUERY "select oid,reltuples,relpages from pg_class where oid=%i"
40+
#define PAGES_QUERY "select oid,reltuples,relpages from pg_class where oid=%u"
4141
#define FROZENOID_QUERY "select oid,age(datfrozenxid) from pg_database where datname = 'template1'"
4242
#define FROZENOID_QUERY2 "select oid,datname,age(datfrozenxid) from pg_database where datname!='template0'"
4343

44+
/* define atooid */
45+
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
46+
4447
/* define cmd_args stucture */
4548
struct cmdargs
4649
{
@@ -67,9 +70,9 @@ cmd_args *args;
6770
I think we need to guarantee this happens approx every 1Million TX's */
6871
struct dbinfo
6972
{
70-
int oid,
71-
age;
72-
int analyze_threshold,
73+
Oid oid;
74+
long age;
75+
long analyze_threshold,
7376
vacuum_threshold; /* Use these as defaults for table
7477
* thresholds */
7578
PGconn *conn;
@@ -84,9 +87,9 @@ struct tableinfo
8487
{
8588
char *schema_name,
8689
*table_name;
87-
int relid,
88-
reltuples,
89-
relisshared,
90+
float reltuples;
91+
int relisshared;
92+
Oid relid,
9093
relpages;
9194
long analyze_threshold,
9295
vacuum_threshold;
@@ -111,7 +114,7 @@ static void usage(void);
111114

112115
/* Functions for managing database lists */
113116
static Dllist *init_db_list(void);
114-
static db_info *init_dbinfo(char *dbname, int oid, int age);
117+
static db_info *init_dbinfo(char *dbname, Oid oid, long age);
115118
static void update_db_list(Dllist *db_list);
116119
static void remove_db_from_list(Dlelem *db_to_remove);
117120
static void print_db_info(db_info * dbi, int print_table_list);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy