1999-04-10 16:48:05 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* vacuumlo.c
|
|
|
|
* This removes orphaned large objects from a database.
|
|
|
|
*
|
2025-01-01 11:21:55 -05:00
|
|
|
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
|
2000-01-26 05:58:53 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-04-10 16:48:05 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/vacuumlo/vacuumlo.c
|
1999-04-10 16:48:05 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2002-09-05 21:19:13 +00:00
|
|
|
#include "postgres_fe.h"
|
2002-04-24 02:45:51 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2002-09-05 21:19:13 +00:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
1999-04-10 16:48:05 +00:00
|
|
|
|
2018-04-08 13:59:52 -04:00
|
|
|
#include "catalog/pg_class_d.h"
|
2020-08-10 09:22:54 -07:00
|
|
|
#include "common/connect.h"
|
2019-09-06 14:00:13 +09:00
|
|
|
#include "common/logging.h"
|
Remove arbitrary restrictions on password length.
This patch started out with the goal of harmonizing various arbitrary
limits on password length, but after awhile a better idea emerged:
let's just get rid of those fixed limits.
recv_password_packet() has an arbitrary limit on the packet size,
which we don't really need, so just drop it. (Note that this doesn't
really affect anything for MD5 or SCRAM password verification, since
those will hash the user's password to something shorter anyway.
It does matter for auth methods that require a cleartext password.)
Likewise remove the arbitrary error condition in pg_saslprep().
The remaining limits are mostly in client-side code that prompts
for passwords. To improve those, refactor simple_prompt() so that
it allocates its own result buffer that can be made as big as
necessary. Actually, it proves best to make a separate routine
pg_get_line() that has essentially the semantics of fgets(), except
that it allocates a suitable result buffer and hence will never
return a truncated line. (pg_get_line has a lot of potential
applications to replace randomly-sized fgets buffers elsewhere,
but I'll leave that for another patch.)
I built pg_get_line() atop stringinfo.c, which requires moving
that code to src/common/; but that seems fine since it was a poor
fit for src/port/ anyway.
This patch is mostly mine, but it owes a good deal to Nathan Bossart
who pressed for a solution to the password length problem and
created a predecessor patch. Also thanks to Peter Eisentraut and
Stephen Frost for ideas and discussion.
Discussion: https://postgr.es/m/09512C4F-8CB9-4021-B455-EF4C4F0D55A0@amazon.com
2020-09-03 20:09:18 -04:00
|
|
|
#include "common/string.h"
|
2019-10-23 09:26:22 +05:30
|
|
|
#include "getopt_long.h"
|
1999-04-10 16:48:05 +00:00
|
|
|
#include "libpq-fe.h"
|
2014-02-15 14:31:30 -05:00
|
|
|
#include "pg_getopt.h"
|
1999-04-10 16:48:05 +00:00
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
2009-02-26 16:02:39 +00:00
|
|
|
enum trivalue
|
|
|
|
{
|
|
|
|
TRI_DEFAULT,
|
|
|
|
TRI_NO,
|
|
|
|
TRI_YES,
|
|
|
|
};
|
|
|
|
|
2002-04-24 02:45:51 +00:00
|
|
|
struct _param
|
|
|
|
{
|
|
|
|
char *pg_user;
|
2009-02-26 16:02:39 +00:00
|
|
|
enum trivalue pg_prompt;
|
2002-04-24 02:45:51 +00:00
|
|
|
char *pg_port;
|
|
|
|
char *pg_host;
|
2012-07-04 15:39:33 -04:00
|
|
|
const char *progname;
|
2002-04-24 02:45:51 +00:00
|
|
|
int verbose;
|
|
|
|
int dry_run;
|
2011-08-08 09:12:16 -04:00
|
|
|
long transaction_limit;
|
2002-04-24 02:45:51 +00:00
|
|
|
};
|
|
|
|
|
2012-03-20 19:05:08 -04:00
|
|
|
static int vacuumlo(const char *database, const struct _param *param);
|
|
|
|
static void usage(const char *progname);
|
2002-04-24 02:45:51 +00:00
|
|
|
|
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
|
|
|
|
/*
|
2000-11-21 17:54:21 +00:00
|
|
|
* This vacuums LOs of one database. It returns 0 on success, -1 on failure.
|
1999-04-10 16:48:05 +00:00
|
|
|
*/
|
2012-03-20 19:05:08 -04:00
|
|
|
static int
|
|
|
|
vacuumlo(const char *database, const struct _param *param)
|
1999-04-10 16:48:05 +00:00
|
|
|
{
|
|
|
|
PGconn *conn;
|
|
|
|
PGresult *res,
|
2002-04-24 02:45:51 +00:00
|
|
|
*res2;
|
1999-04-10 16:48:05 +00:00
|
|
|
char buf[BUFSIZE];
|
2011-08-08 09:12:16 -04:00
|
|
|
long matched;
|
|
|
|
long deleted;
|
2002-04-24 02:45:51 +00:00
|
|
|
int i;
|
2007-12-11 02:08:59 +00:00
|
|
|
bool new_pass;
|
2011-08-08 09:12:16 -04:00
|
|
|
bool success = true;
|
Remove arbitrary restrictions on password length.
This patch started out with the goal of harmonizing various arbitrary
limits on password length, but after awhile a better idea emerged:
let's just get rid of those fixed limits.
recv_password_packet() has an arbitrary limit on the packet size,
which we don't really need, so just drop it. (Note that this doesn't
really affect anything for MD5 or SCRAM password verification, since
those will hash the user's password to something shorter anyway.
It does matter for auth methods that require a cleartext password.)
Likewise remove the arbitrary error condition in pg_saslprep().
The remaining limits are mostly in client-side code that prompts
for passwords. To improve those, refactor simple_prompt() so that
it allocates its own result buffer that can be made as big as
necessary. Actually, it proves best to make a separate routine
pg_get_line() that has essentially the semantics of fgets(), except
that it allocates a suitable result buffer and hence will never
return a truncated line. (pg_get_line has a lot of potential
applications to replace randomly-sized fgets buffers elsewhere,
but I'll leave that for another patch.)
I built pg_get_line() atop stringinfo.c, which requires moving
that code to src/common/; but that seems fine since it was a poor
fit for src/port/ anyway.
This patch is mostly mine, but it owes a good deal to Nathan Bossart
who pressed for a solution to the password length problem and
created a predecessor patch. Also thanks to Peter Eisentraut and
Stephen Frost for ideas and discussion.
Discussion: https://postgr.es/m/09512C4F-8CB9-4021-B455-EF4C4F0D55A0@amazon.com
2020-09-03 20:09:18 -04:00
|
|
|
static char *password = NULL;
|
2002-09-04 20:31:48 +00:00
|
|
|
|
2012-03-20 19:05:08 -04:00
|
|
|
/* Note: password can be carried over from a previous call */
|
Remove arbitrary restrictions on password length.
This patch started out with the goal of harmonizing various arbitrary
limits on password length, but after awhile a better idea emerged:
let's just get rid of those fixed limits.
recv_password_packet() has an arbitrary limit on the packet size,
which we don't really need, so just drop it. (Note that this doesn't
really affect anything for MD5 or SCRAM password verification, since
those will hash the user's password to something shorter anyway.
It does matter for auth methods that require a cleartext password.)
Likewise remove the arbitrary error condition in pg_saslprep().
The remaining limits are mostly in client-side code that prompts
for passwords. To improve those, refactor simple_prompt() so that
it allocates its own result buffer that can be made as big as
necessary. Actually, it proves best to make a separate routine
pg_get_line() that has essentially the semantics of fgets(), except
that it allocates a suitable result buffer and hence will never
return a truncated line. (pg_get_line has a lot of potential
applications to replace randomly-sized fgets buffers elsewhere,
but I'll leave that for another patch.)
I built pg_get_line() atop stringinfo.c, which requires moving
that code to src/common/; but that seems fine since it was a poor
fit for src/port/ anyway.
This patch is mostly mine, but it owes a good deal to Nathan Bossart
who pressed for a solution to the password length problem and
created a predecessor patch. Also thanks to Peter Eisentraut and
Stephen Frost for ideas and discussion.
Discussion: https://postgr.es/m/09512C4F-8CB9-4021-B455-EF4C4F0D55A0@amazon.com
2020-09-03 20:09:18 -04:00
|
|
|
if (param->pg_prompt == TRI_YES && !password)
|
|
|
|
password = simple_prompt("Password: ", false);
|
2007-12-11 02:08:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Start the connection. Loop until we have a password if requested by
|
|
|
|
* backend.
|
|
|
|
*/
|
|
|
|
do
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
2012-07-04 15:39:33 -04:00
|
|
|
#define PARAMS_ARRAY_SIZE 7
|
|
|
|
|
|
|
|
const char *keywords[PARAMS_ARRAY_SIZE];
|
|
|
|
const char *values[PARAMS_ARRAY_SIZE];
|
|
|
|
|
|
|
|
keywords[0] = "host";
|
|
|
|
values[0] = param->pg_host;
|
|
|
|
keywords[1] = "port";
|
|
|
|
values[1] = param->pg_port;
|
|
|
|
keywords[2] = "user";
|
|
|
|
values[2] = param->pg_user;
|
|
|
|
keywords[3] = "password";
|
Remove arbitrary restrictions on password length.
This patch started out with the goal of harmonizing various arbitrary
limits on password length, but after awhile a better idea emerged:
let's just get rid of those fixed limits.
recv_password_packet() has an arbitrary limit on the packet size,
which we don't really need, so just drop it. (Note that this doesn't
really affect anything for MD5 or SCRAM password verification, since
those will hash the user's password to something shorter anyway.
It does matter for auth methods that require a cleartext password.)
Likewise remove the arbitrary error condition in pg_saslprep().
The remaining limits are mostly in client-side code that prompts
for passwords. To improve those, refactor simple_prompt() so that
it allocates its own result buffer that can be made as big as
necessary. Actually, it proves best to make a separate routine
pg_get_line() that has essentially the semantics of fgets(), except
that it allocates a suitable result buffer and hence will never
return a truncated line. (pg_get_line has a lot of potential
applications to replace randomly-sized fgets buffers elsewhere,
but I'll leave that for another patch.)
I built pg_get_line() atop stringinfo.c, which requires moving
that code to src/common/; but that seems fine since it was a poor
fit for src/port/ anyway.
This patch is mostly mine, but it owes a good deal to Nathan Bossart
who pressed for a solution to the password length problem and
created a predecessor patch. Also thanks to Peter Eisentraut and
Stephen Frost for ideas and discussion.
Discussion: https://postgr.es/m/09512C4F-8CB9-4021-B455-EF4C4F0D55A0@amazon.com
2020-09-03 20:09:18 -04:00
|
|
|
values[3] = password;
|
2012-07-04 15:39:33 -04:00
|
|
|
keywords[4] = "dbname";
|
|
|
|
values[4] = database;
|
|
|
|
keywords[5] = "fallback_application_name";
|
|
|
|
values[5] = param->progname;
|
|
|
|
keywords[6] = NULL;
|
|
|
|
values[6] = NULL;
|
2007-12-11 02:08:59 +00:00
|
|
|
|
2012-07-04 15:39:33 -04:00
|
|
|
new_pass = false;
|
|
|
|
conn = PQconnectdbParams(keywords, values, true);
|
2007-12-11 02:08:59 +00:00
|
|
|
if (!conn)
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("connection to database \"%s\" failed", database);
|
2007-12-11 02:08:59 +00:00
|
|
|
return -1;
|
2002-04-24 02:45:51 +00:00
|
|
|
}
|
|
|
|
|
2007-12-11 02:08:59 +00:00
|
|
|
if (PQstatus(conn) == CONNECTION_BAD &&
|
|
|
|
PQconnectionNeedsPassword(conn) &&
|
Remove arbitrary restrictions on password length.
This patch started out with the goal of harmonizing various arbitrary
limits on password length, but after awhile a better idea emerged:
let's just get rid of those fixed limits.
recv_password_packet() has an arbitrary limit on the packet size,
which we don't really need, so just drop it. (Note that this doesn't
really affect anything for MD5 or SCRAM password verification, since
those will hash the user's password to something shorter anyway.
It does matter for auth methods that require a cleartext password.)
Likewise remove the arbitrary error condition in pg_saslprep().
The remaining limits are mostly in client-side code that prompts
for passwords. To improve those, refactor simple_prompt() so that
it allocates its own result buffer that can be made as big as
necessary. Actually, it proves best to make a separate routine
pg_get_line() that has essentially the semantics of fgets(), except
that it allocates a suitable result buffer and hence will never
return a truncated line. (pg_get_line has a lot of potential
applications to replace randomly-sized fgets buffers elsewhere,
but I'll leave that for another patch.)
I built pg_get_line() atop stringinfo.c, which requires moving
that code to src/common/; but that seems fine since it was a poor
fit for src/port/ anyway.
This patch is mostly mine, but it owes a good deal to Nathan Bossart
who pressed for a solution to the password length problem and
created a predecessor patch. Also thanks to Peter Eisentraut and
Stephen Frost for ideas and discussion.
Discussion: https://postgr.es/m/09512C4F-8CB9-4021-B455-EF4C4F0D55A0@amazon.com
2020-09-03 20:09:18 -04:00
|
|
|
!password &&
|
2009-02-26 16:02:39 +00:00
|
|
|
param->pg_prompt != TRI_NO)
|
2007-12-11 02:08:59 +00:00
|
|
|
{
|
|
|
|
PQfinish(conn);
|
Remove arbitrary restrictions on password length.
This patch started out with the goal of harmonizing various arbitrary
limits on password length, but after awhile a better idea emerged:
let's just get rid of those fixed limits.
recv_password_packet() has an arbitrary limit on the packet size,
which we don't really need, so just drop it. (Note that this doesn't
really affect anything for MD5 or SCRAM password verification, since
those will hash the user's password to something shorter anyway.
It does matter for auth methods that require a cleartext password.)
Likewise remove the arbitrary error condition in pg_saslprep().
The remaining limits are mostly in client-side code that prompts
for passwords. To improve those, refactor simple_prompt() so that
it allocates its own result buffer that can be made as big as
necessary. Actually, it proves best to make a separate routine
pg_get_line() that has essentially the semantics of fgets(), except
that it allocates a suitable result buffer and hence will never
return a truncated line. (pg_get_line has a lot of potential
applications to replace randomly-sized fgets buffers elsewhere,
but I'll leave that for another patch.)
I built pg_get_line() atop stringinfo.c, which requires moving
that code to src/common/; but that seems fine since it was a poor
fit for src/port/ anyway.
This patch is mostly mine, but it owes a good deal to Nathan Bossart
who pressed for a solution to the password length problem and
created a predecessor patch. Also thanks to Peter Eisentraut and
Stephen Frost for ideas and discussion.
Discussion: https://postgr.es/m/09512C4F-8CB9-4021-B455-EF4C4F0D55A0@amazon.com
2020-09-03 20:09:18 -04:00
|
|
|
password = simple_prompt("Password: ", false);
|
2007-12-11 02:08:59 +00:00
|
|
|
new_pass = true;
|
|
|
|
}
|
|
|
|
} while (new_pass);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
/* check to see that the backend connection was successfully made */
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD)
|
|
|
|
{
|
2021-01-22 16:52:31 -05:00
|
|
|
pg_log_error("%s", PQerrorMessage(conn));
|
2000-11-21 17:54:21 +00:00
|
|
|
PQfinish(conn);
|
1999-04-10 16:48:05 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2002-04-24 02:45:51 +00:00
|
|
|
if (param->verbose)
|
|
|
|
{
|
2012-03-20 19:05:08 -04:00
|
|
|
fprintf(stdout, "Connected to database \"%s\"\n", database);
|
2002-04-24 02:45:51 +00:00
|
|
|
if (param->dry_run)
|
|
|
|
fprintf(stdout, "Test run: no large objects will be removed!\n");
|
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
Empty search_path in Autovacuum and non-psql/pgbench clients.
This makes the client programs behave as documented regardless of the
connect-time search_path and regardless of user-created objects. Today,
a malicious user with CREATE permission on a search_path schema can take
control of certain of these clients' queries and invoke arbitrary SQL
functions under the client identity, often a superuser. This is
exploitable in the default configuration, where all users have CREATE
privilege on schema "public".
This changes behavior of user-defined code stored in the database, like
pg_index.indexprs and pg_extension_config_dump(). If they reach code
bearing unqualified names, "does not exist" or "no schema has been
selected to create in" errors might appear. Users may fix such errors
by schema-qualifying affected names. After upgrading, consider watching
server logs for these errors.
The --table arguments of src/bin/scripts clients have been lax; for
example, "vacuumdb -Zt pg_am\;CHECKPOINT" performed a checkpoint. That
now fails, but for now, "vacuumdb -Zt 'pg_am(amname);CHECKPOINT'" still
performs a checkpoint.
Back-patch to 9.3 (all supported versions).
Reviewed by Tom Lane, though this fix strategy was not his first choice.
Reported by Arseniy Sharoglazov.
Security: CVE-2018-1058
2018-02-26 07:39:44 -08:00
|
|
|
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
2002-10-18 18:41:22 +00:00
|
|
|
{
|
2024-09-04 13:50:44 +09:00
|
|
|
pg_log_error("failed to set \"search_path\": %s", PQerrorMessage(conn));
|
2002-10-18 18:41:22 +00:00
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
/*
|
2000-11-21 17:54:21 +00:00
|
|
|
* First we create and populate the LO temp table
|
1999-04-10 16:48:05 +00:00
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
2003-09-24 05:38:38 +00:00
|
|
|
strcat(buf, "CREATE TEMP TABLE vacuum_l AS ");
|
2010-02-17 04:19:41 +00:00
|
|
|
if (PQserverVersion(conn) >= 90000)
|
2009-12-14 00:39:11 +00:00
|
|
|
strcat(buf, "SELECT oid AS lo FROM pg_largeobject_metadata");
|
|
|
|
else
|
|
|
|
strcat(buf, "SELECT DISTINCT loid AS lo FROM pg_largeobject");
|
2000-11-21 17:54:21 +00:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to create temp table: %s", PQerrorMessage(conn));
|
2000-11-21 17:54:21 +00:00
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
2001-03-22 04:01:46 +00:00
|
|
|
|
2000-11-21 17:54:21 +00:00
|
|
|
/*
|
2009-07-13 22:56:30 +00:00
|
|
|
* Analyze the temp table so that planner will generate decent plans for
|
2000-11-21 17:54:21 +00:00
|
|
|
* the DELETEs below.
|
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
2009-07-13 22:56:30 +00:00
|
|
|
strcat(buf, "ANALYZE vacuum_l");
|
2000-11-21 17:54:21 +00:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
1999-04-10 16:48:05 +00:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to vacuum temp table: %s", PQerrorMessage(conn));
|
2000-11-21 17:54:21 +00:00
|
|
|
PQclear(res);
|
1999-04-10 16:48:05 +00:00
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
|
|
|
/*
|
2003-09-24 05:38:38 +00:00
|
|
|
* Now find any candidate tables that have columns of type oid.
|
2000-11-21 17:54:21 +00:00
|
|
|
*
|
2003-08-04 22:03:39 +00:00
|
|
|
* NOTE: we ignore system tables and temp tables by the expedient of
|
|
|
|
* rejecting tables in schemas named 'pg_*'. In particular, the temp
|
|
|
|
* table formed above is ignored, and pg_largeobject will be too. If
|
2000-11-21 17:54:21 +00:00
|
|
|
* either of these were scanned, obviously we'd end up with nothing to
|
|
|
|
* delete...
|
1999-05-25 16:15:34 +00:00
|
|
|
*/
|
1999-04-10 16:48:05 +00:00
|
|
|
buf[0] = '\0';
|
2003-09-24 05:38:38 +00:00
|
|
|
strcat(buf, "SELECT s.nspname, c.relname, a.attname ");
|
2003-08-04 22:03:39 +00:00
|
|
|
strcat(buf, "FROM pg_class c, pg_attribute a, pg_namespace s, pg_type t ");
|
2003-09-24 05:38:38 +00:00
|
|
|
strcat(buf, "WHERE a.attnum > 0 AND NOT a.attisdropped ");
|
1999-04-10 16:48:05 +00:00
|
|
|
strcat(buf, " AND a.attrelid = c.oid ");
|
|
|
|
strcat(buf, " AND a.atttypid = t.oid ");
|
2003-08-04 22:03:39 +00:00
|
|
|
strcat(buf, " AND c.relnamespace = s.oid ");
|
2001-09-17 02:30:54 +00:00
|
|
|
strcat(buf, " AND t.typname in ('oid', 'lo') ");
|
2017-03-09 23:36:44 -05:00
|
|
|
strcat(buf, " AND c.relkind in (" CppAsString2(RELKIND_RELATION) ", " CppAsString2(RELKIND_MATVIEW) ")");
|
2006-02-23 22:33:59 +00:00
|
|
|
strcat(buf, " AND s.nspname !~ '^pg_'");
|
2000-11-21 17:54:21 +00:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
1999-04-10 16:48:05 +00:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to find OID columns: %s", PQerrorMessage(conn));
|
2000-11-21 17:54:21 +00:00
|
|
|
PQclear(res);
|
1999-04-10 16:48:05 +00:00
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-11-21 17:54:21 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
for (i = 0; i < PQntuples(res); i++)
|
1999-05-25 16:15:34 +00:00
|
|
|
{
|
2003-09-24 05:38:38 +00:00
|
|
|
char *schema,
|
|
|
|
*table,
|
1999-05-25 16:15:34 +00:00
|
|
|
*field;
|
|
|
|
|
2003-09-24 05:38:38 +00:00
|
|
|
schema = PQgetvalue(res, i, 0);
|
|
|
|
table = PQgetvalue(res, i, 1);
|
|
|
|
field = PQgetvalue(res, i, 2);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2002-04-24 02:45:51 +00:00
|
|
|
if (param->verbose)
|
2003-09-24 05:38:38 +00:00
|
|
|
fprintf(stdout, "Checking %s in %s.%s\n", field, schema, table);
|
2000-11-21 17:54:21 +00:00
|
|
|
|
2012-03-20 19:05:08 -04:00
|
|
|
schema = PQescapeIdentifier(conn, schema, strlen(schema));
|
|
|
|
table = PQescapeIdentifier(conn, table, strlen(table));
|
|
|
|
field = PQescapeIdentifier(conn, field, strlen(field));
|
|
|
|
|
|
|
|
if (!schema || !table || !field)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("%s", PQerrorMessage(conn));
|
2012-03-20 19:05:08 -04:00
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
2022-08-26 19:16:28 +02:00
|
|
|
PQfreemem(schema);
|
|
|
|
PQfreemem(table);
|
|
|
|
PQfreemem(field);
|
2012-03-20 19:05:08 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-08-04 22:03:39 +00:00
|
|
|
snprintf(buf, BUFSIZE,
|
|
|
|
"DELETE FROM vacuum_l "
|
2012-03-20 19:05:08 -04:00
|
|
|
"WHERE lo IN (SELECT %s FROM %s.%s)",
|
2003-09-24 05:38:38 +00:00
|
|
|
field, schema, table);
|
2000-11-21 17:54:21 +00:00
|
|
|
res2 = PQexec(conn, buf);
|
1999-04-10 16:48:05 +00:00
|
|
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
1999-05-25 16:15:34 +00:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to check %s in table %s.%s: %s",
|
|
|
|
field, schema, table, PQerrorMessage(conn));
|
1999-04-10 16:48:05 +00:00
|
|
|
PQclear(res2);
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
2015-01-14 15:14:20 -05:00
|
|
|
PQfreemem(schema);
|
|
|
|
PQfreemem(table);
|
|
|
|
PQfreemem(field);
|
1999-04-10 16:48:05 +00:00
|
|
|
return -1;
|
1999-05-25 16:15:34 +00:00
|
|
|
}
|
1999-04-10 16:48:05 +00:00
|
|
|
PQclear(res2);
|
2012-03-20 19:05:08 -04:00
|
|
|
|
|
|
|
PQfreemem(schema);
|
|
|
|
PQfreemem(table);
|
|
|
|
PQfreemem(field);
|
1999-04-10 16:48:05 +00:00
|
|
|
}
|
|
|
|
PQclear(res);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2000-11-21 17:54:21 +00:00
|
|
|
/*
|
2012-03-20 19:05:08 -04:00
|
|
|
* Now, those entries remaining in vacuum_l are orphans. Delete 'em.
|
|
|
|
*
|
|
|
|
* We don't want to run each delete as an individual transaction, because
|
|
|
|
* the commit overhead would be high. However, since 9.0 the backend will
|
|
|
|
* acquire a lock per deleted LO, so deleting too many LOs per transaction
|
|
|
|
* risks running out of room in the shared-memory lock table. Accordingly,
|
|
|
|
* we delete up to transaction_limit LOs per transaction.
|
2000-11-21 17:54:21 +00:00
|
|
|
*/
|
1999-04-10 16:48:05 +00:00
|
|
|
res = PQexec(conn, "begin");
|
2012-03-20 19:05:08 -04:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to start transaction: %s", PQerrorMessage(conn));
|
2012-03-20 19:05:08 -04:00
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
1999-04-10 16:48:05 +00:00
|
|
|
PQclear(res);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
buf[0] = '\0';
|
2013-07-15 10:48:44 -04:00
|
|
|
strcat(buf,
|
|
|
|
"DECLARE myportal CURSOR WITH HOLD FOR SELECT lo FROM vacuum_l");
|
2000-11-21 17:54:21 +00:00
|
|
|
res = PQexec(conn, buf);
|
2013-07-15 10:48:44 -04:00
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
1999-04-10 16:48:05 +00:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("DECLARE CURSOR failed: %s", PQerrorMessage(conn));
|
2000-11-21 17:54:21 +00:00
|
|
|
PQclear(res);
|
1999-04-10 16:48:05 +00:00
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
1999-05-25 16:15:34 +00:00
|
|
|
}
|
2013-07-15 10:48:44 -04:00
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
snprintf(buf, BUFSIZE, "FETCH FORWARD %ld IN myportal",
|
|
|
|
param->transaction_limit > 0 ? param->transaction_limit : 1000L);
|
2000-11-21 17:54:21 +00:00
|
|
|
|
|
|
|
deleted = 0;
|
2013-07-15 10:48:44 -04:00
|
|
|
|
2019-03-24 15:13:20 -04:00
|
|
|
do
|
1999-05-25 16:15:34 +00:00
|
|
|
{
|
2013-07-15 10:48:44 -04:00
|
|
|
res = PQexec(conn, buf);
|
|
|
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("FETCH FORWARD failed: %s", PQerrorMessage(conn));
|
2013-07-15 10:48:44 -04:00
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2013-07-15 10:48:44 -04:00
|
|
|
matched = PQntuples(res);
|
|
|
|
if (matched <= 0)
|
1999-04-10 16:48:05 +00:00
|
|
|
{
|
2013-07-15 10:48:44 -04:00
|
|
|
/* at end of resultset */
|
|
|
|
PQclear(res);
|
|
|
|
break;
|
1999-04-10 16:48:05 +00:00
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2013-07-15 10:48:44 -04:00
|
|
|
for (i = 0; i < matched; i++)
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
2013-07-15 10:48:44 -04:00
|
|
|
Oid lo = atooid(PQgetvalue(res, i, 0));
|
|
|
|
|
|
|
|
if (param->verbose)
|
|
|
|
{
|
|
|
|
fprintf(stdout, "\rRemoving lo %6u ", lo);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->dry_run == 0)
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
2013-07-15 10:48:44 -04:00
|
|
|
if (lo_unlink(conn, lo) < 0)
|
2011-08-08 09:12:16 -04:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to remove lo %u: %s", lo,
|
|
|
|
PQerrorMessage(conn));
|
2013-07-15 10:48:44 -04:00
|
|
|
if (PQtransactionStatus(conn) == PQTRANS_INERROR)
|
|
|
|
{
|
|
|
|
success = false;
|
2019-03-24 15:13:20 -04:00
|
|
|
break; /* out of inner for-loop */
|
2013-07-15 10:48:44 -04:00
|
|
|
}
|
2011-08-08 09:12:16 -04:00
|
|
|
}
|
2013-07-15 10:48:44 -04:00
|
|
|
else
|
|
|
|
deleted++;
|
2002-04-24 02:45:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
deleted++;
|
2013-07-15 10:48:44 -04:00
|
|
|
|
|
|
|
if (param->transaction_limit > 0 &&
|
|
|
|
(deleted % param->transaction_limit) == 0)
|
2012-03-20 19:05:08 -04:00
|
|
|
{
|
2013-07-15 10:48:44 -04:00
|
|
|
res2 = PQexec(conn, "commit");
|
|
|
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to commit transaction: %s",
|
|
|
|
PQerrorMessage(conn));
|
2013-07-15 10:48:44 -04:00
|
|
|
PQclear(res2);
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-20 19:05:08 -04:00
|
|
|
PQclear(res2);
|
2013-07-15 10:48:44 -04:00
|
|
|
res2 = PQexec(conn, "begin");
|
|
|
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to start transaction: %s",
|
|
|
|
PQerrorMessage(conn));
|
2013-07-15 10:48:44 -04:00
|
|
|
PQclear(res2);
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-20 19:05:08 -04:00
|
|
|
PQclear(res2);
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 10:48:44 -04:00
|
|
|
|
|
|
|
PQclear(res);
|
2019-03-24 15:13:20 -04:00
|
|
|
} while (success);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
/*
|
|
|
|
* That's all folks!
|
|
|
|
*/
|
2012-03-20 19:05:08 -04:00
|
|
|
res = PQexec(conn, "commit");
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("failed to commit transaction: %s",
|
|
|
|
PQerrorMessage(conn));
|
2012-03-20 19:05:08 -04:00
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
return -1;
|
|
|
|
}
|
1999-04-10 16:48:05 +00:00
|
|
|
PQclear(res);
|
2000-11-21 17:54:21 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
PQfinish(conn);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2002-04-24 02:45:51 +00:00
|
|
|
if (param->verbose)
|
2011-08-08 09:12:16 -04:00
|
|
|
{
|
|
|
|
if (param->dry_run)
|
2012-03-20 19:05:08 -04:00
|
|
|
fprintf(stdout, "\rWould remove %ld large objects from database \"%s\".\n",
|
2011-08-08 09:12:16 -04:00
|
|
|
deleted, database);
|
|
|
|
else if (success)
|
|
|
|
fprintf(stdout,
|
2012-03-20 19:05:08 -04:00
|
|
|
"\rSuccessfully removed %ld large objects from database \"%s\".\n",
|
2011-08-08 09:12:16 -04:00
|
|
|
deleted, database);
|
|
|
|
else
|
2012-03-20 19:05:08 -04:00
|
|
|
fprintf(stdout, "\rRemoval from database \"%s\" failed at object %ld of %ld.\n",
|
2011-08-08 09:12:16 -04:00
|
|
|
database, deleted, matched);
|
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2011-08-08 09:12:16 -04:00
|
|
|
return ((param->dry_run || success) ? 0 : -1);
|
1999-04-10 16:48:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-20 19:05:08 -04:00
|
|
|
static void
|
2009-02-27 09:30:21 +00:00
|
|
|
usage(const char *progname)
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
2009-02-27 09:30:21 +00:00
|
|
|
printf("%s removes unreferenced large objects from databases.\n\n", progname);
|
|
|
|
printf("Usage:\n %s [OPTION]... DBNAME...\n\n", progname);
|
2009-02-25 13:34:32 +00:00
|
|
|
printf("Options:\n");
|
2018-08-28 21:42:45 +09:00
|
|
|
printf(" -l, --limit=LIMIT commit after removing each LIMIT large objects\n");
|
|
|
|
printf(" -n, --dry-run don't remove large objects, just show what would be done\n");
|
|
|
|
printf(" -v, --verbose write a lot of progress messages\n");
|
|
|
|
printf(" -V, --version output version information, then exit\n");
|
|
|
|
printf(" -?, --help show this help, then exit\n");
|
2012-06-18 02:44:00 +03:00
|
|
|
printf("\nConnection options:\n");
|
2018-08-28 21:42:45 +09:00
|
|
|
printf(" -h, --host=HOSTNAME database server host or socket directory\n");
|
|
|
|
printf(" -p, --port=PORT database server port\n");
|
|
|
|
printf(" -U, --username=USERNAME user name to connect as\n");
|
|
|
|
printf(" -w, --no-password never prompt for password\n");
|
|
|
|
printf(" -W, --password force password prompt\n");
|
2009-02-25 13:34:32 +00:00
|
|
|
printf("\n");
|
2020-02-28 08:54:49 +01:00
|
|
|
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
|
2020-02-28 08:54:49 +01:00
|
|
|
printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
|
2002-04-24 02:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2018-08-28 21:42:45 +09:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"host", required_argument, NULL, 'h'},
|
|
|
|
{"limit", required_argument, NULL, 'l'},
|
|
|
|
{"dry-run", no_argument, NULL, 'n'},
|
|
|
|
{"port", required_argument, NULL, 'p'},
|
|
|
|
{"username", required_argument, NULL, 'U'},
|
|
|
|
{"verbose", no_argument, NULL, 'v'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{"no-password", no_argument, NULL, 'w'},
|
|
|
|
{"password", no_argument, NULL, 'W'},
|
|
|
|
{"help", no_argument, NULL, '?'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
int rc = 0;
|
2002-04-24 02:45:51 +00:00
|
|
|
struct _param param;
|
|
|
|
int c;
|
|
|
|
int port;
|
2009-02-27 09:30:21 +00:00
|
|
|
const char *progname;
|
2018-08-28 21:42:45 +09:00
|
|
|
int optindex;
|
2009-02-27 09:30:21 +00:00
|
|
|
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_logging_init(argv[0]);
|
2009-02-27 09:30:21 +00:00
|
|
|
progname = get_progname(argv[0]);
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2012-03-20 19:05:08 -04:00
|
|
|
/* Set default parameter values */
|
2002-04-24 02:45:51 +00:00
|
|
|
param.pg_user = NULL;
|
2009-02-26 16:02:39 +00:00
|
|
|
param.pg_prompt = TRI_DEFAULT;
|
2002-04-24 02:45:51 +00:00
|
|
|
param.pg_host = NULL;
|
2004-11-09 06:09:40 +00:00
|
|
|
param.pg_port = NULL;
|
2012-07-04 15:39:33 -04:00
|
|
|
param.progname = progname;
|
2002-04-24 02:45:51 +00:00
|
|
|
param.verbose = 0;
|
|
|
|
param.dry_run = 0;
|
2012-03-20 19:05:08 -04:00
|
|
|
param.transaction_limit = 1000;
|
2002-04-24 02:45:51 +00:00
|
|
|
|
2012-03-20 19:05:08 -04:00
|
|
|
/* Process command-line arguments */
|
2009-02-27 09:30:21 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 21:42:45 +09:00
|
|
|
while ((c = getopt_long(argc, argv, "h:l:np:U:vwW", long_options, &optindex)) != -1)
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2018-08-28 21:42:45 +09:00
|
|
|
case 'h':
|
|
|
|
param.pg_host = pg_strdup(optarg);
|
2002-04-24 02:45:51 +00:00
|
|
|
break;
|
2012-03-20 19:05:08 -04:00
|
|
|
case 'l':
|
|
|
|
param.transaction_limit = strtol(optarg, NULL, 10);
|
|
|
|
if (param.transaction_limit < 0)
|
2022-04-08 14:55:14 -04:00
|
|
|
pg_fatal("transaction limit must not be negative (0 disables)");
|
2012-03-20 19:05:08 -04:00
|
|
|
break;
|
2018-08-28 21:42:45 +09:00
|
|
|
case 'n':
|
|
|
|
param.dry_run = 1;
|
|
|
|
param.verbose = 1;
|
2002-04-24 02:45:51 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port = strtol(optarg, NULL, 10);
|
|
|
|
if ((port < 1) || (port > 65535))
|
2022-04-08 14:55:14 -04:00
|
|
|
pg_fatal("invalid port number: %s", optarg);
|
2016-08-30 18:22:43 -04:00
|
|
|
param.pg_port = pg_strdup(optarg);
|
2002-04-24 02:45:51 +00:00
|
|
|
break;
|
2018-08-28 21:42:45 +09:00
|
|
|
case 'U':
|
|
|
|
param.pg_user = pg_strdup(optarg);
|
2002-04-24 02:45:51 +00:00
|
|
|
break;
|
2018-08-28 21:42:45 +09:00
|
|
|
case 'v':
|
|
|
|
param.verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
param.pg_prompt = TRI_NO;
|
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
param.pg_prompt = TRI_YES;
|
|
|
|
break;
|
|
|
|
default:
|
2022-04-08 14:55:14 -04:00
|
|
|
/* getopt_long already emitted a complaint */
|
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2018-08-28 21:42:45 +09:00
|
|
|
exit(1);
|
2002-04-24 02:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-04 20:31:48 +00:00
|
|
|
|
2002-04-24 02:45:51 +00:00
|
|
|
/* No database given? Show usage */
|
2002-12-03 07:12:18 +00:00
|
|
|
if (optind >= argc)
|
2002-04-24 02:45:51 +00:00
|
|
|
{
|
2019-09-06 14:00:13 +09:00
|
|
|
pg_log_error("missing required argument: database name");
|
2022-04-08 14:55:14 -04:00
|
|
|
pg_log_error_hint("Try \"%s --help\" for more information.", progname);
|
2002-04-24 02:45:51 +00:00
|
|
|
exit(1);
|
1999-04-10 16:48:05 +00:00
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
2002-04-24 02:45:51 +00:00
|
|
|
for (c = optind; c < argc; c++)
|
|
|
|
{
|
|
|
|
/* Work on selected database */
|
|
|
|
rc += (vacuumlo(argv[c], ¶m) != 0);
|
1999-04-10 16:48:05 +00:00
|
|
|
}
|
1999-05-25 16:15:34 +00:00
|
|
|
|
1999-04-10 16:48:05 +00:00
|
|
|
return rc;
|
|
|
|
}
|