Coverage Report

Created: 2026-10-16 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/backend/commands/matview.c
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 * matview.c
4
 *    materialized view support
5
 *
6
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7
 * Portions Copyright (c) 1994, Regents of the University of California
8
 *
9
 *
10
 * IDENTIFICATION
11
 *    src/backend/commands/matview.c
12
 *
13
 *-------------------------------------------------------------------------
14
 */
15
#include "postgres.h"
16
17
#include "access/genam.h"
18
#include "access/heapam.h"
19
#include "access/htup_details.h"
20
#include "access/multixact.h"
21
#include "access/tableam.h"
22
#include "access/xact.h"
23
#include "catalog/indexing.h"
24
#include "catalog/namespace.h"
25
#include "catalog/pg_am.h"
26
#include "catalog/pg_opclass.h"
27
#include "commands/matview.h"
28
#include "commands/repack.h"
29
#include "commands/tablecmds.h"
30
#include "commands/tablespace.h"
31
#include "executor/executor.h"
32
#include "executor/spi.h"
33
#include "miscadmin.h"
34
#include "pgstat.h"
35
#include "rewrite/rewriteHandler.h"
36
#include "storage/lmgr.h"
37
#include "tcop/tcopprot.h"
38
#include "utils/builtins.h"
39
#include "utils/lsyscache.h"
40
#include "utils/rel.h"
41
#include "utils/snapmgr.h"
42
#include "utils/syscache.h"
43
44
45
typedef struct
46
{
47
  DestReceiver pub;     /* publicly-known function pointers */
48
  Oid     transientoid; /* OID of new heap into which to store */
49
  /* These fields are filled by transientrel_startup: */
50
  Relation  transientrel; /* relation to write to */
51
  CommandId output_cid;   /* cmin to insert in output tuples */
52
  uint32    ti_options;   /* table_tuple_insert performance options */
53
  BulkInsertState bistate;  /* bulk insert state */
54
} DR_transientrel;
55
56
static int  matview_maintenance_depth = 0;
57
58
static void transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
59
static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self);
60
static void transientrel_shutdown(DestReceiver *self);
61
static void transientrel_destroy(DestReceiver *self);
62
static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query,
63
                     const char *queryString, bool is_create);
64
static void refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
65
                   int save_sec_context);
66
static void refresh_by_heap_swap(Oid matviewOid, Oid OIDNewHeap, char relpersistence);
67
static bool is_usable_unique_index(Relation indexRel);
68
static void OpenMatViewIncrementalMaintenance(void);
69
static void CloseMatViewIncrementalMaintenance(void);
70
71
/*
72
 * SetMatViewPopulatedState
73
 *    Mark a materialized view as populated, or not.
74
 *
75
 * NOTE: caller must be holding an appropriate lock on the relation.
76
 */
77
void
78
SetMatViewPopulatedState(Relation relation, bool newstate)
79
0
{
80
0
  Relation  pgrel;
81
0
  HeapTuple tuple;
82
83
0
  Assert(relation->rd_rel->relkind == RELKIND_MATVIEW);
84
85
  /*
86
   * Update relation's pg_class entry.  Crucial side-effect: other backends
87
   * (and this one too!) are sent SI message to make them rebuild relcache
88
   * entries.
89
   */
90
0
  pgrel = table_open(RelationRelationId, RowExclusiveLock);
91
0
  tuple = SearchSysCacheCopy1(RELOID,
92
0
                ObjectIdGetDatum(RelationGetRelid(relation)));
93
0
  if (!HeapTupleIsValid(tuple))
94
0
    elog(ERROR, "cache lookup failed for relation %u",
95
0
       RelationGetRelid(relation));
96
97
0
  ((Form_pg_class) GETSTRUCT(tuple))->relispopulated = newstate;
98
99
0
  CatalogTupleUpdate(pgrel, &tuple->t_self, tuple);
100
101
0
  heap_freetuple(tuple);
102
0
  table_close(pgrel, RowExclusiveLock);
103
104
  /*
105
   * Advance command counter to make the updated pg_class row locally
106
   * visible.
107
   */
108
0
  CommandCounterIncrement();
109
0
}
110
111
/*
112
 * ExecRefreshMatView -- execute a REFRESH MATERIALIZED VIEW command
113
 *
114
 * If WITH NO DATA was specified, this is effectively like a TRUNCATE;
115
 * otherwise it is like a TRUNCATE followed by an INSERT using the SELECT
116
 * statement associated with the materialized view.  The statement node's
117
 * skipData field shows whether the clause was used.
118
 */
119
ObjectAddress
120
ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString,
121
           QueryCompletion *qc)
