Introduce IndexAM fields for parallel vacuum.
authorAmit Kapila <akapila@postgresql.org>
Wed, 15 Jan 2020 01:54:14 +0000 (07:24 +0530)
committerAmit Kapila <akapila@postgresql.org>
Wed, 15 Jan 2020 01:54:14 +0000 (07:24 +0530)
Introduce new fields amusemaintenanceworkmem and amparallelvacuumoptions
in IndexAmRoutine for parallel vacuum.  The amusemaintenanceworkmem tells
whether a particular IndexAM uses maintenance_work_mem or not.  This will
help in controlling the memory used by individual workers as otherwise,
each worker can consume memory equal to maintenance_work_mem.  The
amparallelvacuumoptions tell whether a particular IndexAM participates in
a parallel vacuum and if so in which phase (bulkdelete, vacuumcleanup) of
vacuum.

Author: Masahiko Sawada and Amit Kapila
Reviewed-by: Dilip Kumar, Amit Kapila, Tomas Vondra and Robert Haas
Discussion:
https://postgr.es/m/CAD21AoDTPMgzSkV4E3SFo1CH_x50bf5PqZFQf4jmqjk-C03BWg@mail.gmail.com
https://postgr.es/m/CAA4eK1LmcD5aPogzwim5Nn58Ki+74a6Edghx4Wd8hAskvHaq5A@mail.gmail.com

contrib/bloom/blutils.c
doc/src/sgml/indexam.sgml
src/backend/access/brin/brin.c
src/backend/access/gin/ginutil.c
src/backend/access/gist/gist.c
src/backend/access/hash/hash.c
src/backend/access/nbtree/nbtree.c
src/backend/access/spgist/spgutils.c
src/include/access/amapi.h
src/include/commands/vacuum.h
src/test/modules/dummy_index_am/dummy_index_am.c

index 23d959b9f06342efbcc23e3ae88f99409f9f1780..0104d02f675ffd777077a225763321bbba484e31 100644 (file)
@@ -18,6 +18,7 @@
 #include "access/reloptions.h"
 #include "bloom.h"
 #include "catalog/index.h"
+#include "commands/vacuum.h"
 #include "miscadmin.h"
 #include "storage/bufmgr.h"
 #include "storage/freespace.h"
@@ -121,6 +122,9 @@ blhandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = false;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = false;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = blbuild;
index dd54c6880241a5501631dd2506b1554892ce80c3..37f8d8760a380fac2277178e9acf53e76215a23e 100644 (file)
@@ -122,6 +122,10 @@ typedef struct IndexAmRoutine
     bool        amcanparallel;
     /* does AM support columns included with clause INCLUDE? */
     bool        amcaninclude;
+    /* does AM use maintenance_work_mem? */
+    bool        amusemaintenanceworkmem;
+    /* OR of parallel vacuum flags */
+    uint8       amparallelvacuumoptions;
     /* type of data stored in index, or InvalidOid if variable */
     Oid         amkeytype;
 
index d89af7844da42ab49b44de444bd05f5dba672b83..2e8f67ef10175f79235e6292eb518b3d67b48c07 100644 (file)
@@ -27,6 +27,7 @@
 #include "access/xloginsert.h"
 #include "catalog/index.h"
 #include "catalog/pg_am.h"
+#include "commands/vacuum.h"
 #include "miscadmin.h"
 #include "pgstat.h"
 #include "postmaster/autovacuum.h"
@@ -101,6 +102,9 @@ brinhandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = false;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = false;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_CLEANUP;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = brinbuild;
index 910f0bcb915eb2337e8f75f969816b55a642368c..a7e55caf28d640a6912af5d8e634dab5612ea931 100644 (file)
@@ -20,6 +20,7 @@
 #include "access/xloginsert.h"
 #include "catalog/pg_collation.h"
 #include "catalog/pg_type.h"
+#include "commands/vacuum.h"
 #include "miscadmin.h"
 #include "storage/indexfsm.h"
 #include "storage/lmgr.h"
@@ -53,6 +54,9 @@ ginhandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = true;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = false;
+   amroutine->amusemaintenanceworkmem = true;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = ginbuild;
index 5c9ad341b33bc9311b1a12832b4375568d6681dc..aefc302ed297fb1fbf53f56fc89d9b7202ed409f 100644 (file)
@@ -17,6 +17,7 @@
 #include "access/gist_private.h"
 #include "access/gistscan.h"
 #include "catalog/pg_collation.h"
+#include "commands/vacuum.h"
 #include "miscadmin.h"
 #include "nodes/execnodes.h"
 #include "storage/lmgr.h"
