1999-04-11 00:48:05 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* vacuumlo.c
|
|
|
|
* This removes orphaned large objects from a database.
|
|
|
|
*
|
2012-01-02 07:01:58 +08:00
|
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
2000-01-26 13:58:53 +08:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-04-11 00:48:05 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/vacuumlo/vacuumlo.c
|
1999-04-11 00:48:05 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2002-09-06 05:19:13 +08:00
|
|
|
#include "postgres_fe.h"
|
2002-04-24 10:45:51 +08:00
|
|
|
|
1999-04-11 00:48:05 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2002-09-06 05:19:13 +08:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
1999-04-11 00:48:05 +08:00
|
|
|
|
|
|
|
#include "libpq-fe.h"
|
|
|
|
|
2000-11-22 01:54:21 +08:00
|
|
|
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
|
|
|
|
|
1999-04-11 00:48:05 +08:00
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
extern char *optarg;
|
2002-09-05 04:31:48 +08:00
|
|
|
extern int optind,
|
|
|
|
opterr,
|
|
|
|
optopt;
|
|
|
|
|
2009-02-27 00:02:39 +08:00
|
|
|
enum trivalue
|
|
|
|
{
|
|
|
|
TRI_DEFAULT,
|
|
|
|
TRI_NO,
|
|
|
|
TRI_YES
|
|
|
|
};
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
struct _param
|
|
|
|
{
|
|
|
|
char *pg_user;
|
2009-02-27 00:02:39 +08:00
|
|
|
enum trivalue pg_prompt;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *pg_port;
|
|
|
|
char *pg_host;
|
|
|
|
int verbose;
|
|
|
|
int dry_run;
|
2011-08-08 21:12:16 +08:00
|
|
|
long transaction_limit;
|
2002-04-24 10:45:51 +08:00
|
|
|
};
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
int vacuumlo(char *, struct _param *);
|
2009-02-27 17:30:21 +08:00
|
|
|
void usage(const char *progname);
|
2002-04-24 10:45:51 +08:00
|
|
|
|
|
|
|
|
1999-04-11 00:48:05 +08:00
|
|
|
|
|
|
|
/*
|
2000-11-22 01:54:21 +08:00
|
|
|
* This vacuums LOs of one database. It returns 0 on success, -1 on failure.
|
1999-04-11 00:48:05 +08:00
|
|
|
*/
|
1999-05-26 00:15:34 +08:00
|
|
|
int
|
2002-09-05 04:31:48 +08:00
|
|
|
vacuumlo(char *database, struct _param * param)
|
1999-04-11 00:48:05 +08:00
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
PGconn *conn;
|
|
|
|
PGresult *res,
|
2002-09-05 04:31:48 +08:00
|
|
|
*res2;
|
1999-05-26 00:15:34 +08:00
|
|
|
char buf[BUFSIZE];
|
2011-08-08 21:12:16 +08:00
|
|
|
long matched;
|
|
|
|
long deleted;
|
2002-09-05 04:31:48 +08:00
|
|
|
int i;
|
2007-12-11 10:08:59 +08:00
|
|
|
static char *password = NULL;
|
|
|
|
bool new_pass;
|
2011-08-08 21:12:16 +08:00
|
|
|
bool success = true;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2009-02-27 00:02:39 +08:00
|
|
|
if (param->pg_prompt == TRI_YES && password == NULL)
|
2007-12-11 10:08:59 +08:00
|
|
|
password = simple_prompt("Password: ", 100, false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the connection. Loop until we have a password if requested by
|
|
|
|
* backend.
|
|
|
|
*/
|
|
|
|
do
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2007-12-11 10:08:59 +08:00
|
|
|
new_pass = false;
|
|
|
|
|
|
|
|
conn = PQsetdbLogin(param->pg_host,
|
|
|
|
param->pg_port,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
database,
|
|
|
|
param->pg_user,
|
|
|
|
password);
|
|
|
|
if (!conn)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2007-12-11 10:08:59 +08:00
|
|
|
fprintf(stderr, "Connection to database \"%s\" failed\n",
|
|
|
|
database);
|
|
|
|
return -1;
|
2002-04-24 10:45:51 +08:00
|
|
|
}
|
|
|
|
|
2007-12-11 10:08:59 +08:00
|
|
|
if (PQstatus(conn) == CONNECTION_BAD &&
|
|
|
|
PQconnectionNeedsPassword(conn) &&
|
2009-02-27 00:02:39 +08:00
|
|
|
password == NULL &&
|
|
|
|
param->pg_prompt != TRI_NO)
|
2007-12-11 10:08:59 +08:00
|
|
|
{
|
|
|
|
PQfinish(conn);
|
|
|
|
password = simple_prompt("Password: ", 100, false);
|
|
|
|
new_pass = true;
|
|
|
|
}
|
|
|
|
} while (new_pass);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
|
|
|
/* check to see that the backend connection was successfully made */
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD)
|
1999-04-11 00:48:05 +08:00
|
|
|
{
|
2007-12-11 10:08:59 +08:00
|
|
|
fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
|
|
|
|
database, PQerrorMessage(conn));
|
2000-11-22 01:54:21 +08:00
|
|
|
PQfinish(conn);
|
1999-05-26 00:15:34 +08:00
|
|
|
return -1;
|
1999-04-11 00:48:05 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (param->verbose)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
fprintf(stdout, "Connected to %s\n", database);
|
2002-09-05 04:31:48 +08:00
|
|
|
if (param->dry_run)
|
|
|
|
fprintf(stdout, "Test run: no large objects will be removed!\n");
|
2002-04-24 10:45:51 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2003-09-24 13:38:38 +08:00
|
|
|
/*
|
|
|
|
* Don't get fooled by any non-system catalogs
|
|
|
|
*/
|
|
|
|
res = PQexec(conn, "SET search_path = pg_catalog");
|
2002-10-19 02:41:22 +08:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2003-09-24 13:38:38 +08:00
|
|
|
fprintf(stderr, "Failed to set search_path:\n");
|
2002-10-19 02:41:22 +08:00
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
/*
|
2000-11-22 01:54:21 +08:00
|
|
|
* First we create and populate the LO temp table
|
1999-05-26 00:15:34 +08:00
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
2003-09-24 13:38:38 +08:00
|
|
|
strcat(buf, "CREATE TEMP TABLE vacuum_l AS ");
|
2010-02-17 12:19:41 +08:00
|
|
|
if (PQserverVersion(conn) >= 90000)
|
2009-12-14 08:39:11 +08:00
|
|
|
strcat(buf, "SELECT oid AS lo FROM pg_largeobject_metadata");
|
|
|
|
else
|
|
|
|
strcat(buf, "SELECT DISTINCT loid AS lo FROM pg_largeobject");
|
2000-11-22 01:54:21 +08:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to create temp table:\n");
|
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
2001-03-22 12:01:46 +08:00
|
|
|
|
2000-11-22 01:54:21 +08:00
|
|
|
/*
|
2009-07-14 06:56:30 +08:00
|
|
|
* Analyze the temp table so that planner will generate decent plans for
|
2005-10-15 10:49:52 +08:00
|
|
|
* the DELETEs below.
|
2000-11-22 01:54:21 +08:00
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
2009-07-14 06:56:30 +08:00
|
|
|
strcat(buf, "ANALYZE vacuum_l");
|
2000-11-22 01:54:21 +08:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
1999-04-11 00:48:05 +08:00
|
|
|
{
|
2000-11-22 01:54:21 +08:00
|
|
|
fprintf(stderr, "Failed to vacuum temp table:\n");
|
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
|
|
PQclear(res);
|
1999-04-11 00:48:05 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
1999-05-26 00:15:34 +08:00
|
|
|
}
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/*
|
2003-09-24 13:38:38 +08:00
|
|
|
* Now find any candidate tables that have columns of type oid.
|
2000-11-22 01:54:21 +08:00
|
|
|
*
|
2003-08-05 06:03:39 +08:00
|
|
|
* NOTE: we ignore system tables and temp tables by the expedient of
|
|
|
|
* rejecting tables in schemas named 'pg_*'. In particular, the temp
|
2003-08-09 05:42:59 +08:00
|
|
|
* table formed above is ignored, and pg_largeobject will be too. If
|
|
|
|
* either of these were scanned, obviously we'd end up with nothing to
|
|
|
|
* delete...
|
2000-11-22 01:54:21 +08:00
|
|
|
*
|
2001-03-22 12:01:46 +08:00
|
|
|
* NOTE: the system oid column is ignored, as it has attnum < 1. This
|
|
|
|
* shouldn't matter for correctness, but it saves time.
|
1999-05-26 00:15:34 +08:00
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
2003-09-24 13:38:38 +08:00
|
|
|
strcat(buf, "SELECT s.nspname, c.relname, a.attname ");
|
2003-08-05 06:03:39 +08:00
|
|
|
strcat(buf, "FROM pg_class c, pg_attribute a, pg_namespace s, pg_type t ");
|
2003-09-24 13:38:38 +08:00
|
|
|
strcat(buf, "WHERE a.attnum > 0 AND NOT a.attisdropped ");
|
1999-05-26 00:15:34 +08:00
|
|
|
strcat(buf, " AND a.attrelid = c.oid ");
|
|
|
|
strcat(buf, " AND a.atttypid = t.oid ");
|
2003-08-05 06:03:39 +08:00
|
|
|
strcat(buf, " AND c.relnamespace = s.oid ");
|
2001-09-17 10:30:54 +08:00
|
|
|
strcat(buf, " AND t.typname in ('oid', 'lo') ");
|
2000-11-22 01:54:21 +08:00
|
|
|
strcat(buf, " AND c.relkind = 'r'");
|
2006-02-24 06:33:59 +08:00
|
|
|
strcat(buf, " AND s.nspname !~ '^pg_'");
|
2000-11-22 01:54:21 +08:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
2000-11-22 01:54:21 +08:00
|
|
|
fprintf(stderr, "Failed to find OID columns:\n");
|
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
|
|
PQclear(res);
|
1999-04-11 00:48:05 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
1999-05-26 00:15:34 +08:00
|
|
|
}
|
2000-11-22 01:54:21 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
for (i = 0; i < PQntuples(res); i++)
|
|
|
|
{
|
2003-09-24 13:38:38 +08:00
|
|
|
char *schema,
|
|
|
|
*table,
|
1999-05-26 00:15:34 +08:00
|
|
|
*field;
|
|
|
|
|
2003-09-24 13:38:38 +08:00
|
|
|
schema = PQgetvalue(res, i, 0);
|
|
|
|
table = PQgetvalue(res, i, 1);
|
|
|
|
field = PQgetvalue(res, i, 2);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
if (param->verbose)
|
2003-09-24 13:38:38 +08:00
|
|
|
fprintf(stdout, "Checking %s in %s.%s\n", field, schema, table);
|
2000-11-22 01:54:21 +08:00
|
|
|
|
2003-08-05 06:03:39 +08:00
|
|
|
snprintf(buf, BUFSIZE,
|
|
|
|
"DELETE FROM vacuum_l "
|
2003-09-24 13:38:38 +08:00
|
|
|
"WHERE lo IN (SELECT \"%s\" FROM \"%s\".\"%s\")",
|
|
|
|
field, schema, table);
|
2000-11-22 01:54:21 +08:00
|
|
|
res2 = PQexec(conn, buf);
|
1999-05-26 00:15:34 +08:00
|
|
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2003-09-24 13:38:38 +08:00
|
|
|
fprintf(stderr, "Failed to check %s in table %s.%s:\n",
|
|
|
|
field, schema, table);
|
2000-11-22 01:54:21 +08:00
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
1999-05-26 00:15:34 +08:00
|
|
|
PQclear(res2);
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PQclear(res2);
|
1999-04-11 00:48:05 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
PQclear(res);
|
|
|
|
|
2000-11-22 01:54:21 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Run the actual deletes in a single transaction. Note that this would
|
|
|
|
* be a bad idea in pre-7.1 Postgres releases (since rolling back a table
|
|
|
|
* delete used to cause problems), but it should be safe now.
|
2000-11-22 01:54:21 +08:00
|
|
|
*/
|
1999-05-26 00:15:34 +08:00
|
|
|
res = PQexec(conn, "begin");
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally, those entries remaining in vacuum_l are orphans.
|
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
|
|
|
strcat(buf, "SELECT lo ");
|
|
|
|
strcat(buf, "FROM vacuum_l");
|
2000-11-22 01:54:21 +08:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
1999-04-11 00:48:05 +08:00
|
|
|
{
|
2000-11-22 01:54:21 +08:00
|
|
|
fprintf(stderr, "Failed to read temp table:\n");
|
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
|
|
PQclear(res);
|
1999-05-26 00:15:34 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-11-22 01:54:21 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
matched = PQntuples(res);
|
2000-11-22 01:54:21 +08:00
|
|
|
deleted = 0;
|
1999-05-26 00:15:34 +08:00
|
|
|
for (i = 0; i < matched; i++)
|
|
|
|
{
|
2000-11-22 01:54:21 +08:00
|
|
|
Oid lo = atooid(PQgetvalue(res, i, 0));
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
if (param->verbose)
|
1999-05-26 00:15:34 +08:00
|
|
|
{
|
2000-11-22 01:54:21 +08:00
|
|
|
fprintf(stdout, "\rRemoving lo %6u ", lo);
|
1999-05-26 00:15:34 +08:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (param->dry_run == 0)
|
|
|
|
{
|
|
|
|
if (lo_unlink(conn, lo) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\nFailed to remove lo %u: ", lo);
|
|
|
|
fprintf(stderr, "%s", PQerrorMessage(conn));
|
2011-08-08 21:12:16 +08:00
|
|
|
if (PQtransactionStatus(conn) == PQTRANS_INERROR)
|
|
|
|
{
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
deleted++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
deleted++;
|
2011-08-08 21:12:16 +08:00
|
|
|
if (param->transaction_limit != 0 && deleted >= param->transaction_limit)
|
|
|
|
break;
|
1999-04-11 00:48:05 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* That's all folks!
|
|
|
|
*/
|
|
|
|
res = PQexec(conn, "end");
|
|
|
|
PQclear(res);
|
2000-11-22 01:54:21 +08:00
|
|
|
|
1999-05-26 00:15:34 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
if (param->verbose)
|
2011-08-08 21:12:16 +08:00
|
|
|
{
|
|
|
|
if (param->dry_run)
|
|
|
|
fprintf(stdout, "\rWould remove %ld large objects from %s.\n",
|
|
|
|
deleted, database);
|
|
|
|
else if (success)
|
|
|
|
fprintf(stdout,
|
|
|
|
"\rSuccessfully removed %ld large objects from %s.\n",
|
|
|
|
deleted, database);
|
|
|
|
else
|
|
|
|
fprintf(stdout, "\rRemoval from %s failed at object %ld of %ld.\n",
|
|
|
|
database, deleted, matched);
|
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2011-08-08 21:12:16 +08:00
|
|
|
return ((param->dry_run || success) ? 0 : -1);
|
1999-04-11 00:48:05 +08:00
|
|
|
}
|
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
void
|
2009-02-27 17:30:21 +08:00
|
|
|
usage(const char *progname)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2009-02-27 17:30:21 +08:00
|
|
|
printf("%s removes unreferenced large objects from databases.\n\n", progname);
|
|
|
|
printf("Usage:\n %s [OPTION]... DBNAME...\n\n", progname);
|
2009-02-25 21:34:32 +08:00
|
|
|
printf("Options:\n");
|
|
|
|
printf(" -h HOSTNAME database server host or socket directory\n");
|
2011-08-08 21:12:16 +08:00
|
|
|
printf(" -l LIMIT stop after removing LIMIT large objects\n");
|
2009-02-25 21:34:32 +08:00
|
|
|
printf(" -n don't remove large objects, just show what would be done\n");
|
|
|
|
printf(" -p PORT database server port\n");
|
|
|
|
printf(" -U USERNAME user name to connect as\n");
|
2009-02-27 00:02:39 +08:00
|
|
|
printf(" -w never prompt for password\n");
|
2009-02-25 21:34:32 +08:00
|
|
|
printf(" -W force password prompt\n");
|
|
|
|
printf(" -v write a lot of progress messages\n");
|
2009-02-27 17:30:21 +08:00
|
|
|
printf(" --help show this help, then exit\n");
|
|
|
|
printf(" --version output version information, then exit\n");
|
2009-02-25 21:34:32 +08:00
|
|
|
printf("\n");
|
2009-02-27 17:30:21 +08:00
|
|
|
printf("Report bugs to <pgsql-bugs@postgresql.org>.\n");
|
2002-04-24 10:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-11 00:48:05 +08:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
1999-05-26 00:15:34 +08:00
|
|
|
int rc = 0;
|
2002-09-05 04:31:48 +08:00
|
|
|
struct _param param;
|
|
|
|
int c;
|
|
|
|
int port;
|
2009-02-27 17:30:21 +08:00
|
|
|
const char *progname;
|
|
|
|
|
|
|
|
progname = get_progname(argv[0]);
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
/* Parameter handling */
|
2002-09-05 04:31:48 +08:00
|
|
|
param.pg_user = NULL;
|
2009-02-27 00:02:39 +08:00
|
|
|
param.pg_prompt = TRI_DEFAULT;
|
2002-04-24 10:45:51 +08:00
|
|
|
param.pg_host = NULL;
|
2004-11-09 14:09:40 +08:00
|
|
|
param.pg_port = NULL;
|
2002-09-05 04:31:48 +08:00
|
|
|
param.verbose = 0;
|
2002-04-24 10:45:51 +08:00
|
|
|
param.dry_run = 0;
|
2011-08-08 21:12:16 +08:00
|
|
|
param.transaction_limit = 0;
|
2002-04-24 10:45:51 +08:00
|
|
|
|
2009-02-27 17:30:21 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
|
|
|
{
|
|
|
|
usage(progname);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
|
|
{
|
|
|
|
puts("vacuumlo (PostgreSQL) " PG_VERSION);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
while (1)
|
|
|
|
{
|
2011-08-08 21:12:16 +08:00
|
|
|
c = getopt(argc, argv, "h:l:U:p:vnwW");
|
2002-09-05 04:31:48 +08:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '?':
|
2009-02-27 17:30:21 +08:00
|
|
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
2002-09-05 04:31:48 +08:00
|
|
|
exit(1);
|
|
|
|
case ':':
|
|
|
|
exit(1);
|
|
|
|
case 'v':
|
|
|
|
param.verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
param.dry_run = 1;
|
|
|
|
param.verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
param.pg_user = strdup(optarg);
|
|
|
|
break;
|
2009-02-27 00:02:39 +08:00
|
|
|
case 'w':
|
|
|
|
param.pg_prompt = TRI_NO;
|
|
|
|
break;
|
2002-09-05 04:31:48 +08:00
|
|
|
case 'W':
|
2009-02-27 00:02:39 +08:00
|
|
|
param.pg_prompt = TRI_YES;
|
2002-09-05 04:31:48 +08:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port = strtol(optarg, NULL, 10);
|
|
|
|
if ((port < 1) || (port > 65535))
|
|
|
|
{
|
2009-02-27 17:30:21 +08:00
|
|
|
fprintf(stderr, "%s: invalid port number: %s\n", progname, optarg);
|
2002-09-05 04:31:48 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
param.pg_port = strdup(optarg);
|
|
|
|
break;
|
2011-08-08 21:12:16 +08:00
|
|
|
case 'l':
|
|
|
|
param.transaction_limit = strtol(optarg, NULL, 10);
|
|
|
|
if (param.transaction_limit < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: transaction limit must not be negative (0 disables)\n",
|
|
|
|
progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
2002-09-05 04:31:48 +08:00
|
|
|
case 'h':
|
|
|
|
param.pg_host = strdup(optarg);
|
|
|
|
break;
|
|
|
|
}
|
2002-04-24 10:45:51 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2002-04-24 10:45:51 +08:00
|
|
|
/* No database given? Show usage */
|
2002-12-03 15:12:18 +08:00
|
|
|
if (optind >= argc)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
fprintf(stderr, "vacuumlo: missing required argument: database name\n");
|
|
|
|
fprintf(stderr, "Try 'vacuumlo -?' for help.\n");
|
|
|
|
exit(1);
|
1999-04-11 00:48:05 +08:00
|
|
|
}
|
1999-05-26 00:15:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
for (c = optind; c < argc; c++)
|
|
|
|
{
|
|
|
|
/* Work on selected database */
|
|
|
|
rc += (vacuumlo(argv[c], ¶m) != 0);
|
1999-05-26 00:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
1999-04-11 00:48:05 +08:00
|
|
|
}
|