122
0
{
123
0
  Oid     matviewOid;
124
0
  LOCKMODE  lockmode;
125
126
  /* Determine strength of lock needed. */
127
0
  lockmode = stmt->concurrent ? ExclusiveLock : AccessExclusiveLock;
128
129
  /*
130
   * Get a lock until end of transaction.
131
   */
132
0
  matviewOid = RangeVarGetRelidExtended(stmt->relation,
133
0
                      lockmode, 0,
134
0
                      RangeVarCallbackMaintainsTable,
135
0
                      NULL);
136
137
0
  return RefreshMatViewByOid(matviewOid, false, stmt->skipData,
138
0
                 stmt->concurrent, queryString, qc);
139
0
}
140
141
/*
142
 * RefreshMatViewByOid -- refresh materialized view by OID
143
 *
144
 * This refreshes the materialized view by creating a new table and swapping
145
 * the relfilenumbers of the new table and the old materialized view, so the OID
146
 * of the original materialized view is preserved. Thus we do not lose GRANT
147
 * nor references to this materialized view.
148
 *
149
 * If skipData is true, this is effectively like a TRUNCATE; otherwise it is
150
 * like a TRUNCATE followed by an INSERT using the SELECT statement associated
151
 * with the materialized view.
152
 *
153
 * Indexes are rebuilt too, via REINDEX. Since we are effectively bulk-loading
154
 * the new heap, it's better to create the indexes afterwards than to fill them
155
 * incrementally while we load.
156
 *
157
 * The matview's "populated" state is changed based on whether the contents
158
 * reflect the result set of the materialized view's query.
159
 *
160
 * This is also used to populate the materialized view created by CREATE
161
 * MATERIALIZED VIEW command.
162
 */
163
ObjectAddress
164
RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData,
165
          bool concurrent, const char *queryString,
166
          QueryCompletion *qc)
167
0
{
168
0
  Relation  matviewRel;
169
0
  RewriteRule *rule;
170
0
  List     *actions;
171
0
  Query    *dataQuery;
172
0
  Oid     tableSpace;
173
0
  Oid     relowner;
174
0
  Oid     OIDNewHeap;
175
0
  uint64    processed = 0;
176
0
  char    relpersistence;
177
0
  Oid     save_userid;
178
0
  int     save_sec_context;
179
0
  int     save_nestlevel;
180
0
  ObjectAddress address;
181
182
0
  matviewRel = table_open(matviewOid, NoLock);
183
0
  relowner = matviewRel->rd_rel->relowner;
184
185
  /*
186
   * Switch to the owner's userid, so that any functions are run as that
187
   * user.  Also lock down security-restricted operations and arrange to
188
   * make GUC variable changes local to this command.
189
   */
190
0
  GetUserIdAndSecContext(&save_userid, &save_sec_context);
191
0
  SetUserIdAndSecContext(relowner,
192
0
               save_sec_context | SECURITY_RESTRICTED_OPERATION);
193
0
  save_nestlevel = NewGUCNestLevel();
194
0
  RestrictSearchPath();
195
196
  /* Make sure it is a materialized view. */
197
0
  if (matviewRel->rd_rel->relkind != RELKIND_MATVIEW)
198
0
    ereport(ERROR,
199
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
200
0
         errmsg("\"%s\" is not a materialized view",
201
0
            RelationGetRelationName(matviewRel))));
202
203
  /* Check that CONCURRENTLY is not specified if not populated. */
204
0
  if (concurrent && !RelationIsPopulated(matviewRel))
205
0
    ereport(ERROR,
206
0
        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
207
0
         errmsg("CONCURRENTLY cannot be used when the materialized view is not populated")));
208
209
  /* Check that conflicting options have not been specified. */
210
0
  if (concurrent && skipData)
211
0
    ereport(ERROR,
212
0
        (errcode(ERRCODE_SYNTAX_ERROR),
213
0
         errmsg("%s options %s and %s cannot be used together",
214
0
            "REFRESH", "CONCURRENTLY", "WITH NO DATA")));
215
216
  /*
217
   * Check that everything is correct for a refresh. Problems at this point
218
   * are internal errors, so elog is sufficient.
219
   */
220
0
  if (matviewRel->rd_rel->relhasrules == false ||
221
0
    matviewRel->rd_rules->numLocks < 1)
222
0
    elog(ERROR,
223
0
       "materialized view \"%s\" is missing rewrite information",
224
0
       RelationGetRelationName(matviewRel));
225
226
0
  if (matviewRel->rd_rules->numLocks > 1)
227
0
    elog(ERROR,
228
0
       "materialized view \"%s\" has too many rules",
229
0
       RelationGetRelationName(matviewRel));
230
231
0
  rule = matviewRel->rd_rules->rules[0];
232
0
  if (rule->event != CMD_SELECT || !(rule->isInstead))
233
0
    elog(ERROR,
234
0
       "the rule for materialized view \"%s\" is not a SELECT INSTEAD OF rule",
235
0
       RelationGetRelationName(matviewRel));
236
237
0
  actions = rule->actions;
238
0
  if (list_length(actions) != 1)
239
0
    elog(ERROR,
240
0
       "the rule for materialized view \"%s\" is not a single action",
241
0
       RelationGetRelationName(matviewRel));
