1 /*-------------------------------------------------------------------------
4 * This removes orphaned large objects from a database.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/contrib/vacuumlo/vacuumlo.c,v 1.38 2009/02/25 13:24:40 petere Exp $
13 *-------------------------------------------------------------------------
15 #include "postgres_fe.h"
25 #include "libpq/libpq-fs.h"
27 #define atooid(x) ((Oid) strtoul((x), NULL, 10))
46 int vacuumlo(char *, struct _param *);
52 * This vacuums LOs of one database. It returns 0 on success, -1 on failure.
55 vacuumlo(char *database, struct _param * param)
64 static char *password = NULL;
67 if (param->pg_prompt && password == NULL)
68 password = simple_prompt("Password: ", 100, false);
71 * Start the connection. Loop until we have a password if requested by
78 conn = PQsetdbLogin(param->pg_host,
87 fprintf(stderr, "Connection to database \"%s\" failed\n",
92 if (PQstatus(conn) == CONNECTION_BAD &&
93 PQconnectionNeedsPassword(conn) &&
97 password = simple_prompt("Password: ", 100, false);
102 /* check to see that the backend connection was successfully made */
103 if (PQstatus(conn) == CONNECTION_BAD)
105 fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
106 database, PQerrorMessage(conn));
113 fprintf(stdout, "Connected to %s\n", database);
115 fprintf(stdout, "Test run: no large objects will be removed!\n");
119 * Don't get fooled by any non-system catalogs
121 res = PQexec(conn, "SET search_path = pg_catalog");
122 if (PQresultStatus(res) != PGRES_COMMAND_OK)
124 fprintf(stderr, "Failed to set search_path:\n");
125 fprintf(stderr, "%s", PQerrorMessage(conn));
133 * First we create and populate the LO temp table
136 strcat(buf, "CREATE TEMP TABLE vacuum_l AS ");
137 strcat(buf, "SELECT DISTINCT loid AS lo FROM pg_largeobject ");
138 res = PQexec(conn, buf);
139 if (PQresultStatus(res) != PGRES_COMMAND_OK)
141 fprintf(stderr, "Failed to create temp table:\n");
142 fprintf(stderr, "%s", PQerrorMessage(conn));
150 * Vacuum the temp table so that planner will generate decent plans for
154 strcat(buf, "VACUUM ANALYZE vacuum_l");
155 res = PQexec(conn, buf);
156 if (PQresultStatus(res) != PGRES_COMMAND_OK)
158 fprintf(stderr, "Failed to vacuum temp table:\n");
159 fprintf(stderr, "%s", PQerrorMessage(conn));
167 * Now find any candidate tables that have columns of type oid.
169 * NOTE: we ignore system tables and temp tables by the expedient of
170 * rejecting tables in schemas named 'pg_*'. In particular, the temp
171 * table formed above is ignored, and pg_largeobject will be too. If
172 * either of these were scanned, obviously we'd end up with nothing to
175 * NOTE: the system oid column is ignored, as it has attnum < 1. This
176 * shouldn't matter for correctness, but it saves time.
179 strcat(buf, "SELECT s.nspname, c.relname, a.attname ");
180 strcat(buf, "FROM pg_class c, pg_attribute a, pg_namespace s, pg_type t ");
181 strcat(buf, "WHERE a.attnum > 0 AND NOT a.attisdropped ");
182 strcat(buf, " AND a.attrelid = c.oid ");
183 strcat(buf, " AND a.atttypid = t.oid ");
184 strcat(buf, " AND c.relnamespace = s.oid ");
185 strcat(buf, " AND t.typname in ('oid', 'lo') ");
186 strcat(buf, " AND c.relkind = 'r'");
187 strcat(buf, " AND s.nspname !~ '^pg_'");
188 res = PQexec(conn, buf);
189 if (PQresultStatus(res) != PGRES_TUPLES_OK)
191 fprintf(stderr, "Failed to find OID columns:\n");
192 fprintf(stderr, "%s", PQerrorMessage(conn));
198 for (i = 0; i < PQntuples(res); i++)
204 schema = PQgetvalue(res, i, 0);
205 table = PQgetvalue(res, i, 1);
206 field = PQgetvalue(res, i, 2);
209 fprintf(stdout, "Checking %s in %s.%s\n", field, schema, table);
212 * The "IN" construct used here was horribly inefficient before
213 * Postgres 7.4, but should be now competitive if not better than the
214 * bogus join we used before.
216 snprintf(buf, BUFSIZE,
217 "DELETE FROM vacuum_l "
218 "WHERE lo IN (SELECT \"%s\" FROM \"%s\".\"%s\")",
219 field, schema, table);
220 res2 = PQexec(conn, buf);
221 if (PQresultStatus(res2) != PGRES_COMMAND_OK)
223 fprintf(stderr, "Failed to check %s in table %s.%s:\n",
224 field, schema, table);
225 fprintf(stderr, "%s", PQerrorMessage(conn));
236 * Run the actual deletes in a single transaction. Note that this would
237 * be a bad idea in pre-7.1 Postgres releases (since rolling back a table
238 * delete used to cause problems), but it should be safe now.
240 res = PQexec(conn, "begin");
244 * Finally, those entries remaining in vacuum_l are orphans.
247 strcat(buf, "SELECT lo ");
248 strcat(buf, "FROM vacuum_l");
249 res = PQexec(conn, buf);
250 if (PQresultStatus(res) != PGRES_TUPLES_OK)
252 fprintf(stderr, "Failed to read temp table:\n");
253 fprintf(stderr, "%s", PQerrorMessage(conn));
259 matched = PQntuples(res);
261 for (i = 0; i < matched; i++)
263 Oid lo = atooid(PQgetvalue(res, i, 0));
267 fprintf(stdout, "\rRemoving lo %6u ", lo);
271 if (param->dry_run == 0)
273 if (lo_unlink(conn, lo) < 0)
275 fprintf(stderr, "\nFailed to remove lo %u: ", lo);
276 fprintf(stderr, "%s", PQerrorMessage(conn));
289 res = PQexec(conn, "end");
295 fprintf(stdout, "\r%s %d large objects from %s.\n",
296 (param->dry_run ? "Would remove" : "Removed"), deleted, database);
304 fprintf(stdout, "vacuumlo removes unreferenced large objects from databases\n\n");
305 fprintf(stdout, "Usage:\n vacuumlo [options] dbname [dbname ...]\n\n");
306 fprintf(stdout, "Options:\n");
307 fprintf(stdout, " -v\t\tWrite a lot of progress messages\n");
308 fprintf(stdout, " -n\t\tDon't remove large objects, just show what would be done\n");
309 fprintf(stdout, " -U username\tUsername to connect as\n");
310 fprintf(stdout, " -W\t\tForce password prompt\n");
311 fprintf(stdout, " -h hostname\tDatabase server host\n");
312 fprintf(stdout, " -p port\tDatabase server port\n\n");
317 main(int argc, char **argv)
324 /* Parameter handling */
325 param.pg_user = NULL;
327 param.pg_host = NULL;
328 param.pg_port = NULL;
334 c = getopt(argc, argv, "?h:U:p:vnW");
357 param.pg_user = strdup(optarg);
363 port = strtol(optarg, NULL, 10);
364 if ((port < 1) || (port > 65535))
366 fprintf(stderr, "[%s]: invalid port number '%s'\n", argv[0], optarg);
369 param.pg_port = strdup(optarg);
372 param.pg_host = strdup(optarg);
377 /* No database given? Show usage */
380 fprintf(stderr, "vacuumlo: missing required argument: database name\n");
381 fprintf(stderr, "Try 'vacuumlo -?' for help.\n");
385 for (c = optind; c < argc; c++)
387 /* Work on selected database */
388 rc += (vacuumlo(argv[c], ¶m) != 0);