mirror of
https://github.com/postgres/postgres.git
synced 2025-10-19 15:49:24 +03:00
Don't uselessly rewrite, truncate, VACUUM, or ANALYZE partitioned tables.
Also, recursively perform VACUUM and ANALYZE on partitions when the command is applied to a partitioned table. In passing, some related documentation updates. Amit Langote, reviewed by Michael Paquier, Ashutosh Bapat, and by me. Discussion: http://postgr.es/m/47288cf1-f72c-dfc2-5ff0-4af962ae5c1b@lab.ntt.co.jp
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include "access/xact.h"
|
||||
#include "catalog/namespace.h"
|
||||
#include "catalog/pg_database.h"
|
||||
#include "catalog/pg_inherits_fn.h"
|
||||
#include "catalog/pg_namespace.h"
|
||||
#include "commands/cluster.h"
|
||||
#include "commands/vacuum.h"
|
||||
@@ -394,6 +395,9 @@ get_rel_oids(Oid relid, const RangeVar *vacrel)
|
||||
{
|
||||
/* Process a specific relation */
|
||||
Oid relid;
|
||||
HeapTuple tuple;
|
||||
Form_pg_class classForm;
|
||||
bool include_parts;
|
||||
|
||||
/*
|
||||
* Since we don't take a lock here, the relation might be gone, or the
|
||||
@@ -406,9 +410,29 @@ get_rel_oids(Oid relid, const RangeVar *vacrel)
|
||||
*/
|
||||
relid = RangeVarGetRelid(vacrel, NoLock, false);
|
||||
|
||||
/* Make a relation list entry for this guy */
|
||||
/*
|
||||
* To check whether the relation is a partitioned table, fetch its
|
||||
* syscache entry.
|
||||
*/
|
||||
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
elog(ERROR, "cache lookup failed for relation %u", relid);
|
||||
classForm = (Form_pg_class) GETSTRUCT(tuple);
|
||||
include_parts = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
|
||||
ReleaseSysCache(tuple);
|
||||
|
||||
/*
|
||||
* Make relation list entries for this guy and its partitions, if any.
|
||||
* Note that the list returned by find_all_inheritors() include the
|
||||
* passed-in OID at its head. Also note that we did not request a
|
||||
* lock to be taken to match what would be done otherwise.
|
||||
*/
|
||||
oldcontext = MemoryContextSwitchTo(vac_context);
|
||||
oid_list = lappend_oid(oid_list, relid);
|
||||
if (include_parts)
|
||||
oid_list = list_concat(oid_list,
|
||||
find_all_inheritors(relid, NoLock, NULL));
|
||||
else
|
||||
oid_list = lappend_oid(oid_list, relid);
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
}
|
||||
else
|
||||
@@ -429,8 +453,14 @@ get_rel_oids(Oid relid, const RangeVar *vacrel)
|
||||
{
|
||||
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
|
||||
|
||||
/*
|
||||
* We include partitioned tables here; depending on which
|
||||
* operation is to be performed, caller will decide whether to
|
||||
* process or ignore them.
|
||||
*/
|
||||
if (classForm->relkind != RELKIND_RELATION &&
|
||||
classForm->relkind != RELKIND_MATVIEW)
|
||||
classForm->relkind != RELKIND_MATVIEW &&
|
||||
classForm->relkind != RELKIND_PARTITIONED_TABLE)
|
||||
continue;
|
||||
|
||||
/* Make a relation list entry for this guy */
|
||||
@@ -1349,6 +1379,21 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params)
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ignore partitioned tables as there is no work to be done. Since we
|
||||
* release the lock here, it's possible that any partitions added from
|
||||
* this point on will not get processed, but that seems harmless.
|
||||
*/
|
||||
if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
|
||||
{
|
||||
relation_close(onerel, lmode);
|
||||
PopActiveSnapshot();
|
||||
CommitTransactionCommand();
|
||||
|
||||
/* It's OK for other commands to look at this table */
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a session-level lock too. This will protect our access to the
|
||||
* relation across multiple transactions, so that we can vacuum the
|
||||
|
Reference in New Issue
Block a user