]> granicus.if.org Git - postgresql/blobdiff - src/backend/commands/vacuum.c
From: t-ishii@sra.co.jp
[postgresql] / src / backend / commands / vacuum.c
index 52158c75e6caa5b1cd3cc95d3f9e70817b9ac684..79c0c9bbf61a093ee3b56cf1d30295ed99c159a5 100644 (file)
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.59 1998/01/31 04:38:21 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/commands/vacuum.c,v 1.68 1998/07/26 04:30:25 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <postgres.h>
-
-#include <fmgr.h>
-#include <utils/portal.h>
-#include <access/genam.h>
-#include <access/heapam.h>
-#include <access/xact.h>
-#include <storage/bufmgr.h>
-#include <access/transam.h>
-#include <catalog/pg_index.h>
-#include <catalog/index.h>
-#include <catalog/catname.h>
-#include <catalog/catalog.h>
-#include <catalog/pg_class.h>
-#include <catalog/pg_proc.h>
-#include <catalog/pg_statistic.h>
-#include <catalog/pg_type.h>
-#include <catalog/pg_operator.h>
-#include <parser/parse_oper.h>
-#include <storage/smgr.h>
-#include <storage/lmgr.h>
-#include <utils/inval.h>
-#include <utils/mcxt.h>
-#include <utils/inval.h>
-#include <utils/syscache.h>
-#include <utils/builtins.h>
-#include <commands/vacuum.h>
-#include <storage/bufpage.h>
+#include "postgres.h"
+
+#include "access/genam.h"
+#include "access/heapam.h"
+#include "access/transam.h"
+#include "access/xact.h"
+#include "catalog/catalog.h"
+#include "catalog/catname.h"
+#include "catalog/index.h"
+#ifdef MULTIBYTE
+#include "catalog/pg_class_mb.h"
+#else
+#include "catalog/pg_class.h"
+#endif
+#include "catalog/pg_index.h"
+#include "catalog/pg_operator.h"
+#include "catalog/pg_statistic.h"
+#include "catalog/pg_type.h"
+#include "commands/vacuum.h"
+#include "fmgr.h"
+#include "parser/parse_oper.h"
+#include "storage/bufmgr.h"
+#include "storage/bufpage.h"
 #include "storage/shmem.h"
+#include "storage/smgr.h"
+#include "storage/lmgr.h"
+#include "utils/builtins.h"
+#include "utils/inval.h"
+#include "utils/mcxt.h"
+#include "utils/portal.h"
+#include "utils/syscache.h"
+
 #ifndef HAVE_GETRUSAGE
 #include <rusagestub.h>
 #else
@@ -54,9 +57,9 @@
 #include <sys/resource.h>
 #endif
 
-/* #include <port-protos.h> */ /* Why? */
+ /* #include <port-protos.h> *//* Why? */
 
-extern int BlowawayRelationBuffers(Relation rdesc, BlockNumber block);
+extern int     BlowawayRelationBuffers(Relation rdesc, BlockNumber block);
 
 bool           VacuumRunning = false;
 
@@ -135,7 +138,9 @@ vacuum(char *vacrel, bool verbose, bool analyze, List *va_spec)
        pmem = PortalGetVariableMemory(vc_portal);
        old = MemoryContextSwitchTo((MemoryContext) pmem);
 
-       Assert(va_spec == NIL || analyze);
+       if (va_spec != NIL && !analyze)
+               elog(ERROR, "Can't vacuum columns, only tables.  You can 'vacuum analyze' columns.");
+
        foreach(le, va_spec)
        {
                char       *col = (char *) lfirst(le);
@@ -277,13 +282,13 @@ vc_getrels(NameData *VacRelP)
        if (VacRelP->data)
        {
                ScanKeyEntryInitialize(&pgckey, 0x0, Anum_pg_class_relname,
-                                                          NameEqualRegProcedure,
+                                                          F_NAMEEQ,
                                                           PointerGetDatum(VacRelP->data));
        }
        else
        {
                ScanKeyEntryInitialize(&pgckey, 0x0, Anum_pg_class_relkind,
-                                                 CharacterEqualRegProcedure, CharGetDatum('r'));
+                                                 F_CHAREQ, CharGetDatum('r'));
        }
 
        portalmem = PortalGetVariableMemory(vc_portal);
