Commit 35e9b1cc authored by Tom Lane's avatar Tom Lane

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.
parent 962a4bb6
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 ) { ...@@ -52,7 +52,7 @@ gistVacuumUpdate( GistVacuum *gv, BlockNumber blkno, bool needunion ) {
int lenaddon=4, curlenaddon=0, ntodelete=0; int lenaddon=4, curlenaddon=0, ntodelete=0;
IndexTuple idxtuple, *addon=NULL; IndexTuple idxtuple, *addon=NULL;
bool needwrite=false; bool needwrite=false;
OffsetNumber todelete[ BLCKSZ/SizeOfIptrData ]; OffsetNumber todelete[MaxOffsetNumber];
ItemPointerData *completed=NULL; ItemPointerData *completed=NULL;
int ncompleted=0, lencompleted=16; int ncompleted=0, lencompleted=16;
...@@ -439,7 +439,7 @@ gistbulkdelete(PG_FUNCTION_ARGS) { ...@@ -439,7 +439,7 @@ gistbulkdelete(PG_FUNCTION_ARGS) {
page = (Page) BufferGetPage(buffer); page = (Page) BufferGetPage(buffer);
if ( GistPageIsLeaf(page) ) { if ( GistPageIsLeaf(page) ) {
OffsetNumber todelete[BLCKSZ/SizeOfIptrData]; OffsetNumber todelete[MaxOffsetNumber];
int ntodelete = 0; int ntodelete = 0;
LockBuffer(buffer, GIST_UNLOCK); LockBuffer(buffer, GIST_UNLOCK);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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) ...@@ -584,7 +584,7 @@ btbulkdelete(PG_FUNCTION_ARGS)
IndexBulkDeleteResult *result; IndexBulkDeleteResult *result;
double tuples_removed; double tuples_removed;
double num_index_tuples; double num_index_tuples;
OffsetNumber deletable[BLCKSZ / sizeof(OffsetNumber)]; OffsetNumber deletable[MaxOffsetNumber];
int ndeletable; int ndeletable;
Buffer buf; Buffer buf;
BlockNumber num_pages; BlockNumber num_pages;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* *
* IDENTIFICATION * 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, ...@@ -2407,7 +2407,7 @@ repair_frag(VRelStats *vacrelstats, Relation onerel,
{ {
Buffer buf; Buffer buf;
Page page; Page page;
OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)]; OffsetNumber unused[MaxOffsetNumber];
OffsetNumber offnum, OffsetNumber offnum,
maxoff; maxoff;
int uncnt; int uncnt;
...@@ -2896,7 +2896,7 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages) ...@@ -2896,7 +2896,7 @@ vacuum_heap(VRelStats *vacrelstats, Relation onerel, VacPageList vacuum_pages)
static void static void
vacuum_page(Relation onerel, Buffer buffer, VacPage vacpage) vacuum_page(Relation onerel, Buffer buffer, VacPage vacpage)
{ {
OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)]; OffsetNumber unused[MaxOffsetNumber];
int uncnt; int uncnt;
Page page = BufferGetPage(buffer); Page page = BufferGetPage(buffer);
ItemId itemid; ItemId itemid;
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
* *
* *
* IDENTIFICATION * 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 @@ ...@@ -60,9 +60,6 @@
#define REL_TRUNCATE_MINIMUM 1000 #define REL_TRUNCATE_MINIMUM 1000
#define REL_TRUNCATE_FRACTION 16 #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 typedef struct LVRelStats
{ {
...@@ -259,7 +256,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats, ...@@ -259,7 +256,7 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
* dead-tuple TIDs, pause and do a cycle of vacuuming before we * dead-tuple TIDs, pause and do a cycle of vacuuming before we
* tackle this page. * 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) vacrelstats->num_dead_tuples > 0)
{ {
/* Remove index entries */ /* Remove index entries */
...@@ -554,7 +551,7 @@ static int ...@@ -554,7 +551,7 @@ static int
lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer,
int tupindex, LVRelStats *vacrelstats) int tupindex, LVRelStats *vacrelstats)
{ {
OffsetNumber unused[BLCKSZ / sizeof(OffsetNumber)]; OffsetNumber unused[MaxOffsetNumber];
int uncnt; int uncnt;
Page page = BufferGetPage(buffer); Page page = BufferGetPage(buffer);
ItemId itemid; ItemId itemid;
...@@ -960,7 +957,7 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks) ...@@ -960,7 +957,7 @@ lazy_space_alloc(LVRelStats *vacrelstats, BlockNumber relblocks)
maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData); maxtuples = (maintenance_work_mem * 1024L) / sizeof(ItemPointerData);
maxtuples = Min(maxtuples, INT_MAX); maxtuples = Min(maxtuples, INT_MAX);
/* stay sane if small maintenance_work_mem */ /* stay sane if small maintenance_work_mem */
maxtuples = Max(maxtuples, MAX_TUPLES_PER_PAGE); maxtuples = Max(maxtuples, MaxHeapTuplesPerPage);
vacrelstats->num_dead_tuples = 0; vacrelstats->num_dead_tuples = 0;
vacrelstats->max_dead_tuples = (int) maxtuples; vacrelstats->max_dead_tuples = (int) maxtuples;
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
* Copyright (c) 2003-2005, PostgreSQL Global Development Group * Copyright (c) 2003-2005, PostgreSQL Global Development Group
* *
* IDENTIFICATION * 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 @@ ...@@ -42,7 +42,7 @@
* the per-page bitmaps variable size. We just legislate that the size * the per-page bitmaps variable size. We just legislate that the size
* is this: * 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 * When we have to switch over to lossy storage, we use a data structure
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 { \ ...@@ -323,6 +323,16 @@ do { \
#define MaxTupleSize \ #define MaxTupleSize \
(BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace)) (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 * 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 * data fields of char(n) and similar types. It need not have anything
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment