Fix up pgstats counting of live and dead tuples to recognize that committed

and aborted transactions have different effects; also teach it not to assume
that prepared transactions are always committed.

Along the way, simplify the pgstats API by tying counting directly to
Relations; I cannot detect any redeeming social value in having stats
pointers in HeapScanDesc and IndexScanDesc structures.  And fix a few
corner cases in which counts might be missed because the relation's
pgstat_info pointer hadn't been set.
This commit is contained in:
Tom Lane 2007-05-27 03:50:39 +00:00
parent cadb78330e
commit 77947c51c0
20 changed files with 805 additions and 417 deletions

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gin/ginscan.c,v 1.9 2007/01/31 15:09:45 teodor Exp $
* $PostgreSQL: pgsql/src/backend/access/gin/ginscan.c,v 1.10 2007/05/27 03:50:38 tgl Exp $
*-------------------------------------------------------------------------
*/
@ -189,7 +189,7 @@ newScanKey(IndexScanDesc scan)
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("GIN index does not support search with void query")));
pgstat_count_index_scan(&scan->xs_pgstat_info);
pgstat_count_index_scan(scan->indexRelation);
}
Datum

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.65 2007/04/06 22:33:41 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.66 2007/05/27 03:50:38 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -165,7 +165,7 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids,
stk->next = NULL;
stk->block = GIST_ROOT_BLKNO;
pgstat_count_index_scan(&scan->xs_pgstat_info);
pgstat_count_index_scan(scan->indexRelation);
}
else if (so->curbuf == InvalidBuffer)
{

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/hash/hashsearch.c,v 1.49 2007/05/03 16:45:58 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/hash/hashsearch.c,v 1.50 2007/05/27 03:50:38 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -127,7 +127,7 @@ _hash_first(IndexScanDesc scan, ScanDirection dir)
ItemPointer current;
OffsetNumber offnum;
pgstat_count_index_scan(&scan->xs_pgstat_info);
pgstat_count_index_scan(rel);
current = &(so->hashso_curpos);
ItemPointerSetInvalid(current);

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.232 2007/04/08 01:26:27 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.233 2007/05/27 03:50:38 tgl Exp $
*
*
* INTERFACE ROUTINES
@ -100,7 +100,7 @@ initscan(HeapScanDesc scan, ScanKey key)
if (key != NULL)
memcpy(scan->rs_key, key, scan->rs_nkeys * sizeof(ScanKeyData));
pgstat_count_heap_scan(&scan->rs_pgstat_info);
pgstat_count_heap_scan(scan->rs_rd);
}
/*
@ -701,6 +701,8 @@ relation_open(Oid relationId, LOCKMODE lockmode)
if (!RelationIsValid(r))
elog(ERROR, "could not open relation with OID %u", relationId);
pgstat_initstats(r);
return r;
}
@ -743,6 +745,8 @@ try_relation_open(Oid relationId, LOCKMODE lockmode)
if (!RelationIsValid(r))
elog(ERROR, "could not open relation with OID %u", relationId);
pgstat_initstats(r);
return r;
}
@ -787,6 +791,8 @@ relation_open_nowait(Oid relationId, LOCKMODE lockmode)
if (!RelationIsValid(r))
elog(ERROR, "could not open relation with OID %u", relationId);
pgstat_initstats(r);
return r;
}
@ -873,8 +879,6 @@ heap_open(Oid relationId, LOCKMODE lockmode)
errmsg("\"%s\" is a composite type",
RelationGetRelationName(r))));
pgstat_initstats(&r->pgstat_info, r);
return r;
}
@ -903,8 +907,6 @@ heap_openrv(const RangeVar *relation, LOCKMODE lockmode)
errmsg("\"%s\" is a composite type",
RelationGetRelationName(r))));
pgstat_initstats(&r->pgstat_info, r);
return r;
}
@ -954,8 +956,6 @@ heap_beginscan(Relation relation, Snapshot snapshot,
else
scan->rs_key = NULL;
pgstat_initstats(&scan->rs_pgstat_info, relation);
initscan(scan, key);
return scan;
@ -1059,7 +1059,7 @@ heap_getnext(HeapScanDesc scan, ScanDirection direction)
*/
HEAPDEBUG_3; /* heap_getnext returning tuple */
pgstat_count_heap_getnext(&scan->rs_pgstat_info);
pgstat_count_heap_getnext(scan->rs_rd);
return &(scan->rs_ctup);
}
@ -1086,6 +1086,10 @@ heap_getnext(HeapScanDesc scan, ScanDirection direction)
* and return it in *userbuf (so the caller must eventually unpin it); when
* keep_buf = false, the pin is released and *userbuf is set to InvalidBuffer.
*
* stats_relation is the relation to charge the heap_fetch operation against
* for statistical purposes. (This could be the heap rel itself, an
* associated index, or NULL to not count the fetch at all.)
*
* It is somewhat inconsistent that we ereport() on invalid block number but
* return false on invalid item number. There are a couple of reasons though.
* One is that the caller can relatively easily check the block number for
@ -1101,12 +1105,12 @@ heap_fetch(Relation relation,
HeapTuple tuple,
Buffer *userbuf,
bool keep_buf,
PgStat_Info *pgstat_info)
Relation stats_relation)
{
/* Assume *userbuf is undefined on entry */
*userbuf = InvalidBuffer;
return heap_release_fetch(relation, snapshot, tuple,
userbuf, keep_buf, pgstat_info);
userbuf, keep_buf, stats_relation);
}
/*
@ -1125,7 +1129,7 @@ heap_release_fetch(Relation relation,
HeapTuple tuple,
Buffer *userbuf,
bool keep_buf,
PgStat_Info *pgstat_info)
Relation stats_relation)
{
ItemPointer tid = &(tuple->t_self);
ItemId lp;
@ -1210,9 +1214,9 @@ heap_release_fetch(Relation relation,
*/
*userbuf = buffer;
/* Count the successful fetch in *pgstat_info, if given. */
if (pgstat_info != NULL)
pgstat_count_heap_fetch(pgstat_info);
/* Count the successful fetch against appropriate rel, if any */
if (stats_relation != NULL)
pgstat_count_heap_fetch(stats_relation);
return true;
}
@ -1517,7 +1521,7 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
*/
CacheInvalidateHeapTuple(relation, heaptup);
pgstat_count_heap_insert(&relation->pgstat_info);
pgstat_count_heap_insert(relation);
/*
* If heaptup is a private copy, release it. Don't forget to copy t_self
@ -1807,7 +1811,7 @@ l1:
if (have_tuple_lock)
UnlockTuple(relation, &(tp.t_self), ExclusiveLock);
pgstat_count_heap_delete(&relation->pgstat_info);
pgstat_count_heap_delete(relation);
return HeapTupleMayBeUpdated;
}
@ -2269,7 +2273,7 @@ l2:
if (have_tuple_lock)
UnlockTuple(relation, &(oldtup.t_self), ExclusiveLock);
pgstat_count_heap_update(&relation->pgstat_info);
pgstat_count_heap_update(relation);
/*
* If heaptup is a private copy, release it. Don't forget to copy t_self

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/index/genam.c,v 1.61 2007/01/20 18:43:35 neilc Exp $
* $PostgreSQL: pgsql/src/backend/access/index/genam.c,v 1.62 2007/05/27 03:50:38 tgl Exp $
*
* NOTES
* many of the old access method routines have been turned into
@ -96,8 +96,6 @@ RelationGetIndexScan(Relation indexRelation,
scan->xs_ctup.t_data = NULL;
scan->xs_cbuf = InvalidBuffer;
pgstat_initstats(&scan->xs_pgstat_info, indexRelation);
/*
* Let the AM fill in the key and any opaque data it wants.
*/

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.97 2007/01/05 22:19:23 momjian Exp $
* $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.98 2007/05/27 03:50:38 tgl Exp $
*
* INTERFACE ROUTINES
* index_open - open an index relation by relation OID
@ -145,8 +145,6 @@ index_open(Oid relationId, LOCKMODE lockmode)
errmsg("\"%s\" is not an index",
RelationGetRelationName(r))));
pgstat_initstats(&r->pgstat_info, r);
return r;
}
@ -433,14 +431,14 @@ index_getnext(IndexScanDesc scan, ScanDirection direction)
return NULL; /* failure exit */
}
pgstat_count_index_tuples(&scan->xs_pgstat_info, 1);
pgstat_count_index_tuples(scan->indexRelation, 1);
/*
* Fetch the heap tuple and see if it matches the snapshot.
*/
if (heap_release_fetch(scan->heapRelation, scan->xs_snapshot,
heapTuple, &scan->xs_cbuf, true,
&scan->xs_pgstat_info))
scan->indexRelation))
break;
/* Skip if no undeleted tuple at this location */
@ -502,7 +500,7 @@ index_getnext_indexitem(IndexScanDesc scan,
Int32GetDatum(direction)));
if (found)
pgstat_count_index_tuples(&scan->xs_pgstat_info, 1);
pgstat_count_index_tuples(scan->indexRelation, 1);
return found;
}
@ -543,7 +541,7 @@ index_getmulti(IndexScanDesc scan,
Int32GetDatum(max_tids),
PointerGetDatum(returned_tids)));
pgstat_count_index_tuples(&scan->xs_pgstat_info, *returned_tids);
pgstat_count_index_tuples(scan->indexRelation, *returned_tids);
return found;
}

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.112 2007/04/06 22:33:42 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.113 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -453,7 +453,7 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
int i;
StrategyNumber strat_total;
pgstat_count_index_scan(&scan->xs_pgstat_info);
pgstat_count_index_scan(rel);
/*
* Examine the scan keys and eliminate any redundant keys; also mark the

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.30 2007/04/30 21:01:52 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.31 2007/05/27 03:50:39 tgl Exp $
*
* NOTES
* Each global transaction is associated with a global transaction
@ -1211,7 +1211,8 @@ FinishPreparedTransaction(const char *gid, bool isCommit)
else
ProcessRecords(bufptr, xid, twophase_postabort_callbacks);
pgstat_count_xact_commit();
/* Count the prepared xact as committed or aborted */
AtEOXact_PgStat(isCommit);
/*
* And now we can clean up our mess.

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/transam/twophase_rmgr.c,v 1.4 2007/01/05 22:19:23 momjian Exp $
* $PostgreSQL: pgsql/src/backend/access/transam/twophase_rmgr.c,v 1.5 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -16,6 +16,7 @@
#include "access/twophase_rmgr.h"
#include "commands/async.h"
#include "pgstat.h"
#include "storage/lock.h"
#include "utils/flatfiles.h"
#include "utils/inval.h"
@ -27,7 +28,8 @@ const TwoPhaseCallback twophase_recover_callbacks[TWOPHASE_RM_MAX_ID + 1] =
lock_twophase_recover, /* Lock */
NULL, /* Inval */
NULL, /* flat file update */
NULL /* notify/listen */
NULL, /* notify/listen */
NULL /* pgstat */
};
const TwoPhaseCallback twophase_postcommit_callbacks[TWOPHASE_RM_MAX_ID + 1] =
@ -36,7 +38,8 @@ const TwoPhaseCallback twophase_postcommit_callbacks[TWOPHASE_RM_MAX_ID + 1] =
lock_twophase_postcommit, /* Lock */
inval_twophase_postcommit, /* Inval */
flatfile_twophase_postcommit, /* flat file update */
notify_twophase_postcommit /* notify/listen */
notify_twophase_postcommit, /* notify/listen */
pgstat_twophase_postcommit /* pgstat */
};
const TwoPhaseCallback twophase_postabort_callbacks[TWOPHASE_RM_MAX_ID + 1] =
@ -45,5 +48,6 @@ const TwoPhaseCallback twophase_postabort_callbacks[TWOPHASE_RM_MAX_ID + 1] =
lock_twophase_postabort, /* Lock */
NULL, /* Inval */
NULL, /* flat file update */
NULL /* notify/listen */
NULL, /* notify/listen */
pgstat_twophase_postabort /* pgstat */
};