@@ -332,9 +337,7 @@ vc_getrels(NameData *VacRelP)
                /* get a relation list entry for this guy */
                old = MemoryContextSwitchTo((MemoryContext) portalmem);
                if (vrl == (VRelList) NULL)
-               {
                        vrl = cur = (VRelList) palloc(sizeof(VRelListData));
-               }
                else
                {
                        cur->vrl_next = (VRelList) palloc(sizeof(VRelListData));
@@ -396,7 +399,7 @@ vc_vacone(Oid relid, bool analyze, List *va_cols)
        StartTransactionCommand();
 
        ScanKeyEntryInitialize(&pgckey, 0x0, ObjectIdAttributeNumber,
-                                                  ObjectIdEqualRegProcedure,
+                                                  F_OIDEQ,
                                                   ObjectIdGetDatum(relid));
 
        pgclass = heap_openr(RelationRelationName);
@@ -653,6 +656,8 @@ vc_scanheap(VRelStats *vacrelstats, Relation onerel,
        vpc = (VPageDescr) palloc(sizeof(VPageDescrData) + MaxOffsetNumber * sizeof(OffsetNumber));
        vpc->vpd_nusd = 0;
 
+       elog(MESSAGE_LEVEL, "--Relation %s--", relname);
+       
        for (blkno = 0; blkno < nblocks; blkno++)
        {
                buf = ReadBuffer(onerel, blkno);
@@ -723,8 +728,9 @@ vc_scanheap(VRelStats *vacrelstats, Relation onerel,
                                        }
                                        else if (!TransactionIdIsInProgress(htup->t_xmin))
                                        {
+
                                                /*
-                                                * Not Aborted, Not Committed, Not in Progress - 
+                                                * Not Aborted, Not Committed, Not in Progress -
                                                 * so it's from crashed process. - vadim 11/26/96
                                                 */
                                                ncrash++;
@@ -739,11 +745,11 @@ vc_scanheap(VRelStats *vacrelstats, Relation onerel,
                                }
                        }
 
-                       /* 
-                        * here we are concerned about tuples with xmin committed 
-                        * and xmax unknown or committed
+                       /*
+                        * here we are concerned about tuples with xmin committed and
+                        * xmax unknown or committed
                         */
-                       if (htup->t_infomask & HEAP_XMIN_COMMITTED && 
+                       if (htup->t_infomask & HEAP_XMIN_COMMITTED &&
                                !(htup->t_infomask & HEAP_XMAX_INVALID))
                        {
                                if (htup->t_infomask & HEAP_XMAX_COMMITTED)
@@ -757,6 +763,7 @@ vc_scanheap(VRelStats *vacrelstats, Relation onerel,
                                        tupgone = true;
                                else if (!TransactionIdIsInProgress(htup->t_xmax))
                                {
+
                                        /*
                                         * Not Aborted, Not Committed, Not in Progress - so it
                                         * from crashed process. - vadim 06/02/97
@@ -905,16 +912,15 @@ vc_scanheap(VRelStats *vacrelstats, Relation onerel,
 
        getrusage(RUSAGE_SELF, &ru1);
 
-       elog(MESSAGE_LEVEL, "Rel %s: Pages %u: Changed %u, Reapped %u, Empty %u, New %u; \
+       elog(MESSAGE_LEVEL, "Pages %u: Changed %u, Reapped %u, Empty %u, New %u; \
 Tup %u: Vac %u, Crash %u, UnUsed %u, MinLen %u, MaxLen %u; Re-using: Free/Avail. Space %u/%u; EndEmpty/Avail. Pages %u/%u. Elapsed %u/%u sec.",
-                relname,
                 nblocks, nchpg, Vvpl->vpl_npages, nempg, nnepg,
                 ntups, nvac, ncrash, nunused, min_tlen, max_tlen,
                 frsize, frsusf, nemend, Fvpl->vpl_npages,
                 ru1.ru_stime.tv_sec - ru0.ru_stime.tv_sec,
                 ru1.ru_utime.tv_sec - ru0.ru_utime.tv_sec);
 
-}                                                              /* vc_scanheap */
+}      /* vc_scanheap */
 
 
 /*
@@ -1026,9 +1032,7 @@ vc_rpfheap(VRelStats *vacrelstats, Relation onerel,
                                dowrite = true;
                        }
                        else
-                       {
                                Assert(isempty);
-                       }
                        --Vnpages;
                        Assert(Vnpages > 0);
                        /* get prev reapped page from Vvpl */
@@ -1051,9 +1055,7 @@ vc_rpfheap(VRelStats *vacrelstats, Relation onerel,
                        }
                }
                else
-               {
                        Assert(!isempty);
-               }
 
                vpc->vpd_blkno = blkno;
                maxoff = PageGetMaxOffsetNumber(page);
@@ -1081,23 +1083,19 @@ vc_rpfheap(VRelStats *vacrelstats, Relation onerel,
                                        /*
                                         * If no one tuple can't be added to this page -
                                         * remove page from Fvpl. - vadim 11/27/96
+                                        *
+                                        * But we can't remove last page - this is our
+                                        * "show-stopper" !!!   - vadim 02/25/98
                                         */
-                                       if (!vc_enough_space(ToVpd, vacrelstats->min_tlen))
+                                       if (ToVpd != Fvplast &&
+                                               !vc_enough_space(ToVpd, vacrelstats->min_tlen))
                                        {
-                                               if (ToVpd != Fvplast)
-                                               {
-                                                       Assert(Fnpages > ToVpI + 1);
-                                                       memmove(Fvpl->vpl_pgdesc + ToVpI,
-                                                                       Fvpl->vpl_pgdesc + ToVpI + 1,
-                                                       sizeof(VPageDescr *) * (Fnpages - ToVpI - 1));
-                                               }
-                                               Assert(Fnpages >= 1);
+                                               Assert(Fnpages > ToVpI + 1);
+                                               memmove(Fvpl->vpl_pgdesc + ToVpI,
+                                                               Fvpl->vpl_pgdesc + ToVpI + 1,
+                                                  sizeof(VPageDescr *) * (Fnpages - ToVpI - 1));
                                                Fnpages--;
-                                               if (Fnpages == 0)
-                                                       break;
-                                               /* get prev reapped page from Fvpl */
-                                               Fvplast = Fvpl->vpl_pgdesc[Fnpages - 1];
-                                               Fblklast = Fvplast->vpd_blkno;
+                                               Assert(Fvplast == Fvpl->vpl_pgdesc[Fnpages - 1]);
                                        }
                                }
                                for (i = 0; i < Fnpages; i++)
@@ -1335,7 +1333,7 @@ Elapsed %u/%u sec.",
        {
                i = BlowawayRelationBuffers(onerel, blkno);
                if (i < 0)
-                       elog (FATAL, "VACUUM (vc_rpfheap): BlowawayRelationBuffers returned %d", i);
+                       elog(FATAL, "VACUUM (vc_rpfheap): BlowawayRelationBuffers returned %d", i);
                blkno = smgrtruncate(DEFAULT_SMGR, onerel, blkno);
                Assert(blkno >= 0);
                vacrelstats->npages = blkno;    /* set new number of blocks */
@@ -1351,7 +1349,7 @@ Elapsed %u/%u sec.",
 
        pfree(vpc);
 
-}                                                              /* vc_rpfheap */
+}      /* vc_rpfheap */
 
 /*
  *     vc_vacheap() -- free dead tuples
@@ -1369,7 +1367,7 @@ vc_vacheap(VRelStats *vacrelstats, Relation onerel, VPageList Vvpl)
        int                     i;
 
        nblocks = Vvpl->vpl_npages;
-       nblocks -= Vvpl->vpl_nemend;    /* nothing to do with them */
+       nblocks -= Vvpl->vpl_nemend;/* nothing to do with them */
 
        for (i = 0, vpp = Vvpl->vpl_pgdesc; i < nblocks; i++, vpp++)
        {
@@ -1396,17 +1394,17 @@ vc_vacheap(VRelStats *vacrelstats, Relation onerel, VPageList Vvpl)
                 * it) before truncation
                 */
                FlushBufferPool(!TransactionFlushEnabled());
-               
+
                i = BlowawayRelationBuffers(onerel, nblocks);
                if (i < 0)
-                       elog (FATAL, "VACUUM (vc_vacheap): BlowawayRelationBuffers returned %d", i);
+                       elog(FATAL, "VACUUM (vc_vacheap): BlowawayRelationBuffers returned %d", i);
 
                nblocks = smgrtruncate(DEFAULT_SMGR, onerel, nblocks);
                Assert(nblocks >= 0);
                vacrelstats->npages = nblocks;  /* set new number of blocks */
        }
 
-}                                                              /* vc_vacheap */
+}      /* vc_vacheap */
 
 /*
  *     vc_vacpage() -- free dead tuples on a page
@@ -1426,7 +1424,7 @@ vc_vacpage(Page page, VPageDescr vpd)
        }
        PageRepairFragmentation(page);
 
-}                                                              /* vc_vacpage */
+}      /* vc_vacpage */
 
 /*
  *     _vc_scanoneind() -- scan one index relation to update statistic.
@@ -1472,7 +1470,7 @@ vc_scanoneind(Relation indrel, int nhtups)
                elog(NOTICE, "Ind %s: NUMBER OF INDEX' TUPLES (%u) IS NOT THE SAME AS HEAP' (%u)",
                         indrel->rd_rel->relname.data, nitups, nhtups);
 
-}                                                              /* vc_scanoneind */
+}      /* vc_scanoneind */
 
 /*
  *     vc_vaconeind() -- vacuum one index relation.
@@ -1530,9 +1528,7 @@ vc_vaconeind(VPageList vpl, Relation indrel, int nhtups)
                        index_delete(indrel, &res->index_iptr);
                }
                else
-               {
                        nitups++;
-               }
 
                /* be tidy */
                pfree(res);
