*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.29 2002/03/21 23:27:20 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.30 2002/04/02 01:03:05 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "access/tuptoaster.h"
#include "catalog/catname.h"
#include "catalog/indexing.h"
+#include "catalog/pg_namespace.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
+#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/tuplesort.h"
numrows;
double totalrows;
HeapTuple *rows;
- HeapTuple tuple;
if (vacstmt->verbose)
elevel = INFO;
* Race condition -- if the pg_class tuple has gone away since the
* last time we saw it, we don't need to process it.
*/
- tuple = SearchSysCache(RELOID,
- ObjectIdGetDatum(relid),
- 0, 0, 0);
- if (!HeapTupleIsValid(tuple))
+ if (!SearchSysCacheExists(RELOID,
+ ObjectIdGetDatum(relid),
+ 0, 0, 0))
{
CommitTransactionCommand();
return;
}
/*
- * We can ANALYZE any table except pg_statistic. See update_attstats
+ * Open the class, getting only a read lock on it, and check
+ * permissions. Permissions check should match vacuum's check!
*/
- if (strcmp(NameStr(((Form_pg_class) GETSTRUCT(tuple))->relname),
- StatisticRelationName) == 0)
+ onerel = relation_open(relid, AccessShareLock);
+
+ if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
+ (is_dbadmin(MyDatabaseId) && !onerel->rd_rel->relisshared)))
{
- ReleaseSysCache(tuple);
+ /* No need for a WARNING if we already complained during VACUUM */
+ if (!vacstmt->vacuum)
+ elog(WARNING, "Skipping \"%s\" --- only table or database owner can ANALYZE it",
+ RelationGetRelationName(onerel));
+ relation_close(onerel, AccessShareLock);
CommitTransactionCommand();
return;
}
- ReleaseSysCache(tuple);
/*
- * Open the class, getting only a read lock on it, and check
- * permissions. Permissions check should match vacuum's check!
+ * Check that it's a plain table; we used to do this in getrels() but
+ * seems safer to check after we've locked the relation.
*/
- onerel = heap_open(relid, AccessShareLock);
-
- if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
- (is_dbadmin(MyDatabaseId) && !onerel->rd_rel->relisshared)))
+ if (onerel->rd_rel->relkind != RELKIND_RELATION)
{
/* No need for a WARNING if we already complained during VACUUM */
if (!vacstmt->vacuum)
- elog(WARNING, "Skipping \"%s\" --- only table or database owner can ANALYZE it",
+ elog(WARNING, "Skipping \"%s\" --- can not process indexes, views or special system tables",
RelationGetRelationName(onerel));
- heap_close(onerel, NoLock);
+ relation_close(onerel, AccessShareLock);
+ CommitTransactionCommand();
+ return;
+ }
+
+ /*
+ * We can ANALYZE any table except pg_statistic. See update_attstats
+ */
+ if (RelationGetNamespace(onerel) == PG_CATALOG_NAMESPACE &&
+ strcmp(RelationGetRelationName(onerel), StatisticRelationName) == 0)
+ {
+ relation_close(onerel, AccessShareLock);
CommitTransactionCommand();
return;
}
- elog(elevel, "Analyzing %s", RelationGetRelationName(onerel));
+ elog(elevel, "Analyzing %s.%s",
+ get_namespace_name(RelationGetNamespace(onerel)),
+ RelationGetRelationName(onerel));
/*
* Determine which columns to analyze
*/
if (attr_cnt <= 0)
{
- heap_close(onerel, NoLock);
+ relation_close(onerel, NoLock);
CommitTransactionCommand();
return;
}
* before we commit. (If someone did, they'd fail to clean up the
* entries we made in pg_statistic.)
*/
- heap_close(onerel, NoLock);
+ relation_close(onerel, NoLock);
/* Commit and release working memory */
CommitTransactionCommand();
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.220 2002/03/31 06:26:30 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.221 2002/04/02 01:03:05 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "access/xlog.h"
#include "catalog/catalog.h"
#include "catalog/catname.h"
+#include "catalog/namespace.h"
#include "catalog/pg_database.h"
#include "catalog/pg_index.h"
#include "commands/vacuum.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/inval.h"
+#include "utils/lsyscache.h"
#include "utils/relcache.h"
#include "utils/syscache.h"
#include "pgstat.h"
-typedef struct VRelListData
-{
- Oid vrl_relid;
- struct VRelListData *vrl_next;
-} VRelListData;
-
-typedef VRelListData *VRelList;
-
typedef struct VacPageData
{
BlockNumber blkno; /* BlockNumber of this Page */
/* non-export function prototypes */
static void vacuum_init(VacuumStmt *vacstmt);
static void vacuum_shutdown(VacuumStmt *vacstmt);
-static VRelList getrels(Name VacRelP, const char *stmttype);
+static List *getrels(const RangeVar *vacrel, const char *stmttype);
static void vac_update_dbstats(Oid dbid,
TransactionId vacuumXID,
TransactionId frozenXID);
static void vac_truncate_clog(TransactionId vacuumXID,
TransactionId frozenXID);
-static void vacuum_rel(Oid relid, VacuumStmt *vacstmt);
+static void vacuum_rel(Oid relid, VacuumStmt *vacstmt, char expected_relkind);
static void full_vacuum_rel(Relation onerel, VacuumStmt *vacstmt);
static void scan_heap(VRelStats *vacrelstats, Relation onerel,
VacPageList vacuum_pages, VacPageList fraged_pages);
vacuum(VacuumStmt *vacstmt)
{
const char *stmttype = vacstmt->vacuum ? "VACUUM" : "ANALYZE";
- NameData VacRel;
- Name VacRelName;
- VRelList vrl,
- cur;
+ List *vrl,
+ *cur;
+
+ if (vacstmt->verbose)
+ elevel = INFO;
+ else
+ elevel = DEBUG1;
/*
* We cannot run VACUUM inside a user transaction block; if we were
*/
pgstat_vacuum_tabstat();
- if (vacstmt->verbose)
- elevel = INFO;
- else
- elevel = DEBUG1;
-
/*
* Create special memory context for cross-transaction storage.
*
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
- /* Convert relname, which is just a string, to a Name */
- if (vacstmt->relation)
- {
- namestrcpy(&VacRel, vacstmt->relation->relname);
- VacRelName = &VacRel;
- }
- else
- VacRelName = NULL;
-
/* Build list of relations to process (note this lives in vac_context) */
- vrl = getrels(VacRelName, stmttype);
+ vrl = getrels(vacstmt->relation, stmttype);
/*
* Start up the vacuum cleaner.
* ANALYZE part runs as a separate transaction from the VACUUM to
* further reduce locking.
*/
- for (cur = vrl; cur != (VRelList) NULL; cur = cur->vrl_next)
+ foreach(cur, vrl)
{
+ Oid relid = (Oid) lfirsti(cur);
+
if (vacstmt->vacuum)
- vacuum_rel(cur->vrl_relid, vacstmt);
+ vacuum_rel(relid, vacstmt, RELKIND_RELATION);
if (vacstmt->analyze)
- analyze_rel(cur->vrl_relid, vacstmt);
+ analyze_rel(relid, vacstmt);
}
/* clean up */
}
/*
- * Build a list of VRelListData nodes for each relation to be processed
+ * Build a list of Oids for each relation to be processed
*
* The list is built in vac_context so that it will survive across our
* per-relation transactions.
*/
-static VRelList
-getrels(Name VacRelP, const char *stmttype)
+static List *
+getrels(const RangeVar *vacrel, const char *stmttype)
{
- Relation rel;
- TupleDesc tupdesc;
- HeapScanDesc scan;
- HeapTuple tuple;
- VRelList vrl,
- cur;
- Datum d;
- char *rname;
- char rkind;
- bool n;
- ScanKeyData key;
-
- if (VacRelP)
+ List *vrl = NIL;
+ MemoryContext oldcontext;
+
+ if (vacrel)
{
- /*
- * we could use the cache here, but it is clearer to use scankeys
- * for both vacuum cases, bjm 2000/01/19
- */
- ScanKeyEntryInitialize(&key, 0x0, Anum_pg_class_relname,
- F_NAMEEQ,
- PointerGetDatum(NameStr(*VacRelP)));
+ /* Process specific relation */
+ Oid relid;
+
+ relid = RangeVarGetRelid(vacrel, false);
+
+ /* Make a relation list entry for this guy */
+ oldcontext = MemoryContextSwitchTo(vac_context);
+ vrl = lappendi(vrl, relid);
+ MemoryContextSwitchTo(oldcontext);
}
else
{
- /* find all plain relations listed in pg_class */
- ScanKeyEntryInitialize(&key, 0x0, Anum_pg_class_relkind,
- F_CHAREQ, CharGetDatum(RELKIND_RELATION));
- }
-
- vrl = cur = (VRelList) NULL;
-
- rel = heap_openr(RelationRelationName, AccessShareLock);
- tupdesc = RelationGetDescr(rel);
-
- scan = heap_beginscan(rel, false, SnapshotNow, 1, &key);
+ /* Process all plain relations listed in pg_class */
+ Relation pgclass;
+ HeapScanDesc scan;
+ HeapTuple tuple;
+ ScanKeyData key;
- while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
- {
- d = heap_getattr(tuple, Anum_pg_class_relname, tupdesc, &n);
- rname = (char *) DatumGetName(d);
+ ScanKeyEntryInitialize(&key, 0x0,
+ Anum_pg_class_relkind,
+ F_CHAREQ,
+ CharGetDatum(RELKIND_RELATION));
- d = heap_getattr(tuple, Anum_pg_class_relkind, tupdesc, &n);
- rkind = DatumGetChar(d);
+ pgclass = heap_openr(RelationRelationName, AccessShareLock);
- if (rkind != RELKIND_RELATION)
- {
- elog(WARNING, "%s: can not process indexes, views or special system tables",
- stmttype);
- continue;
- }
+ scan = heap_beginscan(pgclass, false, SnapshotNow, 1, &key);
- /* Make a relation list entry for this guy */
- if (vrl == (VRelList) NULL)
- vrl = cur = (VRelList)
- MemoryContextAlloc(vac_context, sizeof(VRelListData));
- else
+ while (HeapTupleIsValid(tuple = heap_getnext(scan, 0)))
{
- cur->vrl_next = (VRelList)
- MemoryContextAlloc(vac_context, sizeof(VRelListData));
- cur = cur->vrl_next;
+ /* Make a relation list entry for this guy */
+ oldcontext = MemoryContextSwitchTo(vac_context);
+ vrl = lappendi(vrl, tuple->t_data->t_oid);
+ MemoryContextSwitchTo(oldcontext);
}
- cur->vrl_relid = tuple->t_data->t_oid;
- cur->vrl_next = (VRelList) NULL;
+ heap_endscan(scan);
+ heap_close(pgclass, AccessShareLock);
}
- heap_endscan(scan);
- heap_close(rel, AccessShareLock);
-
- if (vrl == NULL)
- elog(WARNING, "%s: table not found", stmttype);
-
return vrl;
}
* At entry and exit, we are not inside a transaction.
*/
static void
-vacuum_rel(Oid relid, VacuumStmt *vacstmt)
+vacuum_rel(Oid relid, VacuumStmt *vacstmt, char expected_relkind)
{
LOCKMODE lmode;
Relation onerel;
* Note we choose to treat permissions failure as a WARNING and keep
* trying to vacuum the rest of the DB --- is this appropriate?
*/
- onerel = heap_open(relid, lmode);
+ onerel = relation_open(relid, lmode);
if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
(is_dbadmin(MyDatabaseId) && !onerel->rd_rel->relisshared)))
{
elog(WARNING, "Skipping \"%s\" --- only table or database owner can VACUUM it",
RelationGetRelationName(onerel));
- heap_close(onerel, lmode);
+ relation_close(onerel, lmode);
+ CommitTransactionCommand();
+ return;
+ }
+
+ /*
+ * Check that it's a plain table; we used to do this in getrels() but
+ * seems safer to check after we've locked the relation.
+ */
+ if (onerel->rd_rel->relkind != expected_relkind)
+ {
+ elog(WARNING, "Skipping \"%s\" --- can not process indexes, views or special system tables",
+ RelationGetRelationName(onerel));
+ relation_close(onerel, lmode);
CommitTransactionCommand();
return;
}
lazy_vacuum_rel(onerel, vacstmt);
/* all done with this class, but hold lock until commit */
- heap_close(onerel, NoLock);
+ relation_close(onerel, NoLock);
/*
* Complete the transaction and free all temporary memory used.
* statistics are totally unimportant for toast relations.
*/
if (toast_relid != InvalidOid)
- vacuum_rel(toast_relid, vacstmt);
+ vacuum_rel(toast_relid, vacstmt, RELKIND_TOASTVALUE);
/*
* Now release the session-level lock on the master table.
vac_init_rusage(&ru0);
relname = RelationGetRelationName(onerel);
- elog(elevel, "--Relation %s--", relname);
+ elog(elevel, "--Relation %s.%s--",
+ get_namespace_name(RelationGetNamespace(onerel)),
+ relname);
empty_pages = new_pages = changed_pages = empty_end_pages = 0;
num_tuples = tups_vacuumed = nkeep = nunused = 0;