~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/bin/scripts/vacuumdb.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * vacuumdb
 
4
 *
 
5
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
6
 * Portions Copyright (c) 1994, Regents of the University of California
 
7
 *
 
8
 * $PostgreSQL: pgsql/src/bin/scripts/vacuumdb.c,v 1.12 2004-12-31 22:03:17 pgsql Exp $
 
9
 *
 
10
 *-------------------------------------------------------------------------
 
11
 */
 
12
 
 
13
#include "postgres_fe.h"
 
14
#include "common.h"
 
15
 
 
16
 
 
17
static void vacuum_one_database(const char *dbname, bool full, bool verbose, bool analyze,
 
18
                                        const char *table,
 
19
                                        const char *host, const char *port,
 
20
                                        const char *username, bool password,
 
21
                                        const char *progname, bool echo, bool quiet);
 
22
static void vacuum_all_databases(bool full, bool verbose, bool analyze,
 
23
                                         const char *host, const char *port,
 
24
                                         const char *username, bool password,
 
25
                                         const char *progname, bool echo, bool quiet);
 
26
 
 
27
static void help(const char *progname);
 
28
 
 
29
 
 
30
int
 
31
main(int argc, char *argv[])
 
32
{
 
33
        static struct option long_options[] = {
 
34
                {"host", required_argument, NULL, 'h'},
 
35
                {"port", required_argument, NULL, 'p'},
 
36
                {"username", required_argument, NULL, 'U'},
 
37
                {"password", no_argument, NULL, 'W'},
 
38
                {"echo", no_argument, NULL, 'e'},
 
39
                {"quiet", no_argument, NULL, 'q'},
 
40
                {"dbname", required_argument, NULL, 'd'},
 
41
                {"analyze", no_argument, NULL, 'z'},
 
42
                {"all", no_argument, NULL, 'a'},
 
43
                {"table", required_argument, NULL, 't'},
 
44
                {"full", no_argument, NULL, 'f'},
 
45
                {"verbose", no_argument, NULL, 'v'},
 
46
                {NULL, 0, NULL, 0}
 
47
        };
 
48
 
 
49
        const char *progname;
 
50
        int                     optindex;
 
51
        int                     c;
 
52
 
 
53
        const char *dbname = NULL;
 
54
        char       *host = NULL;
 
55
        char       *port = NULL;
 
56
        char       *username = NULL;
 
57
        bool            password = false;
 
58
        bool            echo = false;
 
59
        bool            quiet = false;
 
60
        bool            analyze = false;
 
61
        bool            alldb = false;
 
62
        char       *table = NULL;
 
63
        bool            full = false;
 
64
        bool            verbose = false;
 
65
 
 
66
        progname = get_progname(argv[0]);
 
67
        set_pglocale_pgservice(argv[0], "pgscripts");
 
68
 
 
69
        handle_help_version_opts(argc, argv, "vacuumdb", help);
 
70
 
 
71
        while ((c = getopt_long(argc, argv, "h:p:U:Weqd:zat:fv", long_options, &optindex)) != -1)
 
72
        {
 
73
                switch (c)
 
74
                {
 
75
                        case 'h':
 
76
                                host = optarg;
 
77
                                break;
 
78
                        case 'p':
 
79
                                port = optarg;
 
80
                                break;
 
81
                        case 'U':
 
82
                                username = optarg;
 
83
                                break;
 
84
                        case 'W':
 
85
                                password = true;
 
86
                                break;
 
87
                        case 'e':
 
88
                                echo = true;
 
89
                                break;
 
90
                        case 'q':
 
91
                                quiet = true;
 
92
                                break;
 
93
                        case 'd':
 
94
                                dbname = optarg;
 
95
                                break;
 
96
                        case 'z':
 
97
                                analyze = true;
 
98
                                break;
 
99
                        case 'a':
 
100
                                alldb = true;
 
101
                                break;
 
102
                        case 't':
 
103
                                table = optarg;
 
104
                                break;
 
105
                        case 'f':
 
106
                                full = true;
 
107
                                break;
 
108
                        case 'v':
 
109
                                verbose = true;
 
110
                                break;
 
111
                        default:
 
112
                                fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
113
                                exit(1);
 
114
                }
 
115
        }
 
116
 
 
117
        switch (argc - optind)
 
118
        {
 
119
                case 0:
 
120
                        break;
 
121
                case 1:
 
122
                        dbname = argv[optind];
 
123
                        break;
 
124
                default:
 
125
                        fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
 
126
                                        progname, argv[optind + 1]);
 
127
                        fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
 
128
                        exit(1);
 
129
        }
 
130
 
 
131
        if (alldb)
 
132
        {
 
133
                if (dbname)
 
134
                {
 
135
                        fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"),
 
136
                                        progname);
 
137
                        exit(1);
 
138
                }
 
139
                if (table)
 
140
                {
 
141
                        fprintf(stderr, _("%s: cannot vacuum a specific table in all databases\n"),
 
142
                                        progname);
 
143
                        exit(1);
 
144
                }
 
145
 
 
146
                vacuum_all_databases(full, verbose, analyze,
 
147
                                                         host, port, username, password,
 
148
                                                         progname, echo, quiet);
 
149
        }
 
150
        else
 