@@ -1555,7 +1551,7 @@ vc_vaconeind(VPageList vpl, Relation indrel, int nhtups)
                elog(NOTICE, "Ind %s: NUMBER OF INDEX' TUPLES (%u) IS NOT THE SAME AS HEAP' (%u)",
                         indrel->rd_rel->relname.data, nitups, nhtups);
 
-}                                                              /* vc_vaconeind */
+}      /* vc_vaconeind */
 
 /*
  *     vc_tidreapped() -- is a particular tid reapped?
@@ -1599,7 +1595,7 @@ vc_tidreapped(ItemPointer itemptr, VPageList vpl)
 
        return (vp);
 
-}                                                              /* vc_tidreapped */
+}      /* vc_tidreapped */
 
 /*
  *     vc_attrstats() -- compute column statistics used by the optimzer
@@ -1776,7 +1772,7 @@ vc_updstats(Oid relid, int npages, int ntups, bool hasindex, VRelStats *vacrelst
         * update number of tuples and number of pages in pg_class
         */
        ScanKeyEntryInitialize(&rskey, 0x0, ObjectIdAttributeNumber,
-                                                  ObjectIdEqualRegProcedure,
+                                                  F_OIDEQ,
                                                   ObjectIdGetDatum(relid));
 
        rd = heap_openr(RelationRelationName);
