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