242
243
  /*
244
   * Check that there is a unique index with no WHERE clause on one or more
245
   * columns of the materialized view if CONCURRENTLY is specified.
246
   */
247
0
  if (concurrent)
248
0
  {
249
0
    List     *indexoidlist = RelationGetIndexList(matviewRel);
250
0
    ListCell   *indexoidscan;
251
0
    bool    hasUniqueIndex = false;
252
253
0
    Assert(!is_create);
254
255
0
    foreach(indexoidscan, indexoidlist)
256
0
    {
257
0
      Oid     indexoid = lfirst_oid(indexoidscan);
258
0
      Relation  indexRel;
259
260
0
      indexRel = index_open(indexoid, AccessShareLock);
261
0
      hasUniqueIndex = is_usable_unique_index(indexRel);
262
0
      index_close(indexRel, AccessShareLock);
263
0
      if (hasUniqueIndex)
264
0
        break;
265
0
    }
266
267
0
    list_free(indexoidlist);
268
269
0
    if (!hasUniqueIndex)
270
0
      ereport(ERROR,
271
0
          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
272
0
           errmsg("cannot refresh materialized view \"%s\" concurrently",
273
0
              quote_qualified_identifier(get_namespace_name(RelationGetNamespace(matviewRel)),
274
0
                             RelationGetRelationName(matviewRel))),
275
0
           errhint("Create a unique index with no WHERE clause on one or more columns of the materialized view.")));
276
0
  }
277
278
  /*
279
   * The stored query was rewritten at the time of the MV definition, but
280
   * has not been scribbled on by the planner.
281
   */
282
0
  dataQuery = linitial_node(Query, actions);
283
284
  /*
285
   * Check for active uses of the relation in the current transaction, such
286
   * as open scans.
287
   *
288
   * NB: We count on this to protect us against problems with refreshing the
289
   * data using TABLE_INSERT_FROZEN.
290
   */
291
0
  CheckTableNotInUse(matviewRel,
292
0
             is_create ? "CREATE MATERIALIZED VIEW" :
293
0
             "REFRESH MATERIALIZED VIEW");
294
295
  /*
296
   * Tentatively mark the matview as populated or not, if its state is
297
   * changing (this will roll back if we fail later).
298
   */
299
0
  if (RelationIsPopulated(matviewRel) != !skipData)
300
0
    SetMatViewPopulatedState(matviewRel, !skipData);
301
302
  /* Concurrent refresh builds new data in temp tablespace, and does diff. */
303
0
  if (concurrent)
304
0
  {
305
0
    tableSpace = GetDefaultTablespace(RELPERSISTENCE_TEMP, false);
306
0
    relpersistence = RELPERSISTENCE_TEMP;
307
0
  }
308
0
  else
309
0
  {
310
0
    tableSpace = matviewRel->rd_rel->reltablespace;
311
0
    relpersistence = matviewRel->rd_rel->relpersistence;
312
0
  }
313
314
  /*
315
   * Create the transient table that will receive the regenerated data. Lock
316
   * it against access by any other process until commit (by which time it
317
   * will be gone).
318
   */
319
0
  OIDNewHeap = make_new_heap(matviewOid, tableSpace,
320
0
                 matviewRel->rd_rel->relam,
321
0
                 relpersistence, ExclusiveLock);
322
0
  Assert(CheckRelationOidLockedByMe(OIDNewHeap, AccessExclusiveLock, false));
323
324
  /* Generate the data, if wanted. */
325
0
  if (!skipData)
326
0
  {
327
0
    DestReceiver *dest;
328
329
0
    dest = CreateTransientRelDestReceiver(OIDNewHeap);
330
0
    processed = refresh_matview_datafill(dest, dataQuery, queryString,
331
0
                       is_create);
332
0
  }
333
334
  /* Make the matview match the newly generated data. */
335
0
  if (concurrent)
336
0
  {
337
0
    int     old_depth = matview_maintenance_depth;
338
339
0
    PG_TRY();
340
0
    {
341
0
      refresh_by_match_merge(matviewOid, OIDNewHeap, relowner,
342
0
                   save_sec_context);
343
0
    }
344
0
    PG_CATCH();
345
0
    {
346
0
      matview_maintenance_depth = old_depth;
347
0
      PG_RE_THROW();
348
0
    }
349
0
    PG_END_TRY();
350
0
    Assert(matview_maintenance_depth == old_depth);
351
0
  }
352
0
  else
353
0
  {
354
0
    refresh_by_heap_swap(matviewOid, OIDNewHeap, relpersistence);
355
356
    /*
357
     * Inform cumulative stats system about our activity: basically, we
358
     * truncated the matview and inserted some new data.  (The concurrent
359
     * code path above doesn't need to worry about this because the
360
     * inserts and deletes it issues get counted by lower-level code.)
361
     */
362
0
    pgstat_count_truncate(matviewRel);
363
0
    if (!skipData)
364
0
      pgstat_count_heap_insert(matviewRel, processed);
365
0
  }