@@ -1890,10 +1886,10 @@ vc_updstats(Oid relid, int npages, int ntups, bool hasindex, VRelStats *vacrelst
                                        values[i++] = (Datum) InvalidOid;       /* 3 */
                                        fmgr_info(stats->outfunc, &out_function);
                                        out_string = (*fmgr_faddr(&out_function)) (stats->min, stats->attr->atttypid);
-                                       values[i++] = (Datum) fmgr(TextInRegProcedure, out_string);
+                                       values[i++] = (Datum) fmgr(F_TEXTIN, out_string);
                                        pfree(out_string);
                                        out_string = (char *) (*fmgr_faddr(&out_function)) (stats->max, stats->attr->atttypid);
-                                       values[i++] = (Datum) fmgr(TextInRegProcedure, out_string);
+                                       values[i++] = (Datum) fmgr(F_TEXTIN, out_string);
                                        pfree(out_string);
 
                                        sdesc = sd->rd_att;
@@ -1948,7 +1944,7 @@ vc_delhilowstats(Oid relid, int attcnt, int *attnums)
        if (relid != InvalidOid)
        {
                ScanKeyEntryInitialize(&pgskey, 0x0, Anum_pg_statistic_starelid,
-                                                          ObjectIdEqualRegProcedure,
+                                                          F_OIDEQ,
                                                           ObjectIdGetDatum(relid));
                pgsscan = heap_beginscan(pgstatistic, false, false, 1, &pgskey);
        }
@@ -2013,7 +2009,7 @@ vc_reappage(VPageList vpl, VPageDescr vpc)
        /* insert this page into vpl list */
        vc_vpinsert(vpl, newvpd);
 
-}                                                              /* vc_reappage */
+}      /* vc_reappage */
 
 static void
 vc_vpinsert(VPageList vpl, VPageDescr vpnew)
