63 const char *queryString,
bool is_create);
66 int save_sec_context);
95 elog(
ERROR,
"cache lookup failed for relation %u",
139 stmt->concurrent, queryString, qc);
166 bool concurrent,
const char *queryString,
179 int save_sec_context;
184 relowner = matviewRel->
rd_rel->relowner;
198 if (matviewRel->
rd_rel->relkind != RELKIND_MATVIEW)
200 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
201 errmsg(
"\"%s\" is not a materialized view",
207 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
208 errmsg(
"CONCURRENTLY cannot be used when the materialized view is not populated")));
211 if (concurrent && skipData)
213 (
errcode(ERRCODE_SYNTAX_ERROR),
214 errmsg(
"%s and %s options cannot be used together",
215 "CONCURRENTLY",
"WITH NO DATA")));
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",
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.")));
293 is_create ?
"CREATE MATERIALIZED VIEW" :
294 "REFRESH MATERIALIZED VIEW");
306 relpersistence = RELPERSISTENCE_TEMP;
310 tableSpace = matviewRel->
rd_rel->reltablespace;
311 relpersistence = matviewRel->
rd_rel->relpersistence;
320 matviewRel->
rd_rel->relam,
390 is_create ? CMDTAG_SELECT : CMDTAG_REFRESH_MATERIALIZED_VIEW,
406 const char *queryString,
bool is_create)
421 elog(
ERROR,
"unexpected rewrite result for %s",
422 is_create ?
"CREATE MATERIALIZED VIEW " :
"REFRESH MATERIALIZED VIEW");
443 dest, NULL, NULL, 0);
615 int save_sec_context)
624 bool foundUniqueIndex;
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);
681 (
errcode(ERRCODE_CARDINALITY_VIOLATION),
682 errmsg(
"new data for materialized view \"%s\" contains duplicate rows without any null columns",
700 "CREATE TEMP TABLE %s (tid pg_catalog.tid)",
708 "ALTER TABLE %s ADD COLUMN newdata %s",
717 "SELECT mv.ctid AS tid, newdata.*::%s AS newdata "
718 "FROM %s mv FULL JOIN %s newdata ON (",
719 diffname, tempname, matviewname, tempname);
727 tupdesc = matviewRel->
rd_att;
729 foundUniqueIndex =
false;
733 foreach(indexoidscan, indexoidlist)
742 int indnkeyatts = indexStruct->indnkeyatts;
750 Anum_pg_index_indclass);
754 for (
i = 0;
i < indnkeyatts;
i++)
756 int attnum = indexStruct->indkey.values[
i];
759 Oid attrtype = attr->atttypid;
774 elog(
ERROR,
"cache lookup failed for opclass %u", opclass);
776 Assert(cla_tup->opcmethod == BTREE_AM_OID);
777 opfamily = cla_tup->opcfamily;
778 opcintype = cla_tup->opcintype;
784 elog(
ERROR,
"missing operator %d(%u,%u) in opfamily %u",
798 if (opUsedForQual[
attnum - 1] == op)
800 opUsedForQual[
attnum - 1] = op;
805 if (foundUniqueIndex)
818 foundUniqueIndex =
true;
837 if (!foundUniqueIndex)
839 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
840 errmsg(
"could not find suitable unique index on materialized view"));
843 " AND newdata.* OPERATOR(pg_catalog.*=) mv.*) "
844 "WHERE newdata.* IS NULL OR mv.* IS NULL "
867 "DELETE FROM %s mv WHERE ctid OPERATOR(pg_catalog.=) ANY "
868 "(SELECT diff.tid FROM %s diff "
869 "WHERE diff.tid IS NOT NULL "
870 "AND diff.newdata IS NULL)",
871 matviewname, diffname);
878 "INSERT INTO %s SELECT (diff.newdata).* "
879 "FROM %s diff WHERE tid IS NULL",
880 matviewname, diffname);
928 if (indexStruct->indisunique &&
929 indexStruct->indimmediate &&
930 indexRel->
rd_rel->relam == BTREE_AM_OID &&
931 indexStruct->indisvalid &&
933 indexStruct->indnatts > 0)
942 int numatts = indexStruct->indnatts;
945 for (
i = 0;
i < numatts;
i++)
947 int attnum = indexStruct->indkey.values[
i];
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
#define InvalidBlockNumber
#define Assert(condition)
#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)
BulkInsertState GetBulkInsertState(void)
void FreeBulkInsertState(BulkInsertState bistate)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(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)
void LockRelationOid(Oid relid, LOCKMODE lockmode)
#define AccessExclusiveLock
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
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)
#define BTEqualStrategyNumber
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_finish_bulk_insert(Relation rel, int options)
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
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)