366
367
0
  table_close(matviewRel, NoLock);
368
369
  /* Roll back any GUC changes */
370
0
  AtEOXact_GUC(false, save_nestlevel);
371
372
  /* Restore userid and security context */
373
0
  SetUserIdAndSecContext(save_userid, save_sec_context);
374
375
0
  ObjectAddressSet(address, RelationRelationId, matviewOid);
376
377
  /*
378
   * Save the rowcount so that pg_stat_statements can track the total number
379
   * of rows processed by REFRESH MATERIALIZED VIEW command. Note that we
380
   * still don't display the rowcount in the command completion tag output,
381
   * i.e., the display_rowcount flag of CMDTAG_REFRESH_MATERIALIZED_VIEW
382
   * command tag is left false in cmdtaglist.h. Otherwise, the change of
383
   * completion tag output might break applications using it.
384
   *
385
   * When called from CREATE MATERIALIZED VIEW command, the rowcount is
386
   * displayed with the command tag CMDTAG_SELECT.
387
   */
388
0
  if (qc)
389
0
    SetQueryCompletion(qc,
390
0
               is_create ? CMDTAG_SELECT : CMDTAG_REFRESH_MATERIALIZED_VIEW,
391
0
               processed);
392
393
0
  return address;
394
0
}
395
396
/*
397
 * refresh_matview_datafill
398
 *
399
 * Execute the given query, sending result rows to "dest" (which will
400
 * insert them into the target matview).
401
 *
402
 * Returns number of rows inserted.
403
 */
404
static uint64
405
refresh_matview_datafill(DestReceiver *dest, Query *query,
406
             const char *queryString, bool is_create)
407
0
{
408
0
  List     *rewritten;
409
0
  PlannedStmt *plan;
410
0
  QueryDesc  *queryDesc;
411
0
  Query    *copied_query;
412
0
  uint64    processed;
413
414
  /* Lock and rewrite, using a copy to preserve the original query. */
415
0
  copied_query = copyObject(query);
416
0
  AcquireRewriteLocks(copied_query, true, false);
417
0
  rewritten = QueryRewrite(copied_query);
418
419
  /* SELECT should never rewrite to more or less than one SELECT query */
420
0
  if (list_length(rewritten) != 1)
421
0
    elog(ERROR, "unexpected rewrite result for %s",
422
0
       is_create ? "CREATE MATERIALIZED VIEW " : "REFRESH MATERIALIZED VIEW");
423
0
  query = (Query *) linitial(rewritten);
424
425
  /* Check for user-requested abort. */
426
0
  CHECK_FOR_INTERRUPTS();
427
428
  /* Plan the query which will generate data for the refresh. */
429
0
  plan = pg_plan_query(query, queryString, CURSOR_OPT_PARALLEL_OK, NULL, NULL);
430
431
  /*
432
   * Use a snapshot with an updated command ID to ensure this query sees
433
   * results of any previously executed queries.  (This could only matter if
434
   * the planner executed an allegedly-stable function that changed the
435
   * database contents, but let's do it anyway to be safe.)
436
   */
437
0
  PushCopiedSnapshot(GetActiveSnapshot());
438
0
  UpdateActiveSnapshotCommandId();
439
440
  /* Create a QueryDesc, redirecting output to our tuple receiver */
441
0
  queryDesc = CreateQueryDesc(plan, queryString,
442
0
                GetActiveSnapshot(), InvalidSnapshot,
443
0
                dest, NULL, NULL, 0);
444
445
  /* call ExecutorStart to prepare the plan for execution */
446
0
  ExecutorStart(queryDesc, 0);
447
448
  /* run the plan */
449
0
  ExecutorRun(queryDesc, ForwardScanDirection, 0);
450
451
0
  processed = queryDesc->estate->es_processed;
452
453
  /* and clean up */
454
0
  ExecutorFinish(queryDesc);
455
0
  ExecutorEnd(queryDesc);
456
457
0
  FreeQueryDesc(queryDesc);
458
459
0
  PopActiveSnapshot();
460
461
0
  return processed;
462
0
}
463
464
DestReceiver *
465
CreateTransientRelDestReceiver(Oid transientoid)
466
0
{
467
0
  DR_transientrel *self = palloc0_object(DR_transientrel);
468
469
0
  self->pub.receiveSlot = transientrel_receive;
470
0
  self->pub.rStartup = transientrel_startup;
471
0
  self->pub.rShutdown = transientrel_shutdown;
472
0
  self->pub.rDestroy = transientrel_destroy;
473
0
  self->pub.mydest = DestTransientRel;
474
0
  self->transientoid = transientoid;
475
476
0
  return (DestReceiver *) self;
477
0
}
478
479
/*
480
 * transientrel_startup --- executor startup
481
 */
