Clean up a couple of ad-hoc computations of the maximum number of tuples
authorTom Lane
Fri, 2 Sep 2005 19:02:20 +0000 (19:02 +0000)
committerTom Lane
Fri, 2 Sep 2005 19:02:20 +0000 (19:02 +0000)
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
src/backend/access/nbtree/nbtree.c
src/backend/commands/vacuum.c
src/backend/commands/vacuumlazy.c
src/backend/nodes/tidbitmap.c
src/include/access/htup.h

index 381cf98559005c0a814c87f4e2e67b05aa789dad..bcd5ec559c54b2d7384ee2288f31e5e3b537bfec 100644 (file)
@@ -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);
index 84ce90c5ca25da2d71f88a5a8d69dc392971c451..d4232c847f8c6d1b9d4c9f6cf6d4b5afe54f2215 100644 (file)
@@ -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;
index ef199c5f0734c56b3376d147b6315e008321c638..1e3a420c36475d0cb5c6f5c6dbc7928a86ee9c11 100644 (file)
@@ -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;
index bfdf4b6cbb3d679125e6c6bcad7dd4cec3e01f65..f324b726734e05b4a178c0994737b4295ffa6c57 100644 (file)
@@ -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;
index e36a8d9806aca5131b15d55d9afec8834647053c..a3b5c7d6d07a0d23bf0aaa8726bb59b0cf8acd94 100644 (file)
@@ -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
index abc4dce9b95dfa8756284c0a4d8cd25846d3c0f6..6a78cd3a018b8a7ac0a53371ab5f29db87931bec 100644 (file)
@@ -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