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