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

Commit 35e9b1c

Browse files
committed
Clean up a couple of ad-hoc computations of the maximum number of tuples
on a page, as suggested by ITAGAKI Takahiro. Also, change a few places that were using some other estimates of max-items-per-page to consistently use MaxOffsetNumber. This is conservatively large --- we could have used the new MaxHeapTuplesPerPage macro, or a similar one for index tuples --- but those places are simply declaring a fixed-size buffer and assuming it will work, rather than actively testing for overrun. It seems safer to size these buffers in a way that can't overflow even if the page is corrupt.
1 parent 962a4bb commit 35e9b1c

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

src/backend/access/gist/gistvacuum.c

Lines changed: 3 additions & 3 deletions
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/gistvacuum.c,v 1.6 2005/06/30 17:52:14 teodor Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.7 2005/09/02 19:02:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -52,7 +52,7 @@ gistVacuumUpdate( GistVacuum *gv, BlockNumber blkno, bool needunion ) {
5252
int lenaddon=4, curlenaddon=0, ntodelete=0;
5353
IndexTuple idxtuple, *addon=NULL;
5454
bool needwrite=false;
55-
OffsetNumber todelete[ BLCKSZ/SizeOfIptrData ];
55+
OffsetNumber todelete[MaxOffsetNumber];
5656
ItemPointerData *completed=NULL;
5757
int ncompleted=0, lencompleted=16;
5858

@@ -439,7 +439,7 @@ gistbulkdelete(PG_FUNCTION_ARGS) {
439439
page = (Page) BufferGetPage(buffer);
440440

441441
if ( GistPageIsLeaf(page) ) {
442-
OffsetNumber todelete[BLCKSZ/SizeOfIptrData];
442+
OffsetNumber todelete[MaxOffsetNumber];
443443
int ntodelete = 0;
444444

445445
LockBuffer(buffer, GIST_UNLOCK);

src/backend/access/nbtree/nbtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.130 2005/05/11 06:24:53 neilc Exp $
15+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.131 2005/09/02 19:02:19 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -584,7 +584,7 @@ btbulkdelete(PG_FUNCTION_ARGS)
584584
IndexBulkDeleteResult *result;
585585
double tuples_removed;
586586
double num_index_tuples;
587-
OffsetNumber deletable[BLCKSZ / sizeof(OffsetNumber)];
587+
OffsetNumber deletable[MaxOffsetNumber];
588588
int ndeletable;
589589
Buffer buf;
590590
BlockNumber num_pages;

src/backend/commands/vacuum.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.313 2005/08/20 00:39:54 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.314 2005/09/02 19:02:19 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -2407,7 +2407,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
24072407
{
24082408
Buffer buf;
24092409
Page page;
2410-
OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)];
2410+
OffsetNumber unused[MaxOffsetNumber];
24112411
OffsetNumber offnum,
24122412
maxoff;
24132413
int uncnt;
@@ -2896,7 +2896,7 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages)
28962896
static void
28972897
vacuum_page(Relation onerel, Buffer buffer, VacPage vacpage)
28982898
{
2899-
OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)];
2899+
OffsetNumber unused[MaxOffsetNumber];
29002900
int uncnt;
29012901
Page page = BufferGetPage(buffer);
29022902
ItemId itemid;

src/backend/commands/vacuumlazy.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
*
3333
* IDENTIFICATION
34-
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.57 2005/08/20 23:26:13 tgl Exp $
34+
* $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.58 2005/09/02 19:02:20 tgl Exp $
3535
*
3636
*-------------------------------------------------------------------------
3737
*/
@@ -60,9 +60,6 @@
6060
#define REL_TRUNCATE_MINIMUM 1000
6161
#define REL_TRUNCATE_FRACTION 16
6262

63-
/* MAX_TUPLES_PER_PAGE can be a conservative upper limit */
64-
#define MAX_TUPLES_PER_PAGE ((int) (BLCKSZ / sizeof(HeapTupleHeaderData)))
65-
6663

6764
typedef struct LVRelStats
6865
{
@@ -259,7 +256,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
259256
* dead-tuple TIDs, pause and do a cycle of vacuuming before we
260257
* tackle this page.
261258
*/
262-
if ((vacrelstats->max_dead_tuples - vacrelstats->num_dead_tuples) < MAX_TUPLES_PER_PAGE &&
259+
if ((vacrelstats->max_dead_tuples - vacrelstats->num_dead_tuples) < MaxHeapTuplesPerPage &&
263260
vacrelstats->num_dead_tuples > 0)
264261
{
265262
/* Remove index entries */
@@ -554,7 +551,7 @@ static int
554551
lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
555552
int tupindex, LVRelStats *vacrelstats)
556553
{
557-
OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)];
554+
OffsetNumber unused[MaxOffsetNumber];
558555
int uncnt;
559556
Page page = BufferGetPage(buffer);
560557
ItemId itemid;
@@ -960,7 +957,7 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
960957
maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
961958
maxtuples = Min(maxtuples, INT_MAX);
962959
/* stay sane if small maintenance_work_mem */
963-
maxtuples = Max(maxtuples, MAX_TUPLES_PER_PAGE);
960+
maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
964961

965962
vacrelstats->num_dead_tuples = 0;
966963
vacrelstats->max_dead_tuples = (int) maxtuples;

src/backend/nodes/tidbitmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
2424
*
2525
* IDENTIFICATION
26-
* $PostgreSQL: pgsql/src/backend/nodes/tidbitmap.c,v 1.6 2005/08/28 22:47:20 tgl Exp $
26+
* $PostgreSQL: pgsql/src/backend/nodes/tidbitmap.c,v 1.7 2005/09/02 19:02:20 tgl Exp $
2727
*
2828
*-------------------------------------------------------------------------
2929
*/
@@ -42,7 +42,7 @@
4242
* the per-page bitmaps variable size. We just legislate that the size
4343
* is this:
4444
*/
45-
#define MAX_TUPLES_PER_PAGE ((BLCKSZ - 1) / MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) + sizeof(ItemIdData)) + 1)
45+
#define MAX_TUPLES_PER_PAGE MaxHeapTuplesPerPage
4646

4747
/*
4848
* When we have to switch over to lossy storage, we use a data structure

src/include/access/htup.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/access/htup.h,v 1.76 2005/08/20 00:39:59 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/access/htup.h,v 1.77 2005/09/02 19:02:20 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -323,6 +323,16 @@ do { \
323323
#define MaxTupleSize \
324324
(BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace))
325325

326+
/*
327+
* MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
328+
* fit on one heap page. (Note that indexes could have more, because they
329+
* use a smaller tuple header.) We arrive at the divisor because each tuple
330+
* must be maxaligned, and it must have an associated item pointer.
331+
*/
332+
#define MaxHeapTuplesPerPage \
333+
((int) ((BLCKSZ - offsetof(PageHeaderData, pd_linp)) / \
334+
(MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) + sizeof(ItemIdData))))
335+
326336
/*
327337
* MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
328338
* data fields of char(n) and similar types. It need not have anything

0 commit comments

Comments
 (0)