Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cb8b661

Browse files
committed
Revise pgstats stuff to fix the problems with not counting accesses
generated by bitmap index scans. Along the way, simplify and speed up the code for counting sequential and index scans; it was both confusing and inefficient to be taking care of that in the per-tuple loops, IMHO. initdb forced because of internal changes in pg_stat view definitions.
1 parent b5aad11 commit cb8b661

File tree

16 files changed

+206
-165
lines changed

16 files changed

+206
-165
lines changed

doc/src/sgml/monitoring.sgml

Lines changed: 107 additions & 72 deletions
Large diffs are not rendered by default.

src/backend/access/gist/gistget.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.51 2005/09/22 20:44:36 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/gist/gistget.c,v 1.52 2005/10/06 02:29:07 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,8 +17,10 @@
1717
#include "access/itup.h"
1818
#include "access/gist_private.h"
1919
#include "executor/execdebug.h"
20+
#include "pgstat.h"
2021
#include "utils/memutils.h"
2122

23+
2224
static OffsetNumber gistfindnext(IndexScanDesc scan, OffsetNumber n,
2325
ScanDirection dir);
2426
static int gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, bool ignore_killed_tuples);
@@ -161,6 +163,8 @@ gistnext(IndexScanDesc scan, ScanDirection dir, ItemPointer tids, int maxtids, b
161163

162164
stk->next = NULL;
163165
stk->block = GIST_ROOT_BLKNO;
166+
167+
pgstat_count_index_scan(&scan->xs_pgstat_info);
164168
}
165169
else if (so->curbuf == InvalidBuffer)
166170
{

src/backend/access/hash/hashsearch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/hash/hashsearch.c,v 1.38 2004/12/31 21:59:13 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/hash/hashsearch.c,v 1.39 2005/10/06 02:29:08 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
1515
#include "postgres.h"
1616

1717
#include "access/hash.h"
18+
#include "pgstat.h"
1819
#include "storage/lmgr.h"
1920

2021

@@ -130,6 +131,8 @@ _hash_first(IndexScanDesc scan, ScanDirection dir)
130131
ItemPointer current;
131132
OffsetNumber offnum;
132133

134+
pgstat_count_index_scan(&scan->xs_pgstat_info);
135+
133136
current = &(scan->currentItemData);
134137
ItemPointerSetInvalid(current);
135138

src/backend/access/heap/heapam.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.198 2005/08/20 00:39:51 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.199 2005/10/06 02:29:10 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -47,10 +47,10 @@
4747
#include "catalog/catalog.h"
4848
#include "catalog/namespace.h"
4949
#include "miscadmin.h"
50+
#include "pgstat.h"
5051
#include "storage/procarray.h"
5152
#include "utils/inval.h"
5253
#include "utils/relcache.h"
53-
#include "pgstat.h"
5454

5555

5656
static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
@@ -90,6 +90,8 @@ initscan(HeapScanDesc scan, ScanKey key)
9090
*/
9191
if (key != NULL)
9292
memcpy(scan->rs_key, key, scan->rs_nkeys * sizeof(ScanKeyData));
93+
94+
pgstat_count_heap_scan(&scan->rs_pgstat_info);
9395
}
9496

9597
/* ----------------
@@ -680,8 +682,6 @@ heap_rescan(HeapScanDesc scan,
680682
* reinitialize scan descriptor
681683
*/
682684
initscan(scan, key);
683-
684-
pgstat_reset_heap_scan(&scan->rs_pgstat_info);
685685
}
686686

687687
/* ----------------
@@ -762,8 +762,6 @@ heap_getnext(HeapScanDesc scan, ScanDirection direction)
762762
return NULL;
763763
}
764764

765-
pgstat_count_heap_scan(&scan->rs_pgstat_info);
766-
767765
/*
768766
* if we get here it means we have a new current scan tuple, so point
769767
* to the proper return buffer and return the tuple.
@@ -927,14 +925,9 @@ heap_release_fetch(Relation relation,
927925
*/
928926
*userbuf = buffer;
929927