482
static void
483
transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
484
0
{
485
0
  DR_transientrel *myState = (DR_transientrel *) self;
486
0
  Relation  transientrel;
487
488
0
  transientrel = table_open(myState->transientoid, NoLock);
489
490
  /*
491
   * Fill private fields of myState for use by later routines
492
   */
493
0
  myState->transientrel = transientrel;
494
0
  myState->output_cid = GetCurrentCommandId(true);
495
0
  myState->ti_options = TABLE_INSERT_SKIP_FSM | TABLE_INSERT_FROZEN;
496
0
  myState->bistate = GetBulkInsertState();
497
498
  /*
499
   * Valid smgr_targblock implies something already wrote to the relation.
500
   * This may be harmless, but this function hasn't planned for it.
501
   */
502
0
  Assert(RelationGetTargetBlock(transientrel) == InvalidBlockNumber);
503
0
}
504
505
/*
506
 * transientrel_receive --- receive one tuple
507
 */
508
static bool
509
transientrel_receive(TupleTableSlot *slot, DestReceiver *self)
510
0
{
511
0
  DR_transientrel *myState = (DR_transientrel *) self;
512
513
  /*
514
   * Note that the input slot might not be of the type of the target
515
   * relation. That's supported by table_tuple_insert(), but slightly less
516
   * efficient than inserting with the right slot - but the alternative
517
   * would be to copy into a slot of the right type, which would not be
518
   * cheap either. This also doesn't allow accessing per-AM data (say a
519
   * tuple's xmin), but since we don't do that here...
520
   */
521
522
0
  table_tuple_insert(myState->transientrel,
523
0
             slot,
524
0
             myState->output_cid,
525
0
             myState->ti_options,
526
0
             myState->bistate);
527
528
  /* We know this is a newly created relation, so there are no indexes */
529
530
0
  return true;
531
0
}
532
533
/*
534
 * transientrel_shutdown --- executor end
535
 */
536
static void
537
transientrel_shutdown(DestReceiver *self)
538
0
{
539
0
  DR_transientrel *myState = (DR_transientrel *) self;
540
541
0
  FreeBulkInsertState(myState->bistate);
542
543
0
  table_finish_bulk_insert(myState->transientrel, myState->ti_options);
544
545
  /* close transientrel, but keep lock until commit */
546
0
  table_close(myState->transientrel, NoLock);
547
0
  myState->transientrel = NULL;
548
0
}
549
550
/*
551
 * transientrel_destroy --- release DestReceiver object
552
 */
553
static void
554
transientrel_destroy(DestReceiver *self)
555
0
{
556
0
  pfree(self);
557
0
}
558
559
/*
560
 * refresh_by_match_merge
561
 *
562
 * Refresh a materialized view with transactional semantics, while allowing
563
 * concurrent reads.
564
 *
565
 * This is called after a new version of the data has been created in a
566
 * temporary table.  It performs a full outer join against the old version of
567
 * the data, producing "diff" results.  This join cannot work if there are any
568
 * duplicated rows in either the old or new versions, in the sense that every
569
 * column would compare as equal between the two rows.  It does work correctly
570
 * in the face of rows which have at least one NULL value, with all non-NULL
571
 * columns equal.  The behavior of NULLs on equality tests and on UNIQUE
572
 * indexes turns out to be quite convenient here; the tests we need to make
573
 * are consistent with default behavior.  If there is at least one UNIQUE
574
 * index on the materialized view, we have exactly the guarantee we need.
575
 *
576
 * The temporary table used to hold the diff results contains just the TID of
577
 * the old record (if matched) and the ROW from the new table as a single
578
 * column of complex record type (if matched).
579
 *
580
 * Once we have the diff table, we perform set-based DELETE and INSERT
581
 * operations against the materialized view, and discard both temporary
582
 * tables.
583
 *
584
 * Everything from the generation of the new data to applying the differences
585
 * takes place under cover of an ExclusiveLock, since it seems as though we
586
 * would want to prohibit not only concurrent REFRESH operations, but also
587
 * incremental maintenance.  It also doesn't seem reasonable or safe to allow
588
 * SELECT FOR UPDATE or SELECT FOR SHARE on rows being updated or deleted by
589
 * this command.
590
 */
591
static void
592
refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
593
             int save_sec_context)
594
0
{
595
0
  StringInfoData querybuf;
596
0
  Relation  matviewRel;
597
0
  Relation  tempRel;
598
0
  char     *matviewname;
599
0
  char     *tempname;
600
0
  char     *diffname;
601
0
  char     *temprelname;
602
0
  char     *diffrelname;
603
0
  char     *nsp;
604
0
  TupleDesc tupdesc;
605
0
  bool    foundUniqueIndex;
606
0
  List     *indexoidlist;
607
0
  ListCell   *indexoidscan;
608
0
  int16   relnatts;
609
0
  Oid      *opUsedForQual;
610
611
0
  initStringInfo(&querybuf);
612
0
  matviewRel = table_open(matviewOid, NoLock);
613
0
  matviewname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(matviewRel)),