View File

@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.242 2007/04/30 21:01:52 tgl Exp $
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.243 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -1661,8 +1661,7 @@ CommitTransaction(void)
AtEOXact_Files();
AtEOXact_ComboCid();
AtEOXact_HashTables(true);
pgstat_clear_snapshot();
pgstat_count_xact_commit();
AtEOXact_PgStat(true);
pgstat_report_txn_timestamp(0);
CurrentResourceOwner = NULL;
@ -1796,6 +1795,7 @@ PrepareTransaction(void)
AtPrepare_UpdateFlatFiles();
AtPrepare_Inval();
AtPrepare_Locks();
AtPrepare_PgStat();
/*
* Here is where we really truly prepare.
@ -1853,6 +1853,8 @@ PrepareTransaction(void)
/* notify and flatfiles don't need a postprepare call */
PostPrepare_PgStat();
PostPrepare_Inval();
PostPrepare_smgr();
@ -1880,7 +1882,7 @@ PrepareTransaction(void)
AtEOXact_Files();
AtEOXact_ComboCid();
AtEOXact_HashTables(true);
pgstat_clear_snapshot();
/* don't call AtEOXact_PgStat here */
CurrentResourceOwner = NULL;
ResourceOwnerDelete(TopTransactionResourceOwner);
@ -2035,8 +2037,7 @@ AbortTransaction(void)
AtEOXact_Files();
AtEOXact_ComboCid();
AtEOXact_HashTables(false);
pgstat_clear_snapshot();
pgstat_count_xact_rollback();
AtEOXact_PgStat(false);
pgstat_report_txn_timestamp(0);
/*
@ -3749,6 +3750,7 @@ CommitSubTransaction(void)
AtEOSubXact_Files(true, s->subTransactionId,
s->parent->subTransactionId);
AtEOSubXact_HashTables(true, s->nestingLevel);
AtEOSubXact_PgStat(true, s->nestingLevel);
/*
* We need to restore the upper transaction's read-only state, in case the
@ -3861,6 +3863,7 @@ AbortSubTransaction(void)
AtEOSubXact_Files(false, s->subTransactionId,
s->parent->subTransactionId);
AtEOSubXact_HashTables(false, s->nestingLevel);
AtEOSubXact_PgStat(false, s->nestingLevel);
}
/*

View File

@ -21,7 +21,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/nodeBitmapHeapscan.c,v 1.16 2007/01/05 22:19:28 momjian Exp $
* $PostgreSQL: pgsql/src/backend/executor/nodeBitmapHeapscan.c,v 1.17 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -189,7 +189,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
scan->rs_ctup.t_len = ItemIdGetLength(lp);
ItemPointerSet(&scan->rs_ctup.t_self, tbmres->blockno, targoffset);
pgstat_count_heap_fetch(&scan->rs_pgstat_info);
pgstat_count_heap_fetch(scan->rs_rd);
/*
* Set up the result slot to point to this tuple. Note that the slot
@ -389,7 +389,7 @@ ExecBitmapHeapReScan(BitmapHeapScanState *node, ExprContext *exprCtxt)
heap_rescan(node->ss.ss_currentScanDesc, NULL);
/* undo bogus "seq scan" count (see notes in ExecInitBitmapHeapScan) */
pgstat_discount_heap_scan(&node->ss.ss_currentScanDesc->rs_pgstat_info);
pgstat_discount_heap_scan(node->ss.ss_currentScanDesc->rs_rd);
if (node->tbm)
tbm_free(node->tbm);
@ -535,7 +535,7 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags)
* when we actually aren't doing any such thing. Reverse out the added
* scan count. (Eventually we may want to count bitmap scans separately.)
*/
pgstat_discount_heap_scan(&scanstate->ss.ss_currentScanDesc->rs_pgstat_info);
pgstat_discount_heap_scan(scanstate->ss.ss_currentScanDesc->rs_rd);
/*
* get the scan type from the relation descriptor.

View File

@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.37 2007/03/30 18:34:55 mha Exp $
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.38 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -125,13 +125,6 @@ typedef struct
static BgWriterShmemStruct *BgWriterShmem;
/*
* BgWriter statistics counters.
* Stored directly in a stats message structure so it can be sent
* without needing to copy things around.
*/
PgStat_MsgBgWriter BgWriterStats;
/*
* GUC parameters
*/
@ -250,11 +243,6 @@ BackgroundWriterMain(void)
ALLOCSET_DEFAULT_MAXSIZE);
MemoryContextSwitchTo(bgwriter_context);
/*
* Initialize statistics counters to zero
*/
memset(&BgWriterStats, 0, sizeof(BgWriterStats));
/*
* If an exception is encountered, processing resumes here.
*

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.218 2007/05/02 23:34:48 tgl Exp $
* $PostgreSQL: pgsql/src/backend/storage/buffer/bufmgr.c,v 1.219 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -88,12 +88,6 @@ static bool IsForInput;
/* local state for LockBufferForCleanup */
static volatile BufferDesc *PinCountWaitBuf = NULL;
/*
* Global statistics for the bgwriter. The contents of this variable
* only makes sense in the bgwriter process.
*/
extern PgStat_MsgBgWriter BgWriterStats;
static Buffer ReadBuffer_common(Relation reln, BlockNumber blockNum,
bool zeroPage);
@ -174,7 +168,7 @@ ReadBuffer_common(Relation reln, BlockNumber blockNum, bool zeroPage)
if (isExtend)
blockNum = smgrnblocks(reln->rd_smgr);
pgstat_count_buffer_read(&reln->pgstat_info, reln);
pgstat_count_buffer_read(reln);
if (isLocalBuf)
{
@ -204,7 +198,7 @@ ReadBuffer_common(Relation reln, BlockNumber blockNum, bool zeroPage)
if (!isExtend)
{
/* Just need to update stats before we exit */
pgstat_count_buffer_hit(&reln->pgstat_info, reln);
pgstat_count_buffer_hit(reln);
if (VacuumCostActive)
VacuumCostBalance += VacuumCostPageHit;

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.260 2007/05/02 21:08:46 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.261 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -1802,6 +1802,7 @@ RelationClearRelation(Relation relation, bool rebuild)
int old_refcnt = relation->rd_refcnt;
SubTransactionId old_createSubid = relation->rd_createSubid;
SubTransactionId old_newRelfilenodeSubid = relation->rd_newRelfilenodeSubid;
struct PgStat_TableStatus *old_pgstat_info = relation->pgstat_info;
TupleDesc old_att = relation->rd_att;
RuleLock *old_rules = relation->rd_rules;
MemoryContext old_rulescxt = relation->rd_rulescxt;
@ -1821,6 +1822,7 @@ RelationClearRelation(Relation relation, bool rebuild)
relation->rd_refcnt = old_refcnt;
relation->rd_createSubid = old_createSubid;
relation->rd_newRelfilenodeSubid = old_newRelfilenodeSubid;
relation->pgstat_info = old_pgstat_info;
if (equalTupleDescs(old_att, relation->rd_att))
{

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.123 2007/04/08 01:26:33 tgl Exp $
* $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.124 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -147,10 +147,10 @@ extern HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction);
extern bool heap_fetch(Relation relation, Snapshot snapshot,
HeapTuple tuple, Buffer *userbuf, bool keep_buf,
PgStat_Info *pgstat_info);
Relation stats_relation);
extern bool heap_release_fetch(Relation relation, Snapshot snapshot,
HeapTuple tuple, Buffer *userbuf, bool keep_buf,
PgStat_Info *pgstat_info);
Relation stats_relation);
extern void heap_get_latest_tid(Relation relation, Snapshot snapshot,
ItemPointer tid);

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/access/relscan.h,v 1.52 2007/01/20 18:43:35 neilc Exp $
* $PostgreSQL: pgsql/src/include/access/relscan.h,v 1.53 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -37,8 +37,6 @@ typedef struct HeapScanDescData
/* NB: if rs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
ItemPointerData rs_mctid; /* marked scan position, if any */
PgStat_Info rs_pgstat_info; /* statistics collector hook */
/* these fields only used in page-at-a-time mode */
int rs_cindex; /* current tuple's index in vistuples */
int rs_mindex; /* marked tuple's saved index */
@ -78,8 +76,6 @@ typedef struct IndexScanDescData
HeapTupleData xs_ctup; /* current heap tuple, if any */
Buffer xs_cbuf; /* current heap buffer in scan, if any */
/* NB: if xs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
PgStat_Info xs_pgstat_info; /* statistics collector hook */
} IndexScanDescData;
typedef IndexScanDescData *IndexScanDesc;

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/access/twophase_rmgr.h,v 1.4 2007/01/05 22:19:51 momjian Exp $
* $PostgreSQL: pgsql/src/include/access/twophase_rmgr.h,v 1.5 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -26,7 +26,8 @@ typedef uint8 TwoPhaseRmgrId;
#define TWOPHASE_RM_INVAL_ID 2
#define TWOPHASE_RM_FLATFILES_ID 3
#define TWOPHASE_RM_NOTIFY_ID 4
#define TWOPHASE_RM_MAX_ID TWOPHASE_RM_NOTIFY_ID
#define TWOPHASE_RM_PGSTAT_ID 5
#define TWOPHASE_RM_MAX_ID TWOPHASE_RM_PGSTAT_ID
extern const TwoPhaseCallback twophase_recover_callbacks[];
extern const TwoPhaseCallback twophase_postcommit_callbacks[];