930-
/*
931-
* Count the successful fetch in *pgstat_info if given, otherwise
932-
* in the relation's default statistics area.
933-
*/
928+
/* Count the successful fetch in *pgstat_info, if given. */
934929
if (pgstat_info != NULL)
935930
pgstat_count_heap_fetch(pgstat_info);
936-
else
937-
pgstat_count_heap_fetch(&relation->pgstat_info);
938931

939932
return true;
940933
}
@@ -1152,8 +1145,6 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
11521145

11531146
RelationPutHeapTuple(relation, buffer, tup);
11541147

1155-
pgstat_count_heap_insert(&relation->pgstat_info);
1156-
11571148
/* XLOG stuff */
11581149
if (relation->rd_istemp)
11591150
{
@@ -1229,6 +1220,8 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
12291220
*/
12301221
CacheInvalidateHeapTuple(relation, tup);
12311222

1223+
pgstat_count_heap_insert(&relation->pgstat_info);
1224+
12321225
return HeapTupleGetOid(tup);
12331226
}
12341227

@@ -1481,8 +1474,6 @@ heap_delete(Relation relation, ItemPointer tid,
14811474
if (HeapTupleHasExternal(&tp))
14821475
heap_tuple_toast_attrs(relation, NULL, &tp);
14831476

1484-
pgstat_count_heap_delete(&relation->pgstat_info);
1485-
14861477
/*
14871478
* Mark tuple for invalidation from system caches at next command
14881479
* boundary. We have to do this before WriteBuffer because we need to
@@ -1499,6 +1490,8 @@ heap_delete(Relation relation, ItemPointer tid,
14991490
if (have_tuple_lock)
15001491
UnlockTuple(relation, &(tp.t_self), ExclusiveLock);
15011492

1493+
pgstat_count_heap_delete(&relation->pgstat_info);
1494+
15021495
return HeapTupleMayBeUpdated;
15031496
}
15041497

@@ -1851,8 +1844,6 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
18511844
newbuf = buffer;
18521845
}
18531846

1854-
pgstat_count_heap_update(&relation->pgstat_info);
1855-
18561847
/*
18571848
* At this point newbuf and buffer are both pinned and locked, and
18581849
* newbuf has enough space for the new tuple. If they are the same
@@ -1929,6 +1920,8 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
19291920
if (have_tuple_lock)
19301921
UnlockTuple(relation, &(oldtup.t_self), ExclusiveLock);
19311922

1923+
pgstat_count_heap_update(&relation->pgstat_info);
1924+
19321925
return HeapTupleMayBeUpdated;
19331926
}
19341927

src/backend/access/index/indexam.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.84 2005/06/27 12:45:22 teodor Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/index/indexam.c,v 1.85 2005/10/06 02:29:11 tgl Exp $
1212
*
1313
* INTERFACE ROUTINES
1414
* index_open - open an index relation by relation OID
@@ -65,9 +65,9 @@
6565

6666
#include "access/genam.h"
6767
#include "access/heapam.h"
68+
#include "pgstat.h"
6869
#include "utils/relcache.h"
6970

70-
#include "pgstat.h"
7171

7272
/* ----------------------------------------------------------------
7373
* macros used in index_ routines
@@ -354,8 +354,6 @@ index_rescan(IndexScanDesc scan, ScanKey key)
354354
FunctionCall2(procedure,
355355
PointerGetDatum(scan),
356356
PointerGetDatum(key));
357-
358-
pgstat_reset_index_scan(&scan->xs_pgstat_info);
359357
}
360358

361359
/* ----------------
@@ -521,8 +519,6 @@ index_getnext(IndexScanDesc scan, ScanDirection direction)
521519
{
522520
bool found;
523521

524-
pgstat_count_index_scan(&scan->xs_pgstat_info);
525-
526522
/*
527523
* The AM's gettuple proc finds the next tuple matching the scan
528524
* keys.
@@ -545,6 +541,8 @@ index_getnext(IndexScanDesc scan, ScanDirection direction)
545541
return NULL; /* failure exit */
546542
}
547543