614
0
                       RelationGetRelationName(matviewRel));
615
0
  tempRel = table_open(tempOid, NoLock);
616
617
  /*
618
   * Build qualified names of the temporary table and the diff table.  The
619
   * only difference between them is the "_2" suffix on the diff table name.
620
   */
621
0
  nsp = get_namespace_name(RelationGetNamespace(tempRel));
622
0
  temprelname = RelationGetRelationName(tempRel);
623
0
  diffrelname = psprintf("%s_2", temprelname);
624
625
0
  tempname = quote_qualified_identifier(nsp, temprelname);
626
0
  diffname = quote_qualified_identifier(nsp, diffrelname);
627
628
0
  relnatts = RelationGetNumberOfAttributes(matviewRel);
629
630
  /* Open SPI context. */
631
0
  SPI_connect();
632
633
  /* Analyze the temp table with the new contents. */
634
0
  appendStringInfo(&querybuf, "ANALYZE %s", tempname);
635
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
636
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
637
638
  /*
639
   * We need to ensure that there are not duplicate rows without NULLs in
640
   * the new data set before we can count on the "diff" results.  Check for
641
   * that in a way that allows showing the first duplicated row found.  Even
642
   * after we pass this test, a unique index on the materialized view may
643
   * find a duplicate key problem.
644
   *
645
   * Note: here and below, we use "tablename.*::tablerowtype" as a hack to
646
   * keep ".*" from being expanded into multiple columns in a SELECT list.
647
   * Compare ruleutils.c's get_variable().
648
   */
649
0
  resetStringInfo(&querybuf);
650
0
  appendStringInfo(&querybuf,
651
0
           "SELECT newdata.*::%s FROM %s newdata "
652
0
           "WHERE newdata.* IS NOT NULL AND EXISTS "
653
0
           "(SELECT 1 FROM %s newdata2 WHERE newdata2.* IS NOT NULL "
654
0
           "AND newdata2.* OPERATOR(pg_catalog.*=) newdata.* "
655
0
           "AND newdata2.ctid OPERATOR(pg_catalog.<>) "
656
0
           "newdata.ctid)",
657
0
           tempname, tempname, tempname);
658
0
  if (SPI_execute(querybuf.data, false, 1) != SPI_OK_SELECT)
659
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
660
0
  if (SPI_processed > 0)
661
0
  {
662
    /*
663
     * Note that this ereport() is returning data to the user.  Generally,
664
     * we would want to make sure that the user has been granted access to
665
     * this data.  However, REFRESH MAT VIEW is only able to be run by the
666
     * owner of the mat view (or a superuser) and therefore there is no
667
     * need to check for access to data in the mat view.
668
     */
669
0
    ereport(ERROR,
670
0
        (errcode(ERRCODE_CARDINALITY_VIOLATION),
671
0
         errmsg("new data for materialized view \"%s\" contains duplicate rows without any null columns",
672
0
            RelationGetRelationName(matviewRel)),
673
0
         errdetail("Row: %s",
674
0
               SPI_getvalue(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1))));
675
0
  }
676
677
  /*
678
   * Create the temporary "diff" table.
679
   *
680
   * Temporarily switch out of the SECURITY_RESTRICTED_OPERATION context,
681
   * because you cannot create temp tables in SRO context.  For extra
682
   * paranoia, add the composite type column only after switching back to
683
   * SRO context.
684
   */
