diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index f03d5b1c6c..5e05f27462 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -662,18 +662,19 @@ vacuum_one_database(ConnParams *cparams, /* Used to match the tables or schemas listed by the user */ if (objects_listed) { - appendPQExpBufferStr(&catalog_query, " JOIN listed_objects" - " ON listed_objects.object_oid "); - - if (objfilter & OBJFILTER_SCHEMA_EXCLUDE) - appendPQExpBufferStr(&catalog_query, "OPERATOR(pg_catalog.!=) "); - else - appendPQExpBufferStr(&catalog_query, "OPERATOR(pg_catalog.=) "); + appendPQExpBufferStr(&catalog_query, " LEFT JOIN listed_objects" + " ON listed_objects.object_oid" + " OPERATOR(pg_catalog.=) "); if (objfilter & OBJFILTER_TABLE) appendPQExpBufferStr(&catalog_query, "c.oid\n"); else appendPQExpBufferStr(&catalog_query, "ns.oid\n"); + + appendPQExpBuffer(&catalog_query, + " WHERE listed_objects.object_oid IS %s NULL\n", + (objfilter & OBJFILTER_SCHEMA_EXCLUDE) ? "" : "NOT"); + has_where = true; } /* @@ -684,9 +685,11 @@ vacuum_one_database(ConnParams *cparams, */ if ((objfilter & OBJFILTER_TABLE) == 0) { - appendPQExpBufferStr(&catalog_query, " WHERE c.relkind OPERATOR(pg_catalog.=) ANY (array[" - CppAsString2(RELKIND_RELATION) ", " - CppAsString2(RELKIND_MATVIEW) "])\n"); + appendPQExpBuffer(&catalog_query, + " %s c.relkind OPERATOR(pg_catalog.=) ANY (array[" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) "])\n", + has_where ? "AND" : "WHERE"); has_where = true; }