Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 17f8ffa

Browse files
committed
Fix REFRESH MATERIALIZED VIEW to report activity to the stats collector.
The non-concurrent code path for REFRESH MATERIALIZED VIEW failed to report its updates to the stats collector. This is bad since it means auto-analyze doesn't know there's any work to be done. Adjust it to report the refresh as a table truncate followed by insertion of an appropriate number of rows. Since a matview could contain more than INT_MAX rows, change the signature of pgstat_count_heap_insert() to accept an int64 rowcount. (The accumulator it's adding into is already int64, but existing callers could not insert more than a small number of rows at once, so the argument had been declared just "int n".) This is surely a bug fix, but changing pgstat_count_heap_insert()'s API seems too risky for the back branches. Given the lack of previous complaints, I'm not sure it's a big enough problem to justify a kluge solution that would avoid that. So, no back-patch, at least for now. Jim Mlodgenski, adjusted a bit by me Discussion: https://postgr.es/m/CAB_5SRchSz7-WmdO5szdiknG8Oj_GGqJytrk1KRd11yhcMs1KQ@mail.gmail.com
1 parent 27f1f58 commit 17f8ffa

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/backend/commands/matview.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "executor/spi.h"
3131
#include "miscadmin.h"
3232
#include "parser/parse_relation.h"
33+
#include "pgstat.h"
3334
#include "rewrite/rewriteHandler.h"
3435
#include "storage/lmgr.h"
3536
#include "storage/smgr.h"
@@ -59,7 +60,7 @@ static void transientrel_startup(DestReceiver *self, int operation, TupleDesc ty
5960
static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self);
6061
static void transientrel_shutdown(DestReceiver *self);
6162
static void transientrel_destroy(DestReceiver *self);
62-
static void refresh_matview_datafill(DestReceiver *dest, Query *query,
63+
static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query,
6364
const char *queryString);
6465

6566
static char *make_temptable_name_n(char *tempname, int n);
@@ -145,6 +146,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
145146
Oid relowner;
146147
Oid OIDNewHeap;
147148
DestReceiver *dest;
149+
uint64 processed = 0;
148150
bool concurrent;
149151
LOCKMODE lockmode;
150152
char relpersistence;
@@ -322,7 +324,7 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
322324

323325
/* Generate the data, if wanted. */
324326
if (!stmt->skipData)
325-
refresh_matview_datafill(dest, dataQuery, queryString);
327+
processed = refresh_matview_datafill(dest, dataQuery, queryString);
326328

327329
heap_close(matviewRel, NoLock);
328330

@@ -345,8 +347,20 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
345347
Assert(matview_maintenance_depth == old_depth);
346348
}
347349
else
350+
{
348351
refresh_by_heap_swap(matviewOid, OIDNewHeap, relpersistence);
349352

353+
/*
354+
* Inform stats collector about our activity: basically, we truncated
355+
* the matview and inserted some new data. (The concurrent code path
356+
* above doesn't need to worry about this because the inserts and
357+
* deletes it issues get counted by lower-level code.)
358+
*/
359+
pgstat_count_truncate(matviewRel);
360+
if (!stmt->skipData)
361+
pgstat_count_heap_insert(matviewRel, processed);
362+
}
363+
350364
/* Roll back any GUC changes */
351365
AtEOXact_GUC(false, save_nestlevel);
352366

@@ -360,15 +374,21 @@ ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
360374

361375
/*
362376
* refresh_matview_datafill
377+
*
378+
* Execute the given query, sending result rows to "dest" (which will
379+
* insert them into the target matview).
380+
*
381+
* Returns number of rows inserted.
363382
*/
364-
static void
383+
static uint64
365384
refresh_matview_datafill(DestReceiver *dest, Query *query,
366385
const char *queryString)
367386
{
368387
List *rewritten;
369388
PlannedStmt *plan;
370389
QueryDesc *queryDesc;
371390
Query *copied_query;
391+
uint64 processed;
372392

373393
/* Lock and rewrite, using a copy to preserve the original query. */
374394
copied_query = copyObject(query);
@@ -406,13 +426,17 @@ refresh_matview_datafill(DestReceiver *dest, Query *query,
406426
/* run the plan */
407427
ExecutorRun(queryDesc, ForwardScanDirection, 0L);
408428

429+
processed = queryDesc->estate->es_processed;
430+
409431
/* and clean up */
410432
ExecutorFinish(queryDesc);
411433
ExecutorEnd(queryDesc);
412434

413435
FreeQueryDesc(queryDesc);
414436

415437
PopActiveSnapshot();
438+
439+
return processed;
416440
}
417441

418442
DestReceiver *

src/backend/postmaster/pgstat.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
18031803
* pgstat_count_heap_insert - count a tuple insertion of n tuples
18041804
*/
18051805
void
1806-
pgstat_count_heap_insert(Relation rel, int n)
1806+
pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
18071807
{
18081808
PgStat_TableStatus *pgstat_info = rel->pgstat_info;
18091809

src/include/pgstat.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ pgstat_report_wait_end(void)
12561256
#define pgstat_count_buffer_write_time(n) \
12571257
(pgStatBlockWriteTime += (n))
12581258

1259-
extern void pgstat_count_heap_insert(Relation rel, int n);
1259+
extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n);
12601260
extern void pgstat_count_heap_update(Relation rel, bool hot);
12611261
extern void pgstat_count_heap_delete(Relation rel);
12621262
extern void pgstat_count_truncate(Relation rel);

0 commit comments

Comments
 (0)