685
0
  SetUserIdAndSecContext(relowner,
686
0
               save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
687
0
  resetStringInfo(&querybuf);
688
0
  appendStringInfo(&querybuf,
689
0
           "CREATE TEMP TABLE %s (tid pg_catalog.tid)",
690
0
           diffname);
691
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
692
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
693
0
  SetUserIdAndSecContext(relowner,
694
0
               save_sec_context | SECURITY_RESTRICTED_OPERATION);
695
0
  resetStringInfo(&querybuf);
696
0
  appendStringInfo(&querybuf,
697
0
           "ALTER TABLE %s ADD COLUMN newdata %s",
698
0
           diffname, tempname);
699
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
700
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
701
702
  /* Start building the query for populating the diff table. */
703
0
  resetStringInfo(&querybuf);
704
0
  appendStringInfo(&querybuf,
705
0
           "INSERT INTO %s "
706
0
           "SELECT mv.ctid AS tid, newdata.*::%s AS newdata "
707
0
           "FROM %s mv FULL JOIN %s newdata ON (",
708
0
           diffname, tempname, matviewname, tempname);
709
710
  /*
711
   * Get the list of index OIDs for the table from the relcache, and look up
712
   * each one in the pg_index syscache.  We will test for equality on all
713
   * columns present in all unique indexes which only reference columns and
714
   * include all rows.
715
   */
716
0
  tupdesc = matviewRel->rd_att;
717
0
  opUsedForQual = palloc0_array(Oid, relnatts);
718
0
  foundUniqueIndex = false;
719
720
0
  indexoidlist = RelationGetIndexList(matviewRel);
721
722
0
  foreach(indexoidscan, indexoidlist)
723
0
  {
724
0
    Oid     indexoid = lfirst_oid(indexoidscan);
725
0
    Relation  indexRel;
726
727
0
    indexRel = index_open(indexoid, RowExclusiveLock);
728
0
    if (is_usable_unique_index(indexRel))
729
0
    {
730
0
      Form_pg_index indexStruct = indexRel->rd_index;
731
0
      int     indnkeyatts = indexStruct->indnkeyatts;
732
0
      oidvector  *indclass;
733
0
      Datum   indclassDatum;
734
0
      int     i;
735
736
      /* Must get indclass the hard way. */
737
0
      indclassDatum = SysCacheGetAttrNotNull(INDEXRELID,
738
0
                           indexRel->rd_indextuple,
739
0
                           Anum_pg_index_indclass);
740
0
      indclass = (oidvector *) DatumGetPointer(indclassDatum);
741
742
      /* Add quals for all columns from this index. */
743
0
      for (i = 0; i < indnkeyatts; i++)
744
0
      {
745
0
        int     attnum = indexStruct->indkey.values[i];
746
0
        Oid     opclass = indclass->values[i];
747
0
        Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
748
0
        Oid     attrtype = attr->atttypid;
749
0
        HeapTuple cla_ht;
750
0
        Form_pg_opclass cla_tup;
751
0
        Oid     opfamily;
752
0
        Oid     opcintype;
753
0
        Oid     op;
754
0
        const char *leftop;
755
0
        const char *rightop;
756
757
        /*
758
         * Identify the equality operator associated with this index
759
         * column.  First we need to look up the column's opclass.
760
         */
761
0
        cla_ht = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
762
0
        if (!HeapTupleIsValid(cla_ht))
763
0
          elog(ERROR, "cache lookup failed for opclass %u", opclass);
764
0
        cla_tup = (Form_pg_opclass) GETSTRUCT(cla_ht);
765
0
        opfamily = cla_tup->opcfamily;
766
0
        opcintype = cla_tup->opcintype;
767
0
        ReleaseSysCache(cla_ht);
768
769
0
        op = get_opfamily_member_for_cmptype(opfamily, opcintype, opcintype, COMPARE_EQ);
770
0
        if (!OidIsValid(op))
771
0
          elog(ERROR, "missing equality operator for (%u,%u) in opfamily %u",
772
0
             opcintype, opcintype, opfamily);
773
774
        /*
775
         * If we find the same column with the same equality semantics
776
         * in more than one index, we only need to emit the equality
777
         * clause once.
778
         *
779
         * Since we only remember the last equality operator, this
780
         * code could be fooled into emitting duplicate clauses given
781
         * multiple indexes with several different opclasses ... but
782
         * that's so unlikely it doesn't seem worth spending extra
783
         * code to avoid.
784
         */
785
0
        if (opUsedForQual[attnum - 1] == op)
786
0
          continue;
787
0
        opUsedForQual[attnum - 1] = op;
788
789
        /*
790
         * Actually add the qual, ANDed with any others.
791
         */
792
0
        if (foundUniqueIndex)
793
0
          appendStringInfoString(&querybuf, " AND ");
794
795
0
        leftop = quote_qualified_identifier("newdata",
796
0
                          NameStr(attr->attname));
797
0
        rightop = quote_qualified_identifier("mv",
798
0
                           NameStr(attr->attname));
799
800
0
        generate_operator_clause(&querybuf,
801
0
                     leftop, attrtype,
802
0
                     op,
803
0
                     rightop, attrtype);
804
805
0
        foundUniqueIndex = true;
806
0
      }
807
0
    }
808
809
    /* Keep the locks, since we're about to run DML which needs them. */
810
0
    index_close(indexRel, NoLock);
811
0
  }
812
813
0
  list_free(indexoidlist);
814
815
  /*
816
   * There must be at least one usable unique index on the matview.
817
   *
818
   * ExecRefreshMatView() checks that after taking the exclusive lock on the
819
   * matview. So at least one unique index is guaranteed to exist here
820
   * because the lock is still being held.  (One known exception is if a
821
   * function called as part of refreshing the matview drops the index.
822
   * That's a pretty silly thing to do.)
823
   */
824
0
  if (!foundUniqueIndex)
825
0
    ereport(ERROR,
826
0
        errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
827
0
        errmsg("could not find suitable unique index on materialized view \"%s\"",
828
0
             RelationGetRelationName(matviewRel)));
829
830
0
  appendStringInfoString(&querybuf,
831
0
               " AND newdata.* OPERATOR(pg_catalog.*=) mv.*) "
832
0
               "WHERE newdata.* IS NULL OR mv.* IS NULL "
833
0
               "ORDER BY tid");
834
835
  /* Populate the temporary "diff" table. */
836
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
837
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
838
839
  /*
840
   * We have no further use for data from the "full-data" temp table, but we
841
   * must keep it around because its type is referenced from the diff table.
842
   */
843
844
  /* Analyze the diff table. */
845
0
  resetStringInfo(&querybuf);
846
0
  appendStringInfo(&querybuf, "ANALYZE %s", diffname);
847
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
848
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
849
850
0
  OpenMatViewIncrementalMaintenance();
851
852
  /* Deletes must come before inserts; do them first. */
853
0
  resetStringInfo(&querybuf);
854
0
  appendStringInfo(&querybuf,
855
0
           "DELETE FROM %s mv WHERE ctid OPERATOR(pg_catalog.=) ANY "
856
0
           "(SELECT diff.tid FROM %s diff "
857
0
           "WHERE diff.tid IS NOT NULL "
858
0
           "AND diff.newdata IS NULL)",
859
0
           matviewname, diffname);
860
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_DELETE)
861
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
862
863
  /* Inserts go last. */
