1/*-------------------------------------------------------------------------
4 * materialized view support
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/commands/matview.c
13 *-------------------------------------------------------------------------
49 /* These fields are filled by transientrel_startup: */
52 int ti_options;
/* table_tuple_insert performance options */
63 const char *queryString,
bool is_create);
66 int save_sec_context);
73 * SetMatViewPopulatedState
74 * Mark a materialized view as populated, or not.
76 * NOTE: caller must be holding an appropriate lock on the relation.
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
95 elog(
ERROR,
"cache lookup failed for relation %u",
106 * Advance command counter to make the updated pg_class row locally
113 * ExecRefreshMatView -- execute a REFRESH MATERIALIZED VIEW command
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.
127 /* Determine strength of lock needed. */
131 * Get a lock until end of transaction.
139 stmt->concurrent, queryString, qc);
143 * RefreshMatViewByOid -- refresh materialized view by OID
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.
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.
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.
158 * The matview's "populated" state is changed based on whether the contents
159 * reflect the result set of the materialized view's query.
161 * This is also used to populate the materialized view created by CREATE
162 * MATERIALIZED VIEW command.
166 bool concurrent,
const char *queryString,
179 int save_sec_context;
184 relowner = matviewRel->
rd_rel->relowner;
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.
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",
204 /* Check that CONCURRENTLY is not specified if not populated. */
207 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
208 errmsg(
"CONCURRENTLY cannot be used when the materialized view is not populated")));
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")));
218 * Check that everything is correct for a refresh. Problems at this point
219 * are internal errors, so elog is sufficient.
221 if (matviewRel->
rd_rel->relhasrules ==
false ||
224 "materialized view \"%s\" is missing rewrite information",
229 "materialized view \"%s\" has too many rules",
235 "the rule for materialized view \"%s\" is not a SELECT INSTEAD OF rule",
238 actions =
rule->actions;
241 "the rule for materialized view \"%s\" is not a single action",
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.
252 bool hasUniqueIndex =
false;
256 foreach(indexoidscan, indexoidlist)
272 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
273 errmsg(
"cannot refresh materialized view \"%s\" concurrently",
276 errhint(
"Create a unique index with no WHERE clause on one or more columns of the materialized view.")));
280 * The stored query was rewritten at the time of the MV definition, but
281 * has not been scribbled on by the planner.
286 * Check for active uses of the relation in the current transaction, such
289 * NB: We count on this to protect us against problems with refreshing the
290 * data using TABLE_INSERT_FROZEN.
293 is_create ?
"CREATE MATERIALIZED VIEW" :
294 "REFRESH MATERIALIZED VIEW");
297 * Tentatively mark the matview as populated or not (this will roll back
302 /* Concurrent refresh builds new data in temp tablespace, and does diff. */
306 relpersistence = RELPERSISTENCE_TEMP;
310 tableSpace = matviewRel->
rd_rel->reltablespace;
311 relpersistence = matviewRel->
rd_rel->relpersistence;
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
320 matviewRel->
rd_rel->relam,
324 /* Generate the data, if wanted. */
334 /* Make the matview match the newly generated data. */
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.)
369 /* Roll back any GUC changes */
372 /* Restore userid and security context */
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.
385 * When called from CREATE MATERIALIZED VIEW command, the rowcount is
386 * displayed with the command tag CMDTAG_SELECT.
390 is_create ? CMDTAG_SELECT : CMDTAG_REFRESH_MATERIALIZED_VIEW,
397 * refresh_matview_datafill
399 * Execute the given query, sending result rows to "dest" (which will
400 * insert them into the target matview).
402 * Returns number of rows inserted.
406 const char *queryString,
bool is_create)
414 /* Lock and rewrite, using a copy to preserve the original query. */
419 /* SELECT should never rewrite to more or less than one SELECT query */
421 elog(
ERROR,
"unexpected rewrite result for %s",
422 is_create ?
"CREATE MATERIALIZED VIEW " :
"REFRESH MATERIALIZED VIEW");
425 /* Check for user-requested abort. */
428 /* Plan the query which will generate data for the refresh. */
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.)
440 /* Create a QueryDesc, redirecting output to our tuple receiver */
443 dest, NULL, NULL, 0);
445 /* call ExecutorStart to prepare the plan for execution */
480 * transientrel_startup --- executor startup
491 * Fill private fields of myState for use by later routines
499 * Valid smgr_targblock implies something already wrote to the relation.
500 * This may be harmless, but this function hasn't planned for it.
506 * transientrel_receive --- receive one tuple
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...
528 /* We know this is a newly created relation, so there are no indexes */
534 * transientrel_shutdown --- executor end
545 /* close transientrel, but keep lock until commit */
551 * transientrel_destroy --- release DestReceiver object
561 * Given a qualified temporary table name, append an underscore followed by
562 * the given integer, to make a new table name based on the old one.
563 * The result is a palloc'd string.
565 * As coded, this would fail to make a valid SQL name if the given name were,
566 * say, "FOO"."BAR". Currently, the table name portion of the input will
567 * never be double-quoted because it's of the form "pg_temp_NNN", cf
568 * make_new_heap(). But we might have to work harder someday.
582 * refresh_by_match_merge
584 * Refresh a materialized view with transactional semantics, while allowing
587 * This is called after a new version of the data has been created in a
588 * temporary table. It performs a full outer join against the old version of
589 * the data, producing "diff" results. This join cannot work if there are any
590 * duplicated rows in either the old or new versions, in the sense that every
591 * column would compare as equal between the two rows. It does work correctly
592 * in the face of rows which have at least one NULL value, with all non-NULL
593 * columns equal. The behavior of NULLs on equality tests and on UNIQUE
594 * indexes turns out to be quite convenient here; the tests we need to make
595 * are consistent with default behavior. If there is at least one UNIQUE
596 * index on the materialized view, we have exactly the guarantee we need.
598 * The temporary table used to hold the diff results contains just the TID of
599 * the old record (if matched) and the ROW from the new table as a single
600 * column of complex record type (if matched).
602 * Once we have the diff table, we perform set-based DELETE and INSERT
603 * operations against the materialized view, and discard both temporary
606 * Everything from the generation of the new data to applying the differences
607 * takes place under cover of an ExclusiveLock, since it seems as though we
608 * would want to prohibit not only concurrent REFRESH operations, but also
609 * incremental maintenance. It also doesn't seem reasonable or safe to allow
610 * SELECT FOR UPDATE or SELECT FOR SHARE on rows being updated or deleted by
615 int save_sec_context)
624 bool foundUniqueIndex;
641 /* Open SPI context. */
644 /* Analyze the temp table with the new contents. */
650 * We need to ensure that there are not duplicate rows without NULLs in
651 * the new data set before we can count on the "diff" results. Check for
652 * that in a way that allows showing the first duplicated row found. Even
653 * after we pass this test, a unique index on the materialized view may
654 * find a duplicate key problem.
656 * Note: here and below, we use "tablename.*::tablerowtype" as a hack to
657 * keep ".*" from being expanded into multiple columns in a SELECT list.
658 * Compare ruleutils.c's get_variable().
662 "SELECT newdata.*::%s FROM %s newdata "
663 "WHERE newdata.* IS NOT NULL AND EXISTS "
664 "(SELECT 1 FROM %s newdata2 WHERE newdata2.* IS NOT NULL "
665 "AND newdata2.* OPERATOR(pg_catalog.*=) newdata.* "
666 "AND newdata2.ctid OPERATOR(pg_catalog.<>) "
668 tempname, tempname, tempname);
674 * Note that this ereport() is returning data to the user. Generally,
675 * we would want to make sure that the user has been granted access to
676 * this data. However, REFRESH MAT VIEW is only able to be run by the
677 * owner of the mat view (or a superuser) and therefore there is no
678 * need to check for access to data in the mat view.
681 (
errcode(ERRCODE_CARDINALITY_VIOLATION),
682 errmsg(
"new data for materialized view \"%s\" contains duplicate rows without any null columns",
689 * Create the temporary "diff" table.
691 * Temporarily switch out of the SECURITY_RESTRICTED_OPERATION context,
692 * because you cannot create temp tables in SRO context. For extra
693 * paranoia, add the composite type column only after switching back to
700 "CREATE TEMP TABLE %s (tid pg_catalog.tid)",
708 "ALTER TABLE %s ADD COLUMN newdata %s",
713 /* Start building the query for populating the diff table. */
717 "SELECT mv.ctid AS tid, newdata.*::%s AS newdata "
718 "FROM %s mv FULL JOIN %s newdata ON (",
719 diffname, tempname, matviewname, tempname);
722 * Get the list of index OIDs for the table from the relcache, and look up
723 * each one in the pg_index syscache. We will test for equality on all
724 * columns present in all unique indexes which only reference columns and
727 tupdesc = matviewRel->
rd_att;
729 foundUniqueIndex =
false;
733 foreach(indexoidscan, indexoidlist)
742 int indnkeyatts = indexStruct->indnkeyatts;
747 /* Must get indclass the hard way. */
750 Anum_pg_index_indclass);
753 /* Add quals for all columns from this index. */
754 for (
i = 0;
i < indnkeyatts;
i++)
756 int attnum = indexStruct->indkey.values[
i];
759 Oid attrtype = attr->atttypid;
769 * Identify the equality operator associated with this index
770 * column. First we need to look up the column's opclass.
774 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
776 opfamily = cla_tup->opcfamily;
777 opcintype = cla_tup->opcintype;
782 elog(
ERROR,
"missing equality operator for (%u,%u) in opfamily %u",
783 opcintype, opcintype, opfamily);
786 * If we find the same column with the same equality semantics
787 * in more than one index, we only need to emit the equality
790 * Since we only remember the last equality operator, this
791 * code could be fooled into emitting duplicate clauses given
792 * multiple indexes with several different opclasses ... but
793 * that's so unlikely it doesn't seem worth spending extra
796 if (opUsedForQual[
attnum - 1] == op)
798 opUsedForQual[
attnum - 1] = op;
801 * Actually add the qual, ANDed with any others.
803 if (foundUniqueIndex)
816 foundUniqueIndex =
true;
820 /* Keep the locks, since we're about to run DML which needs them. */
827 * There must be at least one usable unique index on the matview.
829 * ExecRefreshMatView() checks that after taking the exclusive lock on the
830 * matview. So at least one unique index is guaranteed to exist here
831 * because the lock is still being held. (One known exception is if a
832 * function called as part of refreshing the matview drops the index.
833 * That's a pretty silly thing to do.)
835 if (!foundUniqueIndex)
837 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
838 errmsg(
"could not find suitable unique index on materialized view \"%s\"",
842 " AND newdata.* OPERATOR(pg_catalog.*=) mv.*) "
843 "WHERE newdata.* IS NULL OR mv.* IS NULL "
846 /* Populate the temporary "diff" table. */
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.
855 /* Analyze the diff table. */
863 /* Deletes must come before inserts; do them first. */
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);
874 /* Inserts go last. */
877 "INSERT INTO %s SELECT (diff.newdata).* "
878 "FROM %s diff WHERE tid IS NULL",
879 matviewname, diffname);
883 /* We're done maintaining the materialized view. */
888 /* Clean up temp tables. */
894 /* Close SPI context. */
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.
912 * Check whether specified index is usable for match merge.
920 * Must be unique, valid, immediate, non-partial, and be defined over
921 * plain user columns (not expressions).
923 if (indexStruct->indisunique &&
924 indexStruct->indimmediate &&
925 indexStruct->indisvalid &&
927 indexStruct->indnatts > 0)
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,
936 int numatts = indexStruct->indnatts;
939 for (
i = 0;
i < numatts;
i++)
941 int attnum = indexStruct->indkey.values[
i];
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.
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.
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
#define InvalidBlockNumber
#define OidIsValid(objectId)
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)
Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid NewAccessMethod, char relpersistence, LOCKMODE lockmode)
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void ExecutorEnd(QueryDesc *queryDesc)
void ExecutorFinish(QueryDesc *queryDesc)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
int NewGUCNestLevel(void)
void RestrictSearchPath(void)
void AtEOXact_GUC(bool isCommit, int nestLevel)
Assert(PointerIsAligned(start, uint64))
BulkInsertState GetBulkInsertState(void)
void FreeBulkInsertState(BulkInsertState bistate)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
void index_close(Relation relation, LOCKMODE lockmode)
Relation index_open(Oid relationId, LOCKMODE lockmode)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
void list_free(List *list)
bool CheckRelationOidLockedByMe(Oid relid, LOCKMODE lockmode, bool orstronger)
#define AccessExclusiveLock
Oid get_opfamily_member_for_cmptype(Oid opfamily, Oid lefttype, Oid righttype, CompareType cmptype)
char * get_namespace_name(Oid nspid)
DestReceiver * CreateTransientRelDestReceiver(Oid transientoid)
static void transientrel_destroy(DestReceiver *self)
static void transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
static char * make_temptable_name_n(char *tempname, int n)
static void refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, int save_sec_context)
static bool is_usable_unique_index(Relation indexRel)
bool MatViewIncrementalMaintenanceIsEnabled(void)
static void CloseMatViewIncrementalMaintenance(void)
static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query, const char *queryString, bool is_create)
static void OpenMatViewIncrementalMaintenance(void)
void SetMatViewPopulatedState(Relation relation, bool newstate)
ObjectAddress ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString, QueryCompletion *qc)
static int matview_maintenance_depth
ObjectAddress RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData, bool concurrent, const char *queryString, QueryCompletion *qc)
static void refresh_by_heap_swap(Oid matviewOid, Oid OIDNewHeap, char relpersistence)
static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self)
static void transientrel_shutdown(DestReceiver *self)
void pfree(void *pointer)
void * palloc0(Size size)
#define SECURITY_RESTRICTED_OPERATION
#define CHECK_FOR_INTERRUPTS()
#define SECURITY_LOCAL_USERID_CHANGE
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
void SetUserIdAndSecContext(Oid userid, int sec_context)
MultiXactId ReadNextMultiXactId(void)
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
#define ObjectAddressSet(addr, class_id, object_id)
#define CURSOR_OPT_PARALLEL_OK
FormData_pg_attribute * Form_pg_attribute
FormData_pg_class * Form_pg_class
FormData_pg_index * Form_pg_index
static int list_length(const List *l)
#define linitial_node(type, l)
FormData_pg_opclass * Form_pg_opclass
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)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
void FreeQueryDesc(QueryDesc *qdesc)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
static struct state * newstate(struct nfa *nfa)
#define RelationGetRelid(relation)
#define RelationGetNumberOfAttributes(relation)
#define RelationGetRelationName(relation)
#define RelationGetTargetBlock(relation)
#define RelationIsPopulated(relation)
#define RelationGetNamespace(relation)
List * RelationGetIndexList(Relation relation)
List * RelationGetIndexPredicate(Relation relation)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
List * QueryRewrite(Query *parsetree)
char * quote_qualified_identifier(const char *qualifier, const char *ident)
void generate_operator_clause(StringInfo buf, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
void UpdateActiveSnapshotCommandId(void)
void PopActiveSnapshot(void)
void PushCopiedSnapshot(Snapshot snapshot)
Snapshot GetActiveSnapshot(void)
SPITupleTable * SPI_tuptable
int SPI_exec(const char *src, long tcount)
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
int SPI_execute(const char *src, bool read_only, long tcount)
void resetStringInfo(StringInfo str)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void initStringInfo(StringInfo str)
struct HeapTupleData * rd_indextuple
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
void(* rShutdown)(DestReceiver *self)
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
void(* rDestroy)(DestReceiver *self)
Oid values[FLEXIBLE_ARRAY_MEMBER]
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
#define SearchSysCacheCopy1(cacheId, key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
#define TABLE_INSERT_FROZEN
#define TABLE_INSERT_SKIP_FSM
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, BulkInsertStateData *bistate)
static void table_finish_bulk_insert(Relation rel, int options)
void CheckTableNotInUse(Relation rel, const char *stmt)
void RangeVarCallbackMaintainsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
void CommandCounterIncrement(void)
CommandId GetCurrentCommandId(bool used)