diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index f03d5b1c6c..c827873c38 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -647,7 +647,7 @@ vacuum_one_database(ConnParams *cparams, if (objects_listed) appendPQExpBufferStr(&catalog_query, "\n)\n"); - appendPQExpBufferStr(&catalog_query, "SELECT c.relname, ns.nspname"); + appendPQExpBufferStr(&catalog_query, "SELECT DISTINCT c.relpages, c.relname, ns.nspname"); if (objects_listed) appendPQExpBufferStr(&catalog_query, ", listed_objects.column_list"); @@ -690,6 +690,19 @@ vacuum_one_database(ConnParams *cparams, has_where = true; } + /* + * If multiple -N switches are used, the joined table may contain table + * names that should have been excluded. + * This WHERE clause explicitly excludes it. + */ + if ((objfilter & OBJFILTER_SCHEMA_EXCLUDE)) + { + appendPQExpBuffer(&catalog_query, + " %s ns.oid NOT IN (SELECT object_oid FROM listed_objects)\n", + has_where ? "AND" : "WHERE"); + has_where = true; + } + /* * For --min-xid-age and --min-mxid-age, the age of the relation is the * greatest of the ages of the main relation and its associated TOAST @@ -750,11 +763,11 @@ vacuum_one_database(ConnParams *cparams, for (i = 0; i < ntups; i++) { appendPQExpBufferStr(&buf, - fmtQualifiedId(PQgetvalue(res, i, 1), - PQgetvalue(res, i, 0))); + fmtQualifiedId(PQgetvalue(res, i, 2), + PQgetvalue(res, i, 1))); - if (objects_listed && !PQgetisnull(res, i, 2)) - appendPQExpBufferStr(&buf, PQgetvalue(res, i, 2)); + if (objects_listed && !PQgetisnull(res, i, 3)) + appendPQExpBufferStr(&buf, PQgetvalue(res, i, 3)); simple_string_list_append(&dbtables, buf.data); resetPQExpBuffer(&buf);