864
0
  resetStringInfo(&querybuf);
865
0
  appendStringInfo(&querybuf,
866
0
           "INSERT INTO %s SELECT (diff.newdata).* "
867
0
           "FROM %s diff WHERE tid IS NULL",
868
0
           matviewname, diffname);
869
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
870
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
871
872
  /* We're done maintaining the materialized view. */
873
0
  CloseMatViewIncrementalMaintenance();
874
0
  table_close(tempRel, NoLock);
875
0
  table_close(matviewRel, NoLock);
876
877
  /* Clean up temp tables. */
878
0
  resetStringInfo(&querybuf);
879
0
  appendStringInfo(&querybuf, "DROP TABLE %s, %s", diffname, tempname);
880
0
  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
881
0
    elog(ERROR, "SPI_exec failed: %s", querybuf.data);
882
883
  /* Close SPI context. */
884
0
  if (SPI_finish() != SPI_OK_FINISH)
885
0
    elog(ERROR, "SPI_finish failed");
886
0
}
887
888
/*
889
 * Swap the physical files of the target and transient tables, then rebuild
890
 * the target's indexes and throw away the transient table.  Security context
891
 * swapping is handled by the called function, so it is not needed here.
892
 */
893
static void
894
refresh_by_heap_swap(Oid matviewOid, Oid OIDNewHeap, char relpersistence)
895
0
{
896
0
  finish_heap_swap(matviewOid, OIDNewHeap, false, false, true, true,
897
0
           true,   /* reindex */
898
0
           RecentXmin, ReadNextMultiXactId(), relpersistence);
899
0
}
900
901
/*
902
 * Check whether specified index is usable for match merge.
903
 */
904
static bool
905
is_usable_unique_index(Relation indexRel)
906
0
{
907
0
  Form_pg_index indexStruct = indexRel->rd_index;
908
909
  /*
910
   * Must be unique, valid, immediate, non-partial, and be defined over
911
   * plain user columns (not expressions).
912
   */
913
0
  if (indexStruct->indisunique &&
914
0
    indexStruct->indimmediate &&
915
0
    indexStruct->indisvalid &&
916
0
    RelationGetIndexPredicate(indexRel) == NIL &&
917
0
    indexStruct->indnatts > 0)
918
0
  {
919
    /*
920
     * The point of groveling through the index columns individually is to
921
     * reject both index expressions and system columns.  Currently,
922
     * matviews couldn't have OID columns so there's no way to create an
923
     * index on a system column; but maybe someday that wouldn't be true,
924
     * so let's be safe.
925
     */
926
0
    int     numatts = indexStruct->indnatts;
927
0
    int     i;
928
929
0
    for (i = 0; i < numatts; i++)
930
0
    {
931
0
      int     attnum = indexStruct->indkey.values[i];
932
933
0
      if (attnum <= 0)
934
0
        return false;
935
0
    }
936
0
    return true;
937
0
  }
938
0
  return false;
939
0
}
940
941
942
/*
943
 * This should be used to test whether the backend is in a context where it is
944
 * OK to allow DML statements to modify materialized views.  We only want to
945
 * allow that for internal code driven by the materialized view definition,
946
 * not for arbitrary user-supplied code.
947
 *
948
 * While the function names reflect the fact that their main intended use is
949
 * incremental maintenance of materialized views (in response to changes to
950
 * the data in referenced relations), they are initially used to allow REFRESH
951
 * without blocking concurrent reads.
952
 */
953
bool
954
MatViewIncrementalMaintenanceIsEnabled(void)
955
0
{
956
0
  return matview_maintenance_depth > 0;
957
0
}
958
959
static void
960
OpenMatViewIncrementalMaintenance(void)
961
0
{
962
0
  matview_maintenance_depth++;
963
0
}
964
965
static void
966
CloseMatViewIncrementalMaintenance(void)
967
0
{
968
0
  matview_maintenance_depth--;
969
  Assert(matview_maintenance_depth >= 0);
970
0
}