From 35e9b1cc1ee296959d52383455052cb3743af478 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 2 Sep 2005 19:02:20 +0000 Subject: [PATCH] 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. --- src/backend/access/gist/gistvacuum.c | 6 +++--- src/backend/access/nbtree/nbtree.c | 4 ++-- src/backend/commands/vacuum.c | 6 +++--- src/backend/commands/vacuumlazy.c | 11 ++++------- src/backend/nodes/tidbitmap.c | 4 ++-- src/include/access/htup.h | 12 +++++++++++- 6 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/backend/access/gist/gistvacuum.c b/src/backend/access/gist/gistvacuum.c index 381cf98559..bcd5ec559c 100644 --- a/src/backend/access/gist/gistvacuum.c +++ b/src/backend/access/gist/gistvacuum.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.6 2005/06/30 17:52:14 teodor Exp $ + * $PostgreSQL: pgsql/src/backend/access/gist/gistvacuum.c,v 1.7 2005/09/02 19:02:19 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -52,7 +52,7 @@ gistVacuumUpdate( GistVacuum *gv, BlockNumber blkno, bool needunion ) { int lenaddon=4, curlenaddon=0, ntodelete=0; IndexTuple idxtuple, *addon=NULL; bool needwrite=false; - OffsetNumber todelete[ BLCKSZ/SizeOfIptrData ]; + OffsetNumber todelete[MaxOffsetNumber]; ItemPointerData *completed=NULL; int ncompleted=0, lencompleted=16; @@ -439,7 +439,7 @@ gistbulkdelete(PG_FUNCTION_ARGS) { page = (Page) BufferGetPage(buffer); if ( GistPageIsLeaf(page) ) { - OffsetNumber todelete[BLCKSZ/SizeOfIptrData]; + OffsetNumber todelete[MaxOffsetNumber]; int ntodelete = 0; LockBuffer(buffer, GIST_UNLOCK); diff --git a/src/backend/access/nbtree/nbtree.c b/src/backend/access/nbtree/nbtree.c index 84ce90c5ca..d4232c847f 100644 --- a/src/backend/access/nbtree/nbtree.c +++ b/src/backend/access/nbtree/nbtree.c @@ -12,7 +12,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.130 2005/05/11 06:24:53 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.131 2005/09/02 19:02:19 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -584,7 +584,7 @@ btbulkdelete(PG_FUNCTION_ARGS) IndexBulkDeleteResult *result; double tuples_removed; double num_index_tuples; - OffsetNumber deletable[BLCKSZ / sizeof(OffsetNumber)]; + OffsetNumber deletable[MaxOffsetNumber]; int ndeletable; Buffer buf; BlockNumber num_pages; diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index ef199c5f07..1e3a420c36 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -13,7 +13,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.313 2005/08/20 00:39:54 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/vacuum.c,v 1.314 2005/09/02 19:02:19 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -2407,7 +2407,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel, { Buffer buf; Page page; - OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)]; + OffsetNumber unused[MaxOffsetNumber]; OffsetNumber offnum, maxoff; int uncnt; @@ -2896,7 +2896,7 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages) static void vacuum_page(Relation onerel, Buffer buffer, VacPage vacpage) { - OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)]; + OffsetNumber unused[MaxOffsetNumber]; int uncnt; Page page = BufferGetPage(buffer); ItemId itemid; diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index bfdf4b6cbb..f324b72673 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -31,7 +31,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.57 2005/08/20 23:26:13 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/vacuumlazy.c,v 1.58 2005/09/02 19:02:20 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -60,9 +60,6 @@ #define REL_TRUNCATE_MINIMUM 1000 #define REL_TRUNCATE_FRACTION 16 -/* MAX_TUPLES_PER_PAGE can be a conservative upper limit */ -#define MAX_TUPLES_PER_PAGE ((int) (BLCKSZ / sizeof(HeapTupleHeaderData))) - typedef struct LVRelStats { @@ -259,7 +256,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats, * dead-tuple TIDs, pause and do a cycle of vacuuming before we * tackle this page. */ - if ((vacrelstats->max_dead_tuples - vacrelstats->num_dead_tuples) < MAX_TUPLES_PER_PAGE && + if ((vacrelstats->max_dead_tuples - vacrelstats->num_dead_tuples) < MaxHeapTuplesPerPage && vacrelstats->num_dead_tuples > 0) { /* Remove index entries */ @@ -554,7 +551,7 @@ static int lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, int tupindex, LVRelStats *vacrelstats) { - OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)]; + OffsetNumber unused[MaxOffsetNumber]; int uncnt; Page page = BufferGetPage(buffer); ItemId itemid; @@ -960,7 +957,7 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks) maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData); maxtuples = Min(maxtuples, INT_MAX); /* stay sane if small maintenance_work_mem */ - maxtuples = Max(maxtuples, MAX_TUPLES_PER_PAGE); + maxtuples = Max(maxtuples, MaxHeapTuplesPerPage); vacrelstats->num_dead_tuples = 0; vacrelstats->max_dead_tuples = (int) maxtuples; diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c index e36a8d9806..a3b5c7d6d0 100644 --- a/src/backend/nodes/tidbitmap.c +++ b/src/backend/nodes/tidbitmap.c @@ -23,7 +23,7 @@ * Copyright (c) 2003-2005, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/nodes/tidbitmap.c,v 1.6 2005/08/28 22:47:20 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/nodes/tidbitmap.c,v 1.7 2005/09/02 19:02:20 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -42,7 +42,7 @@ * the per-page bitmaps variable size. We just legislate that the size * is this: */ -#define MAX_TUPLES_PER_PAGE ((BLCKSZ - 1) / MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) + sizeof(ItemIdData)) + 1) +#define MAX_TUPLES_PER_PAGE MaxHeapTuplesPerPage /* * When we have to switch over to lossy storage, we use a data structure diff --git a/src/include/access/htup.h b/src/include/access/htup.h index abc4dce9b9..6a78cd3a01 100644 --- a/src/include/access/htup.h +++ b/src/include/access/htup.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/access/htup.h,v 1.76 2005/08/20 00:39:59 tgl Exp $ + * $PostgreSQL: pgsql/src/include/access/htup.h,v 1.77 2005/09/02 19:02:20 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -323,6 +323,16 @@ do { \ #define MaxTupleSize \ (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace)) +/* + * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can + * fit on one heap page. (Note that indexes could have more, because they + * use a smaller tuple header.) We arrive at the divisor because each tuple + * must be maxaligned, and it must have an associated item pointer. + */ +#define MaxHeapTuplesPerPage \ + ((int) ((BLCKSZ - offsetof(PageHeaderData, pd_linp)) / \ + (MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) + sizeof(ItemIdData)))) + /* * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of * data fields of char(n) and similar types. It need not have anything