@@ -2103,7 +2099,7 @@ vc_find_eq(char *bot, int nelem, int size, char *elm, int (*compar) (char *, cha
                first_move = true;
        }
 
-}                                                              /* vc_find_eq */
+}      /* vc_find_eq */
 
 static int
 vc_cmp_blk(char *left, char *right)
@@ -2120,7 +2116,7 @@ vc_cmp_blk(char *left, char *right)
                return (0);
        return (1);
 
-}                                                              /* vc_cmp_blk */
+}      /* vc_cmp_blk */
 
 static int
 vc_cmp_offno(char *left, char *right)
@@ -2132,7 +2128,7 @@ vc_cmp_offno(char *left, char *right)
                return (0);
        return (1);
 
-}                                                              /* vc_cmp_offno */
+}      /* vc_cmp_offno */
 
 
 static void
@@ -2159,7 +2155,7 @@ vc_getindices(Oid relid, int *nindices, Relation **Irel)
        pgidesc = RelationGetTupleDescriptor(pgindex);
 
        ScanKeyEntryInitialize(&pgikey, 0x0, Anum_pg_index_indrelid,
-                                                  ObjectIdEqualRegProcedure,
+                                                  F_OIDEQ,
                                                   ObjectIdGetDatum(relid));
 
        pgiscan = heap_beginscan(pgindex, false, false, 1, &pgikey);
@@ -2209,7 +2205,7 @@ vc_getindices(Oid relid, int *nindices, Relation **Irel)
                *Irel = (Relation *) NULL;
        }
 
-}                                                              /* vc_getindices */
+}      /* vc_getindices */
 
 
 static void
@@ -2220,12 +2216,10 @@ vc_clsindices(int nindices, Relation *Irel)
                return;
 
        while (nindices--)
-       {
                index_close(Irel[nindices]);
-       }
        pfree(Irel);
 
-}                                                              /* vc_clsindices */
+}      /* vc_clsindices */
 
 
 static void
@@ -2264,7 +2258,7 @@ vc_mkindesc(Relation onerel, int nindices, Relation *Irel, IndDesc **Idesc)
                idcur->natts = natts;
        }
 
-}                                                              /* vc_mkindesc */
+}      /* vc_mkindesc */
 
 
 static bool
@@ -2285,4 +2279,4 @@ vc_enough_space(VPageDescr vpd, Size len)
 
        return (false);
 
-}                                                              /* vc_enough_space */
+}      /* vc_enough_space */