544+
pgstat_count_index_tuples(&scan->xs_pgstat_info, 1);
545+
548546
/*
549547
* Fetch the heap tuple and see if it matches the snapshot.
550548
*/
@@ -583,8 +581,6 @@ index_getnext(IndexScanDesc scan, ScanDirection direction)
583581
* initialized to 0, which is the correct state ("on row").
584582
*/
585583

586-
pgstat_count_index_getnext(&scan->xs_pgstat_info);
587-
588584
return heapTuple;
589585
}
590586

@@ -621,6 +617,9 @@ index_getnext_indexitem(IndexScanDesc scan,
621617
PointerGetDatum(scan),
622618
Int32GetDatum(direction)));
623619

620+
if (found)
621+
pgstat_count_index_tuples(&scan->xs_pgstat_info, 1);
622+
624623
return found;
625624
}
626625

@@ -660,6 +659,8 @@ index_getmulti(IndexScanDesc scan,
660659
Int32GetDatum(max_tids),
661660
PointerGetDatum(returned_tids)));
662661

662+
pgstat_count_index_tuples(&scan->xs_pgstat_info, *returned_tids);
663+
663664
return found;
664665
}
665666

src/backend/access/nbtree/nbtsearch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.93 2005/06/19 22:41:00 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsearch.c,v 1.94 2005/10/06 02:29:12 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,6 +17,7 @@
1717

1818
#include "access/genam.h"
1919
#include "access/nbtree.h"
20+
#include "pgstat.h"
2021
#include "utils/lsyscache.h"
2122

2223

@@ -501,6 +502,8 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
501502
int i;
502503
StrategyNumber strat_total;
503504

505+
pgstat_count_index_scan(&scan->xs_pgstat_info);
506+
504507
/*
505508
* Examine the scan keys and eliminate any redundant keys; also
506509
* discover how many keys must be matched to continue the scan.

src/backend/access/rtree/rtget.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/rtree/rtget.c,v 1.35 2005/03/27 23:53:02 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/rtree/rtget.c,v 1.36 2005/10/06 02:29:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -18,6 +18,8 @@
1818
#include "access/iqual.h"
1919
#include "access/relscan.h"
2020
#include "access/rtree.h"
21+
#include "pgstat.h"
22+
2123

2224
static OffsetNumber findnext(IndexScanDesc s, OffsetNumber n,
2325
ScanDirection dir);
@@ -118,6 +120,7 @@ rtnext(IndexScanDesc s, ScanDirection dir)
118120
/* first call: start at the root */
119121
Assert(BufferIsValid(so->curbuf) == false);
120122
so->curbuf = ReadBuffer(s->indexRelation, P_ROOT);
123+
pgstat_count_index_scan(&s->xs_pgstat_info);
121124
}
122125

123126
p = BufferGetPage(so->curbuf);

src/backend/catalog/system_views.sql

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 1996-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/backend/catalog/system_views.sql,v 1.21 2005/08/15 23:00:13 momjian Exp $
6+
* $PostgreSQL: pgsql/src/backend/catalog/system_views.sql,v 1.22 2005/10/06 02:29:15 tgl Exp $
77
*/
88