View File

@ -5,7 +5,7 @@
*
* Copyright (c) 2001-2007, PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.58 2007/04/30 16:37:08 tgl Exp $
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.59 2007/05/27 03:50:39 tgl Exp $
* ----------
*/
#ifndef PGSTAT_H
@ -40,6 +40,90 @@ typedef enum StatMsgType
*/
typedef int64 PgStat_Counter;
/* ----------
* PgStat_TableCounts The actual per-table counts kept by a backend
*
* This struct should contain only actual event counters, because we memcmp
* it against zeroes to detect whether there are any counts to transmit.
* It is a component of PgStat_TableStatus (within-backend state) and
* PgStat_TableEntry (the transmitted message format).
*
* Note: for a table, tuples_returned is the number of tuples successfully
* fetched by heap_getnext, while tuples_fetched is the number of tuples
* successfully fetched by heap_fetch under the control of bitmap indexscans.
* For an index, tuples_returned is the number of index entries returned by
* the index AM, while tuples_fetched is the number of tuples successfully
* fetched by heap_fetch under the control of simple indexscans for this index.
*
* tuples_inserted/tuples_updated/tuples_deleted count attempted actions,
* regardless of whether the transaction committed. new_live_tuples and
* new_dead_tuples are properly adjusted depending on commit or abort.
* ----------
*/
typedef struct PgStat_TableCounts
{
PgStat_Counter t_numscans;
PgStat_Counter t_tuples_returned;
PgStat_Counter t_tuples_fetched;
PgStat_Counter t_tuples_inserted;
PgStat_Counter t_tuples_updated;
PgStat_Counter t_tuples_deleted;
PgStat_Counter t_new_live_tuples;
PgStat_Counter t_new_dead_tuples;
PgStat_Counter t_blocks_fetched;
PgStat_Counter t_blocks_hit;
} PgStat_TableCounts;
/* ------------------------------------------------------------
* Structures kept in backend local memory while accumulating counts
* ------------------------------------------------------------
*/
/* ----------
* PgStat_TableStatus Per-table status within a backend
*
* Most of the event counters are nontransactional, ie, we count events
* in committed and aborted transactions alike. For these, we just count
* directly in the PgStat_TableStatus. However, new_live_tuples and
* new_dead_tuples must be derived from tuple insertion and deletion counts
* with awareness of whether the transaction or subtransaction committed or
* aborted. Hence, we also keep a stack of per-(sub)transaction status
* records for every table modified in the current transaction. At commit
* or abort, we propagate tuples_inserted and tuples_deleted up to the
* parent subtransaction level, or out to the parent PgStat_TableStatus,
* as appropriate.
* ----------
*/
typedef struct PgStat_TableStatus
{
Oid t_id; /* table's OID */
bool t_shared; /* is it a shared catalog? */
struct PgStat_TableXactStatus *trans; /* lowest subxact's counts */
PgStat_TableCounts t_counts; /* event counts to be sent */
} PgStat_TableStatus;
/* ----------
* PgStat_TableXactStatus Per-table, per-subtransaction status
* ----------
*/
typedef struct PgStat_TableXactStatus
{
PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */
int nest_level; /* subtransaction nest level */
/* links to other structs for same relation: */
struct PgStat_TableXactStatus *upper; /* next higher subxact if any */
PgStat_TableStatus *parent; /* per-table status */
/* structs of same subxact level are linked here: */
struct PgStat_TableXactStatus *next; /* next of same subxact */
} PgStat_TableXactStatus;
/* ------------------------------------------------------------
* Message formats follow
@ -78,30 +162,12 @@ typedef struct PgStat_MsgDummy
/* ----------
* PgStat_TableEntry Per-table info in a MsgTabstat
*
* Note: for a table, tuples_returned is the number of tuples successfully
* fetched by heap_getnext, while tuples_fetched is the number of tuples
* successfully fetched by heap_fetch under the control of bitmap indexscans.
* For an index, tuples_returned is the number of index entries returned by
* the index AM, while tuples_fetched is the number of tuples successfully
* fetched by heap_fetch under the control of simple indexscans for this index.
* ----------
*/
typedef struct PgStat_TableEntry
{
Oid t_id;
PgStat_Counter t_numscans;
PgStat_Counter t_tuples_returned;
PgStat_Counter t_tuples_fetched;
PgStat_Counter t_tuples_inserted;
PgStat_Counter t_tuples_updated;
PgStat_Counter t_tuples_deleted;
PgStat_Counter t_blocks_fetched;
PgStat_Counter t_blocks_hit;
PgStat_TableCounts t_counts;
} PgStat_TableEntry;
/* ----------
@ -393,6 +459,10 @@ extern bool pgstat_collect_tuplelevel;
extern bool pgstat_collect_blocklevel;
extern bool pgstat_collect_querystring;
/*
* BgWriter statistics counters are updated directly by bgwriter and bufmgr
*/
extern PgStat_MsgBgWriter BgWriterStats;
/* ----------
* Functions called from postmaster
@ -436,83 +506,67 @@ extern void pgstat_report_activity(const char *what);
extern void pgstat_report_txn_timestamp(TimestampTz tstamp);
extern void pgstat_report_waiting(bool waiting);
extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
extern void pgstat_initstats(Relation rel);
/* nontransactional event counts are simple enough to inline */
#define pgstat_count_heap_scan(s) \
#define pgstat_count_heap_scan(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_numscans++; \
if (pgstat_collect_tuplelevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_numscans++; \
} while (0)
/* kluge for bitmap scans: */
#define pgstat_discount_heap_scan(s) \
#define pgstat_discount_heap_scan(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_numscans--; \
if (pgstat_collect_tuplelevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_numscans--; \
} while (0)
#define pgstat_count_heap_getnext(s) \
#define pgstat_count_heap_getnext(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
if (pgstat_collect_tuplelevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_tuples_returned++; \
} while (0)
#define pgstat_count_heap_fetch(s) \
#define pgstat_count_heap_fetch(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
if (pgstat_collect_tuplelevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_tuples_fetched++; \
} while (0)
#define pgstat_count_heap_insert(s) \
#define pgstat_count_index_scan(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
if (pgstat_collect_tuplelevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_numscans++; \
} while (0)
#define pgstat_count_heap_update(s) \
#define pgstat_count_index_tuples(rel, n) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
if (pgstat_collect_tuplelevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
} while (0)
#define pgstat_count_heap_delete(s) \
#define pgstat_count_buffer_read(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
if (pgstat_collect_blocklevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_blocks_fetched++; \
} while (0)
#define pgstat_count_index_scan(s) \
#define pgstat_count_buffer_hit(rel) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_numscans++; \
} while (0)
#define pgstat_count_index_tuples(s, n) \
do { \
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned += (n); \
} while (0)
#define pgstat_count_buffer_read(s,r) \
do { \
if (pgstat_collect_blocklevel) { \
if ((s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
else { \
pgstat_initstats((s), (r)); \
if ((s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
} \
} \
} while (0)
#define pgstat_count_buffer_hit(s,r) \
do { \
if (pgstat_collect_blocklevel) { \
if ((s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
else { \
pgstat_initstats((s), (r)); \
if ((s)->tabentry != NULL) \
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
} \
} \
if (pgstat_collect_blocklevel && (rel)->pgstat_info != NULL) \
(rel)->pgstat_info->t_counts.t_blocks_hit++; \
} while (0)
extern void pgstat_count_heap_insert(Relation rel);
extern void pgstat_count_heap_update(Relation rel);
extern void pgstat_count_heap_delete(Relation rel);
extern void AtEOXact_PgStat(bool isCommit);
extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
extern void AtPrepare_PgStat(void);
extern void PostPrepare_PgStat(void);
extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
void *recdata, uint32 len);
extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
void *recdata, uint32 len);
extern void pgstat_count_xact_commit(void);
extern void pgstat_count_xact_rollback(void);
extern void pgstat_send_bgwriter(void);
/* ----------

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.100 2007/03/29 00:15:39 tgl Exp $
* $PostgreSQL: pgsql/src/include/utils/rel.h,v 1.101 2007/05/27 03:50:39 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -89,15 +89,6 @@ typedef struct TriggerDesc
} TriggerDesc;
/*
* Same for the statistics collector data in Relation and scan data.
*/
typedef struct PgStat_Info
{
void *tabentry;
} PgStat_Info;
/*
* Cached lookup information for the index access method functions defined
* by the pg_am row associated with an index relation.
@ -200,8 +191,8 @@ typedef struct RelationData
List *rd_indpred; /* index predicate tree, if any */
void *rd_amcache; /* available for use by index AM */
/* statistics collection area */
PgStat_Info pgstat_info;
/* use "struct" here to avoid needing to include pgstat.h: */
struct PgStat_TableStatus *pgstat_info; /* statistics collection area */
} RelationData;
typedef RelationData *Relation;