68 const char *queryString);
71 int save_sec_context);
100 elog(
ERROR,
"cache lookup failed for relation %u",
150 uint64 processed = 0;
155 int save_sec_context;
172 if (matviewRel->
rd_rel->relkind != RELKIND_MATVIEW)
174 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
175 errmsg(
"\"%s\" is not a materialized view",
181 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
182 errmsg(
"CONCURRENTLY cannot be used when the materialized view is not populated")));
187 (
errcode(ERRCODE_SYNTAX_ERROR),
188 errmsg(
"CONCURRENTLY and WITH NO DATA options cannot be used together")));
194 if (matviewRel->
rd_rel->relhasrules ==
false ||
197 "materialized view \"%s\" is missing rewrite information",
202 "materialized view \"%s\" has too many rules",
208 "the rule for materialized view \"%s\" is not a SELECT INSTEAD OF rule",
214 "the rule for materialized view \"%s\" is not a single action",
225 bool hasUniqueIndex =
false;
227 foreach(indexoidscan, indexoidlist)
243 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
244 errmsg(
"cannot refresh materialized view \"%s\" concurrently",
247 errhint(
"Create a unique index with no WHERE clause on one or more columns of the materialized view.")));
271 relowner = matviewRel->
rd_rel->relowner;
288 relpersistence = RELPERSISTENCE_TEMP;
292 tableSpace = matviewRel->
rd_rel->reltablespace;
293 relpersistence = matviewRel->
rd_rel->relpersistence;
301 OIDNewHeap =
make_new_heap(matviewOid, tableSpace, relpersistence,
383 const char *queryString)
398 elog(
ERROR,
"unexpected rewrite result for REFRESH MATERIALIZED VIEW");
419 dest, NULL, NULL, 0);
450 self->transientoid = transientoid;
588 int save_sec_context)
597 bool foundUniqueIndex;
632 "SELECT newdata FROM %s newdata " 633 "WHERE newdata IS NOT NULL AND EXISTS " 634 "(SELECT 1 FROM %s newdata2 WHERE newdata2 IS NOT NULL " 635 "AND newdata2 OPERATOR(pg_catalog.*=) newdata " 636 "AND newdata2.ctid OPERATOR(pg_catalog.<>) " 651 (
errcode(ERRCODE_CARDINALITY_VIOLATION),
652 errmsg(
"new data for materialized view \"%s\" contains duplicate rows without any null columns",
664 "CREATE TEMP TABLE %s AS " 665 "SELECT mv.ctid AS tid, newdata " 666 "FROM %s mv FULL JOIN %s newdata ON (",
667 diffname, matviewname, tempname);
675 tupdesc = matviewRel->
rd_att;
677 foundUniqueIndex =
false;
681 foreach(indexoidscan, indexoidlist)
690 int indnkeyatts = indexStruct->indnkeyatts;
699 Anum_pg_index_indclass,
705 for (i = 0; i < indnkeyatts; i++)
707 int attnum = indexStruct->indkey.values[
i];
710 Oid attrtype = attr->atttypid;
725 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
727 Assert(cla_tup->opcmethod == BTREE_AM_OID);
728 opfamily = cla_tup->opcfamily;
729 opcintype = cla_tup->opcintype;
735 elog(
ERROR,
"missing operator %d(%u,%u) in opfamily %u",
749 if (opUsedForQual[attnum - 1] == op)
751 opUsedForQual[attnum - 1] = op;
756 if (foundUniqueIndex)
769 foundUniqueIndex =
true;
789 " AND newdata OPERATOR(pg_catalog.*=) mv) " 790 "WHERE newdata IS NULL OR mv IS NULL " 816 "DELETE FROM %s mv WHERE ctid OPERATOR(pg_catalog.=) ANY " 817 "(SELECT diff.tid FROM %s diff " 818 "WHERE diff.tid IS NOT NULL " 819 "AND diff.newdata IS NULL)",
820 matviewname, diffname);
827 "INSERT INTO %s SELECT (diff.newdata).* " 828 "FROM %s diff WHERE tid IS NULL",
829 matviewname, diffname);
877 if (indexStruct->indisunique &&
878 indexStruct->indimmediate &&
879 indexRel->
rd_rel->relam == BTREE_AM_OID &&
880 indexStruct->indisvalid &&
882 indexStruct->indnatts > 0)
891 int numatts = indexStruct->indnatts;
894 for (i = 0; i < numatts; i++)
896 int attnum = indexStruct->indkey.values[
i];
#define RelationIsPopulated(relation)
static bool is_usable_unique_index(Relation indexRel)
void RangeVarCallbackOwnsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
void UpdateActiveSnapshotCommandId(void)
List * QueryRewrite(Query *parsetree)
void table_close(Relation relation, LOCKMODE lockmode)
int errhint(const char *fmt,...)
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)
#define SECURITY_RESTRICTED_OPERATION
void SetUserIdAndSecContext(Oid userid, int sec_context)
#define TABLE_INSERT_FROZEN
void FreeQueryDesc(QueryDesc *qdesc)
#define RelationGetNumberOfAttributes(relation)
bool MatViewIncrementalMaintenanceIsEnabled(void)
#define TupleDescAttr(tupdesc, i)
static void transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Snapshot GetActiveSnapshot(void)
SPITupleTable * SPI_tuptable
static void CloseMatViewIncrementalMaintenance(void)
int errcode(int sqlerrcode)
static void table_finish_bulk_insert(Relation rel, int options)
void PopActiveSnapshot(void)
void heap_freetuple(HeapTuple htup)
#define linitial_node(type, l)
#define OidIsValid(objectId)
#define RelationGetTargetBlock(relation)
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
List * RelationGetIndexPredicate(Relation relation)
struct HeapTupleData * rd_indextuple
static char * make_temptable_name_n(char *tempname, int n)
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
void ExecutorEnd(QueryDesc *queryDesc)
BulkInsertState GetBulkInsertState(void)
void pfree(void *pointer)
void appendStringInfo(StringInfo str, const char *fmt,...)
#define ObjectIdGetDatum(X)
int SPI_exec(const char *src, long tcount)
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
void PushCopiedSnapshot(Snapshot snapshot)
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
static void transientrel_destroy(DestReceiver *self)
void appendStringInfoString(StringInfo str, const char *s)
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
char * get_namespace_name(Oid nspid)
Oid values[FLEXIBLE_ARRAY_MEMBER]
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
void AtEOXact_GUC(bool isCommit, int nestLevel)
int errdetail(const char *fmt,...)
static void transientrel_shutdown(DestReceiver *self)
static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self)
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
#define RelationGetRelationName(relation)
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
FormData_pg_attribute * Form_pg_attribute
void resetStringInfo(StringInfo str)
void pgstat_count_truncate(Relation rel)
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
void CheckTableNotInUse(Relation rel, const char *stmt)
void ExecutorFinish(QueryDesc *queryDesc)
static void refresh_by_heap_swap(Oid matviewOid, Oid OIDNewHeap, char relpersistence)
void initStringInfo(StringInfo str)
char * quote_qualified_identifier(const char *qualifier, const char *ident)
FormData_pg_index * Form_pg_index
HeapTuple SearchSysCache1(int cacheId, Datum key1)
void * palloc0(Size size)
void CommandCounterIncrement(void)
void ReleaseSysCache(HeapTuple tuple)
static struct state * newstate(struct nfa *nfa)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
#define TABLE_INSERT_SKIP_FSM
#define SECURITY_LOCAL_USERID_CHANGE
#define ereport(elevel,...)
#define HeapTupleIsValid(tuple)
#define Assert(condition)
void FreeBulkInsertState(BulkInsertState bistate)
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query, const char *queryString)
ObjectAddress ExecRefreshMatView(RefreshMatViewStmt *stmt, const char *queryString, ParamListInfo params, QueryCompletion *qc)
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
#define InvalidBlockNumber
static int list_length(const List *l)
static void OpenMatViewIncrementalMaintenance(void)
List * RelationGetIndexList(Relation relation)
#define ObjectAddressSet(addr, class_id, object_id)
void index_close(Relation relation, LOCKMODE lockmode)
#define DatumGetPointer(X)
FormData_pg_class * Form_pg_class
#define SearchSysCacheCopy1(cacheId, key1)
#define AccessExclusiveLock
DestReceiver * CreateTransientRelDestReceiver(Oid transientoid)
int NewGUCNestLevel(void)
void pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
int errmsg(const char *fmt,...)
static int matview_maintenance_depth
void list_free(List *list)
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
#define CHECK_FOR_INTERRUPTS()
CommandId GetCurrentCommandId(bool used)
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
FormData_pg_opclass * Form_pg_opclass
#define RelationGetRelid(relation)
void SetMatViewPopulatedState(Relation relation, bool newstate)
Relation index_open(Oid relationId, LOCKMODE lockmode)
#define BTEqualStrategyNumber
Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, char relpersistence, LOCKMODE lockmode)
void generate_operator_clause(StringInfo buf, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
static void refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, int save_sec_context)
int SPI_execute(const char *src, bool read_only, long tcount)
MultiXactId ReadNextMultiXactId(void)
#define RelationGetNamespace(relation)