@@ -74,6 +75,9 @@ gisthandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = true;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = true;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_COND_CLEANUP;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = gistbuild;
index 4bb6efc98fad90756b098153ace4a319bea05162..4871b7ff4d688e6286f174fa11f2b10c8a1fed2c 100644 (file)
@@ -72,6 +72,9 @@ hashhandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = true;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = false;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_BULKDEL;
    amroutine->amkeytype = INT4OID;
 
    amroutine->ambuild = hashbuild;
index 8376a5e6b75946418d702ce6dec3a6fcf16af60a..5254bc7ef5cc19a057d731722d12c463b181f638 100644 (file)
@@ -121,6 +121,9 @@ bthandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = true;
    amroutine->amcanparallel = true;
    amroutine->amcaninclude = true;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_COND_CLEANUP;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = btbuild;
index d715908764a49ea152f1d7ba849a3c6c98efa129..4924ae1c59389502d4357d38f0bfa4e157238512 100644 (file)
@@ -22,6 +22,7 @@
 #include "access/transam.h"
 #include "access/xact.h"
 #include "catalog/pg_amop.h"
+#include "commands/vacuum.h"
 #include "storage/bufmgr.h"
 #include "storage/indexfsm.h"
 #include "storage/lmgr.h"
@@ -56,6 +57,9 @@ spghandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = false;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = false;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions =
+       VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_COND_CLEANUP;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = spgbuild;
index d2a49e8d3e8aa8d049e5508491fdef88d904c918..3b3e22f73de8f733219cfd74ca31f63e811f7dd2 100644 (file)
@@ -197,6 +197,10 @@ typedef struct IndexAmRoutine
    bool        amcanparallel;
    /* does AM support columns included with clause INCLUDE? */
    bool        amcaninclude;
+   /* does AM use maintenance_work_mem? */
+   bool        amusemaintenanceworkmem;
+   /* OR of parallel vacuum flags.  See vacuum.h for flags. */
+   uint8       amparallelvacuumoptions;
    /* type of data stored in index, or InvalidOid if variable */
    Oid         amkeytype;
 
index 5dc41dd0c194f7a6c37fa1193ecac2eec823f420..b3351ad40627c7590f9d8df74cdb8cbd4d19c1cd 100644 (file)
 #include "storage/lock.h"
 #include "utils/relcache.h"
 
+/*
+ * Flags for amparallelvacuumoptions to control the participation of bulkdelete
+ * and vacuumcleanup in parallel vacuum.
+ */
+
+/*
+ * Both bulkdelete and vacuumcleanup are disabled by default.  This will be
+ * used by IndexAM's that don't want to or cannot participate in parallel
+ * vacuum.  For example, if an index AM doesn't have a way to communicate the
+ * index statistics allocated by the first ambulkdelete call to the subsequent
+ * ones until amvacuumcleanup, the index AM cannot participate in parallel
+ * vacuum.
+ */
+#define VACUUM_OPTION_NO_PARALLEL          0
+
+/*
+ * bulkdelete can be performed in parallel.  This option can be used by
+ * IndexAm's that need to scan the index to delete the tuples.
+ */
+#define VACUUM_OPTION_PARALLEL_BULKDEL     (1 << 0)
+
+/*
+ * vacuumcleanup can be performed in parallel if bulkdelete is not performed
+ * yet.  This will be used by IndexAM's that can scan the index if the
+ * bulkdelete is not performed.
+ */
+#define VACUUM_OPTION_PARALLEL_COND_CLEANUP    (1 << 1)
+
+/*
+ * vacuumcleanup can be performed in parallel even if bulkdelete has already
+ * processed the index.  This will be used by IndexAM's that scan the index
+ * during the cleanup phase of index irrespective of whether the index is
+ * already scanned or not during bulkdelete phase.
+ */
+#define VACUUM_OPTION_PARALLEL_CLEANUP     (1 << 2)
+
+/* value for checking vacuum flags */
+#define VACUUM_OPTION_MAX_VALID_VALUE      ((1 << 3) - 1)
 
 /*----------
  * ANALYZE builds one of these structs for each attribute (column) that is
index 898ab066391e179fd734b8af599d0ef9f6d9c48f..f32632089b1e15d1ef7d39852a5bd10b29d4a8d5 100644 (file)
@@ -16,6 +16,7 @@
 #include "access/amapi.h"
 #include "access/reloptions.h"
 #include "catalog/index.h"
+#include "commands/vacuum.h"
 #include "nodes/pathnodes.h"
 #include "utils/guc.h"
 #include "utils/rel.h"
@@ -294,6 +295,8 @@ dihandler(PG_FUNCTION_ARGS)
    amroutine->ampredlocks = false;
    amroutine->amcanparallel = false;
    amroutine->amcaninclude = false;
+   amroutine->amusemaintenanceworkmem = false;
+   amroutine->amparallelvacuumoptions = VACUUM_OPTION_NO_PARALLEL;
    amroutine->amkeytype = InvalidOid;
 
    amroutine->ambuild = dibuild;