99
CREATE VIEW pg_roles AS
@@ -182,8 +182,9 @@ CREATE VIEW pg_stat_all_tables AS
182182
C.relname AS relname,
183183
pg_stat_get_numscans(C.oid) AS seq_scan,
184184
pg_stat_get_tuples_returned(C.oid) AS seq_tup_read,
185-
sum(pg_stat_get_numscans(I.indexrelid)) AS idx_scan,
186-
sum(pg_stat_get_tuples_fetched(I.indexrelid)) AS idx_tup_fetch,
185+
sum(pg_stat_get_numscans(I.indexrelid))::bigint AS idx_scan,
186+
sum(pg_stat_get_tuples_fetched(I.indexrelid))::bigint +
187+
pg_stat_get_tuples_fetched(C.oid) AS idx_tup_fetch,
187188
pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins,
188189
pg_stat_get_tuples_updated(C.oid) AS n_tup_upd,
189190
pg_stat_get_tuples_deleted(C.oid) AS n_tup_del
@@ -210,8 +211,8 @@ CREATE VIEW pg_statio_all_tables AS
210211
pg_stat_get_blocks_hit(C.oid) AS heap_blks_read,
211212
pg_stat_get_blocks_hit(C.oid) AS heap_blks_hit,
212213
sum(pg_stat_get_blocks_fetched(I.indexrelid) -
213-
pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_read,
214-
sum(pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_hit,
214+
pg_stat_get_blocks_hit(I.indexrelid))::bigint AS idx_blks_read,
215+
sum(pg_stat_get_blocks_hit(I.indexrelid))::bigint AS idx_blks_hit,
215216
pg_stat_get_blocks_fetched(T.oid) -
216217
pg_stat_get_blocks_hit(T.oid) AS toast_blks_read,
217218
pg_stat_get_blocks_hit(T.oid) AS toast_blks_hit,
@@ -350,5 +351,5 @@ UPDATE pg_proc SET
350351
'bool'],
351352
proargmodes = ARRAY['i'::"char", 'o', 'o', 'o', 'o', 'o', 'o'],
352353
proargnames = ARRAY['filename'::text, 'size', 'access', 'modification',
353-
'change', 'creation', 'isdir']
354+
'change', 'creation', 'isdir']
354355
WHERE oid = 'pg_stat_file(text)'::regprocedure;

src/backend/executor/nodeBitmapHeapscan.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*
2323
* IDENTIFICATION
24-
* $PostgreSQL: pgsql/src/backend/executor/nodeBitmapHeapscan.c,v 1.2 2005/05/06 17:24:54 tgl Exp $
24+
* $PostgreSQL: pgsql/src/backend/executor/nodeBitmapHeapscan.c,v 1.3 2005/10/06 02:29:16 tgl Exp $
2525
*
2626
*-------------------------------------------------------------------------
2727
*/
@@ -39,6 +39,7 @@
3939
#include "executor/execdebug.h"
4040
#include "executor/nodeBitmapHeapscan.h"
4141
#include "parser/parsetree.h"
42+
#include "pgstat.h"
4243
#include "utils/memutils.h"
4344

4445

@@ -328,6 +329,9 @@ ExecBitmapHeapReScan(BitmapHeapScanState *node, ExprContext *exprCtxt)
328329
/* rescan to release any page pin */
329330
heap_rescan(node->ss.ss_currentScanDesc, NULL);
330331

332+
/* undo bogus "seq scan" count (see notes in ExecInitBitmapHeapScan) */
333+
pgstat_discount_heap_scan(&node->ss.ss_currentScanDesc->rs_pgstat_info);
334+
331335
if (node->tbm)
332336
tbm_free(node->tbm);
333337
node->tbm = NULL;
@@ -475,6 +479,13 @@ ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate)
475479
0,
476480
NULL);
477481

482+
/*
483+
* One problem is that heap_beginscan counts a "sequential scan" start,
484+
* when we actually aren't doing any such thing. Reverse out the added
485+
* scan count. (Eventually we may want to count bitmap scans separately.)
486+
*/
487+
pgstat_discount_heap_scan(&scanstate->ss.ss_currentScanDesc->rs_pgstat_info);
488+
478489
/*
479490
* get the scan type from the relation descriptor.
480491
*/

0 commit comments

Comments
 (0)