151
        {
 
152
                if (dbname == NULL)
 
153
                {
 
154
                        if (getenv("PGDATABASE"))
 
155
                                dbname = getenv("PGDATABASE");
 
156
                        else if (getenv("PGUSER"))
 
157
                                dbname = getenv("PGUSER");
 
158
                        else
 
159
                                dbname = get_user_name(progname);
 
160
                }
 
161
 
 
162
                vacuum_one_database(dbname, full, verbose, analyze, table,
 
163
                                                        host, port, username, password,
 
164
                                                        progname, echo, quiet);
 
165
        }
 
166
 
 
167
        exit(0);
 
168
}
 
169
 
 
170
 
 
171
static void
 
172
vacuum_one_database(const char *dbname, bool full, bool verbose, bool analyze,
 
173
                                        const char *table,
 
174
                                        const char *host, const char *port,
 
175
                                        const char *username, bool password,
 
176
                                        const char *progname, bool echo, bool quiet)
 
177
{
 
178
        PQExpBufferData sql;
 
179
 
 
180
        PGconn     *conn;
 
181
        PGresult   *result;
 
182
 
 
183
        initPQExpBuffer(&sql);
 
184
 
 
185
        appendPQExpBuffer(&sql, "VACUUM");
 
186
        if (full)
 
187
                appendPQExpBuffer(&sql, " FULL");
 
188
        if (verbose)
 
189
                appendPQExpBuffer(&sql, " VERBOSE");
 
190
        if (analyze)
 
191
                appendPQExpBuffer(&sql, " ANALYZE");
 
192
        if (table)
 
193
                appendPQExpBuffer(&sql, " %s", table);
 
194
        appendPQExpBuffer(&sql, ";\n");
 
195
 
 
196
        conn = connectDatabase(dbname, host, port, username, password, progname);
 
197
 
 
198
        if (echo)
 
199
                printf("%s", sql.data);
 
200
        result = PQexec(conn, sql.data);
 
201
 
 
202
        if (PQresultStatus(result) != PGRES_COMMAND_OK)
 
203
        {
 
204
                if (table)
 
205
                        fprintf(stderr, _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"),
 
206
                                        progname, table, dbname, PQerrorMessage(conn));
 
207
                else
 
208
                        fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
 
209
                                        progname, dbname, PQerrorMessage(conn));
 
210
                PQfinish(conn);
 
211
                exit(1);
 
212
        }
 
213
 
 
214
        PQclear(result);
 
215
        PQfinish(conn);
 
216
        termPQExpBuffer(&sql);
 
217
 
 
218
        if (!quiet)
 
219
        {
 
220
                puts("VACUUM");
 
221
                fflush(stdout);
 
222
        }
 
223
}
 
224
 
 
225
 
 
226
static void
 
227
vacuum_all_databases(bool full, bool verbose, bool analyze,
 
228
                                         const char *host, const char *port,
 
229
                                         const char *username, bool password,
 
230
                                         const char *progname, bool echo, bool quiet)
 
231
{
 
232
        PGconn     *conn;
 
233
        PGresult   *result;
 
234
        int                     i;
 
235
 
 
236
        conn = connectDatabase("template1", host, port, username, password, progname);
 
237
        result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn;", progname, echo);
 
238
        PQfinish(conn);
 
239
 
 
240
        for (i = 0; i < PQntuples(result); i++)
 
241
        {
 
242
                char       *dbname = PQgetvalue(result, i, 0);
 
243
 
 
244
                if (!quiet)
 
245
                        fprintf(stderr, _("%s: vacuuming database \"%s\"\n"), progname, dbname);
 
246
 
 
247
                vacuum_one_database(dbname, full, verbose, analyze, NULL,
 
248
                                                        host, port, username, password,
 
249
                                                        progname, echo, quiet);
 
250
        }
 
251
 
 
252
        PQclear(result);
 
253
}
 
254
 
 
255
 
 
256
static void
 
257
help(const char *progname)
 
258
{
 
259
        printf(_("%s cleans and analyzes a PostgreSQL database.\n\n"), progname);
 
260
        printf(_("Usage:\n"));
 
261
        printf(_("  %s [OPTION]... [DBNAME]\n"), progname);
 
262
        printf(_("\nOptions:\n"));
 
263
        printf(_("  -a, --all                       vacuum all databases\n"));
 
264
        printf(_("  -d, --dbname=DBNAME             database to vacuum\n"));
 
265
        printf(_("  -t, --table='TABLE[(COLUMNS)]'  vacuum specific table only\n"));
 
266
        printf(_("  -f, --full                      do full vacuuming\n"));
 
267
        printf(_("  -z, --analyze                   update optimizer hints\n"));
 
268
        printf(_("  -e, --echo                      show the commands being sent to the server\n"));
 
269
        printf(_("  -q, --quiet                     don't write any messages\n"));
 
270
        printf(_("  -v, --verbose                   write a lot of output\n"));
 
271
        printf(_("  --help                          show this help, then exit\n"));
 
272
        printf(_("  --version                       output version information, then exit\n"));
 
273
        printf(_("\nConnection options:\n"));
 
274
        printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
 
275
        printf(_("  -p, --port=PORT           database server port\n"));
 
276
        printf(_("  -U, --username=USERNAME   user name to connect as\n"));
 
277
        printf(_("  -W, --password            prompt for password\n"));
 
278
        printf(_("\nRead the description of the SQL command VACUUM for details.\n"));
 
279
        printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
 
280
}