On Sun, Feb 26, 2017 at 11:49 AM, Robert Haas <robertmh...@gmail.com> wrote:
> On Wed, Feb 22, 2017 at 11:13 AM, Jim Nasby <jim.na...@bluetreble.com> > wrote: > > Certainly easier, but I don't think it'd be better. Matviews really > aren't > > the same thing as tables. Off-hand (without reviewing the patch), update > and > > delete counts certainly wouldn't make any sense. "Insert" counts might, > in > > as much as it's how many rows have been added by refreshes. You'd want a > > refresh count too. > > Regular REFRESH truncates the view and repopulates it, but REFRESH > CONCURRENTLY does inserts, updates, and deletes as needed to adjust > the contents. So I think all the same counters that make sense for > regular tables are also sensible here. > > After digging into things further, just making refresh report the stats for what is it basically doing simplifies and solves it and it is something we can back patch if that the consensus. See the attached patch.
diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c index a18c917..4383312 100644 --- a/src/backend/commands/matview.c +++ b/src/backend/commands/matview.c @@ -30,6 +30,7 @@ #include "executor/spi.h" #include "miscadmin.h" #include "parser/parse_relation.h" +#include "pgstat.h" #include "rewrite/rewriteHandler.h" #include "storage/lmgr.h" #include "storage/smgr.h" @@ -59,7 +60,7 @@ static void transientrel_startup(DestReceiver *self, int operation, TupleDesc ty static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self); static void transientrel_shutdown(DestReceiver *self); static void transientrel_destroy(DestReceiver *self); -static void refresh_matview_datafill(DestReceiver *dest, Query *query, +static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query, const char *queryString); static char *make_temptable_name_n(char *tempname, int n); @@ -151,6 +152,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString, Oid save_userid; int save_sec_context; int save_nestlevel; + uint64 processed = 0; ObjectAddress address; /* Determine strength of lock needed. */ @@ -322,7 +324,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString, /* Generate the data, if wanted. */ if (!stmt->skipData) - refresh_matview_datafill(dest, dataQuery, queryString); + processed = refresh_matview_datafill(dest, dataQuery, queryString); heap_close(matviewRel, NoLock); @@ -345,8 +347,17 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString, Assert(matview_maintenance_depth == old_depth); } else + { refresh_by_heap_swap(matviewOid, OIDNewHeap, relpersistence); + /* + * Send the stats to mimic what we are essentially doing. + * A truncate and insert + */ + pgstat_count_truncate(matviewRel); + pgstat_count_heap_insert(matviewRel, processed); + } + /* Roll back any GUC changes */ AtEOXact_GUC(false, save_nestlevel); @@ -361,7 +372,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString, /* * refresh_matview_datafill */ -static void +static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query, const char *queryString) { @@ -369,6 +380,7 @@ refresh_matview_datafill(DestReceiver *dest, Query *query, PlannedStmt *plan; QueryDesc *queryDesc; Query *copied_query; + uint64 processed; /* Lock and rewrite, using a copy to preserve the original query. */ copied_query = copyObject(query); @@ -406,6 +418,8 @@ refresh_matview_datafill(DestReceiver *dest, Query *query, /* run the plan */ ExecutorRun(queryDesc, ForwardScanDirection, 0L); + processed = queryDesc->estate->es_processed; + /* and clean up */ ExecutorFinish(queryDesc); ExecutorEnd(queryDesc); @@ -413,6 +427,8 @@ refresh_matview_datafill(DestReceiver *dest, Query *query, FreeQueryDesc(queryDesc); PopActiveSnapshot(); + + return processed; } DestReceiver *
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers