PostgreSQL Source Code  git master
matview.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/multixact.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "catalog/pg_opclass.h"
#include "commands/cluster.h"
#include "commands/matview.h"
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "rewrite/rewriteHandler.h"
#include "storage/lmgr.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for matview.c:

Go to the source code of this file.

Data Structures

struct  DR_transientrel
 

Functions

static void transientrel_startup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
static bool transientrel_receive (TupleTableSlot *slot, DestReceiver *self)
 
static void transientrel_shutdown (DestReceiver *self)
 
static void transientrel_destroy (DestReceiver *self)
 
static uint64 refresh_matview_datafill (DestReceiver *dest, Query *query, const char *queryString, bool is_create)
 
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 void refresh_by_heap_swap (Oid matviewOid, Oid OIDNewHeap, char relpersistence)
 
static bool is_usable_unique_index (Relation indexRel)
 
static void OpenMatViewIncrementalMaintenance (void)
 
static void CloseMatViewIncrementalMaintenance (void)
 
void SetMatViewPopulatedState (Relation relation, bool newstate)
 
ObjectAddress ExecRefreshMatView (RefreshMatViewStmt *stmt, const char *queryString, QueryCompletion *qc)
 
ObjectAddress RefreshMatViewByOid (Oid matviewOid, bool is_create, bool skipData, bool concurrent, const char *queryString, QueryCompletion *qc)
 
DestReceiverCreateTransientRelDestReceiver (Oid transientoid)
 
bool MatViewIncrementalMaintenanceIsEnabled (void)
 

Variables

static int matview_maintenance_depth = 0
 

Function Documentation

◆ CloseMatViewIncrementalMaintenance()

static void CloseMatViewIncrementalMaintenance ( void  )
static

Definition at line 982 of file matview.c.

983 {
986 }
#define Assert(condition)
Definition: c.h:849
static int matview_maintenance_depth
Definition: matview.c:56

References Assert, and matview_maintenance_depth.

Referenced by refresh_by_match_merge().

◆ CreateTransientRelDestReceiver()

DestReceiver* CreateTransientRelDestReceiver ( Oid  transientoid)

Definition at line 465 of file matview.c.

466 {
468 
469  self->pub.receiveSlot = transientrel_receive;
470  self->pub.rStartup = transientrel_startup;
471  self->pub.rShutdown = transientrel_shutdown;
472  self->pub.rDestroy = transientrel_destroy;
473  self->pub.mydest = DestTransientRel;
474  self->transientoid = transientoid;
475 
476  return (DestReceiver *) self;
477 }
@ DestTransientRel
Definition: dest.h:97
static void transientrel_destroy(DestReceiver *self)
Definition: matview.c:554
static void transientrel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: matview.c:483
static bool transientrel_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: matview.c:509
static void transientrel_shutdown(DestReceiver *self)
Definition: matview.c:537
void * palloc0(Size size)
Definition: mcxt.c:1347

References DestTransientRel, palloc0(), transientrel_destroy(), transientrel_receive(), transientrel_shutdown(), and transientrel_startup().

Referenced by CreateDestReceiver(), and RefreshMatViewByOid().

◆ ExecRefreshMatView()

ObjectAddress ExecRefreshMatView ( RefreshMatViewStmt stmt,
const char *  queryString,
QueryCompletion qc 
)

Definition at line 121 of file matview.c.

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 }
#define stmt
Definition: indent_codes.h:59
int LOCKMODE
Definition: lockdefs.h:26
#define AccessExclusiveLock
Definition: lockdefs.h:43
#define ExclusiveLock
Definition: lockdefs.h:42
ObjectAddress RefreshMatViewByOid(Oid matviewOid, bool is_create, bool skipData, bool concurrent, const char *queryString, QueryCompletion *qc)
Definition: matview.c:165
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
Definition: namespace.c:441
unsigned int Oid
Definition: postgres_ext.h:31
void RangeVarCallbackMaintainsTable(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Definition: tablecmds.c:17839

References AccessExclusiveLock, ExclusiveLock, RangeVarCallbackMaintainsTable(), RangeVarGetRelidExtended(), RefreshMatViewByOid(), and stmt.

Referenced by ProcessUtilitySlow().

◆ is_usable_unique_index()

static bool is_usable_unique_index ( Relation  indexRel)
static

Definition at line 916 of file matview.c.

917 {
918  Form_pg_index indexStruct = indexRel->rd_index;
919 
920  /*
921  * Must be unique, valid, immediate, non-partial, and be defined over
922  * plain user columns (not expressions). We also require it to be a
923  * btree. Even if we had any other unique index kinds, we'd not know how
924  * to identify the corresponding equality operator, nor could we be sure
925  * that the planner could implement the required FULL JOIN with non-btree
926  * operators.
927  */
928  if (indexStruct->indisunique &&
929  indexStruct->indimmediate &&
930  indexRel->rd_rel->relam == BTREE_AM_OID &&
931  indexStruct->indisvalid &&
932  RelationGetIndexPredicate(indexRel) == NIL &&
933  indexStruct->indnatts > 0)
934  {
935  /*
936  * The point of groveling through the index columns individually is to
937  * reject both index expressions and system columns. Currently,
938  * matviews couldn't have OID columns so there's no way to create an
939  * index on a system column; but maybe someday that wouldn't be true,
940  * so let's be safe.
941  */
942  int numatts = indexStruct->indnatts;
943  int i;
944 
945  for (i = 0; i < numatts; i++)
946  {
947  int attnum = indexStruct->indkey.values[i];
948 
949  if (attnum <= 0)
950  return false;
951  }
952  return true;
953  }
954  return false;
955 }
int i
Definition: isn.c:73
int16 attnum
Definition: pg_attribute.h:74
FormData_pg_index * Form_pg_index
Definition: pg_index.h:70
#define NIL
Definition: pg_list.h:68
List * RelationGetIndexPredicate(Relation relation)
Definition: relcache.c:5154
Form_pg_index rd_index
Definition: rel.h:192
Form_pg_class rd_rel
Definition: rel.h:111

References attnum, i, NIL, RelationData::rd_index, RelationData::rd_rel, and RelationGetIndexPredicate().

Referenced by refresh_by_match_merge(), and RefreshMatViewByOid().

◆ make_temptable_name_n()

static char * make_temptable_name_n ( char *  tempname,
int  n 
)
static

Definition at line 571 of file matview.c.

572 {
573  StringInfoData namebuf;
574 
575  initStringInfo(&namebuf);
576  appendStringInfoString(&namebuf, tempname);
577  appendStringInfo(&namebuf, "_%d", n);
578  return namebuf.data;
579 }
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References appendStringInfo(), appendStringInfoString(), StringInfoData::data, and initStringInfo().

Referenced by refresh_by_match_merge().

◆ MatViewIncrementalMaintenanceIsEnabled()

bool MatViewIncrementalMaintenanceIsEnabled ( void  )

Definition at line 970 of file matview.c.

971 {
972  return matview_maintenance_depth > 0;
973 }

References matview_maintenance_depth.

Referenced by CheckValidResultRel().

◆ OpenMatViewIncrementalMaintenance()

static void OpenMatViewIncrementalMaintenance ( void  )
static

Definition at line 976 of file matview.c.

977 {
979 }

References matview_maintenance_depth.

Referenced by refresh_by_match_merge().

◆ refresh_by_heap_swap()

static void refresh_by_heap_swap ( Oid  matviewOid,
Oid  OIDNewHeap,
char  relpersistence 
)
static

Definition at line 906 of file matview.c.

907 {
908  finish_heap_swap(matviewOid, OIDNewHeap, false, false, true, true,
909  RecentXmin, ReadNextMultiXactId(), relpersistence);
910 }
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:1438
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:771
TransactionId RecentXmin
Definition: snapmgr.c:99

References finish_heap_swap(), ReadNextMultiXactId(), and RecentXmin.

Referenced by RefreshMatViewByOid().

◆ refresh_by_match_merge()

static void refresh_by_match_merge ( Oid  matviewOid,
Oid  tempOid,
Oid  relowner,
int  save_sec_context 
)
static

Definition at line 614 of file matview.c.

616 {
617  StringInfoData querybuf;
618  Relation matviewRel;
619  Relation tempRel;
620  char *matviewname;
621  char *tempname;
622  char *diffname;
623  TupleDesc tupdesc;
624  bool foundUniqueIndex;
625  List *indexoidlist;
626  ListCell *indexoidscan;
627  int16 relnatts;
628  Oid *opUsedForQual;
629 
630  initStringInfo(&querybuf);
631  matviewRel = table_open(matviewOid, NoLock);
633  RelationGetRelationName(matviewRel));
634  tempRel = table_open(tempOid, NoLock);
636  RelationGetRelationName(tempRel));
637  diffname = make_temptable_name_n(tempname, 2);
638 
639  relnatts = RelationGetNumberOfAttributes(matviewRel);
640 
641  /* Open SPI context. */
642  SPI_connect();
643 
644  /* Analyze the temp table with the new contents. */
645  appendStringInfo(&querybuf, "ANALYZE %s", tempname);
646  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
647  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
648 
649  /*
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.
655  *
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().
659  */
660  resetStringInfo(&querybuf);
661  appendStringInfo(&querybuf,
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.<>) "
667  "newdata.ctid)",
668  tempname, tempname, tempname);
669  if (SPI_execute(querybuf.data, false, 1) != SPI_OK_SELECT)
670  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
671  if (SPI_processed > 0)
672  {
673  /*
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.
679  */
680  ereport(ERROR,
681  (errcode(ERRCODE_CARDINALITY_VIOLATION),
682  errmsg("new data for materialized view \"%s\" contains duplicate rows without any null columns",
683  RelationGetRelationName(matviewRel)),
684  errdetail("Row: %s",
686  }
687 
688  /*
689  * Create the temporary "diff" table.
690  *
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
694  * SRO context.
695  */
696  SetUserIdAndSecContext(relowner,
697  save_sec_context | SECURITY_LOCAL_USERID_CHANGE);
698  resetStringInfo(&querybuf);
699  appendStringInfo(&querybuf,
700  "CREATE TEMP TABLE %s (tid pg_catalog.tid)",
701  diffname);
702  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
703  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
704  SetUserIdAndSecContext(relowner,
705  save_sec_context | SECURITY_RESTRICTED_OPERATION);
706  resetStringInfo(&querybuf);
707  appendStringInfo(&querybuf,
708  "ALTER TABLE %s ADD COLUMN newdata %s",
709  diffname, tempname);
710  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
711  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
712 
713  /* Start building the query for populating the diff table. */
714  resetStringInfo(&querybuf);
715  appendStringInfo(&querybuf,
716  "INSERT INTO %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);
720 
721  /*
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
725  * include all rows.
726  */
727  tupdesc = matviewRel->rd_att;
728  opUsedForQual = (Oid *) palloc0(sizeof(Oid) * relnatts);
729  foundUniqueIndex = false;
730 
731  indexoidlist = RelationGetIndexList(matviewRel);
732 
733  foreach(indexoidscan, indexoidlist)
734  {
735  Oid indexoid = lfirst_oid(indexoidscan);
736  Relation indexRel;
737 
738  indexRel = index_open(indexoid, RowExclusiveLock);
739  if (is_usable_unique_index(indexRel))
740  {
741  Form_pg_index indexStruct = indexRel->rd_index;
742  int indnkeyatts = indexStruct->indnkeyatts;
743  oidvector *indclass;
744  Datum indclassDatum;
745  int i;
746 
747  /* Must get indclass the hard way. */
748  indclassDatum = SysCacheGetAttrNotNull(INDEXRELID,
749  indexRel->rd_indextuple,
750  Anum_pg_index_indclass);
751  indclass = (oidvector *) DatumGetPointer(indclassDatum);
752 
753  /* Add quals for all columns from this index. */
754  for (i = 0; i < indnkeyatts; i++)
755  {
756  int attnum = indexStruct->indkey.values[i];
757  Oid opclass = indclass->values[i];
758  Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum - 1);
759  Oid attrtype = attr->atttypid;
760  HeapTuple cla_ht;
761  Form_pg_opclass cla_tup;
762  Oid opfamily;
763  Oid opcintype;
764  Oid op;
765  const char *leftop;
766  const char *rightop;
767 
768  /*
769  * Identify the equality operator associated with this index
770  * column. First we need to look up the column's opclass.
771  */
772  cla_ht = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclass));
773  if (!HeapTupleIsValid(cla_ht))
774  elog(ERROR, "cache lookup failed for opclass %u", opclass);
775  cla_tup = (Form_pg_opclass) GETSTRUCT(cla_ht);
776  Assert(cla_tup->opcmethod == BTREE_AM_OID);
777  opfamily = cla_tup->opcfamily;
778  opcintype = cla_tup->opcintype;
779  ReleaseSysCache(cla_ht);
780 
781  op = get_opfamily_member(opfamily, opcintype, opcintype,
783  if (!OidIsValid(op))
784  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
785  BTEqualStrategyNumber, opcintype, opcintype, opfamily);
786 
787  /*
788  * If we find the same column with the same equality semantics
789  * in more than one index, we only need to emit the equality
790  * clause once.
791  *
792  * Since we only remember the last equality operator, this
793  * code could be fooled into emitting duplicate clauses given
794  * multiple indexes with several different opclasses ... but
795  * that's so unlikely it doesn't seem worth spending extra
796  * code to avoid.
797  */
798  if (opUsedForQual[attnum - 1] == op)
799  continue;
800  opUsedForQual[attnum - 1] = op;
801 
802  /*
803  * Actually add the qual, ANDed with any others.
804  */
805  if (foundUniqueIndex)
806  appendStringInfoString(&querybuf, " AND ");
807 
808  leftop = quote_qualified_identifier("newdata",
809  NameStr(attr->attname));
810  rightop = quote_qualified_identifier("mv",
811  NameStr(attr->attname));
812 
813  generate_operator_clause(&querybuf,
814  leftop, attrtype,
815  op,
816  rightop, attrtype);
817 
818  foundUniqueIndex = true;
819  }
820  }
821 
822  /* Keep the locks, since we're about to run DML which needs them. */
823  index_close(indexRel, NoLock);
824  }
825 
826  list_free(indexoidlist);
827 
828  /*
829  * There must be at least one usable unique index on the matview.
830  *
831  * ExecRefreshMatView() checks that after taking the exclusive lock on the
832  * matview. So at least one unique index is guaranteed to exist here
833  * because the lock is still being held. (One known exception is if a
834  * function called as part of refreshing the matview drops the index.
835  * That's a pretty silly thing to do.)
836  */
837  if (!foundUniqueIndex)
838  ereport(ERROR,
839  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
840  errmsg("could not find suitable unique index on materialized view"));
841 
842  appendStringInfoString(&querybuf,
843  " AND newdata.* OPERATOR(pg_catalog.*=) mv.*) "
844  "WHERE newdata.* IS NULL OR mv.* IS NULL "
845  "ORDER BY tid");
846 
847  /* Populate the temporary "diff" table. */
848  if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
849  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
850 
851  /*
852  * We have no further use for data from the "full-data" temp table, but we
853  * must keep it around because its type is referenced from the diff table.
854  */
855 
856  /* Analyze the diff table. */
857  resetStringInfo(&querybuf);
858  appendStringInfo(&querybuf, "ANALYZE %s", diffname);
859  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
860  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
861 
863 
864  /* Deletes must come before inserts; do them first. */
865  resetStringInfo(&querybuf);
866  appendStringInfo(&querybuf,
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);
872  if (SPI_exec(querybuf.data, 0) != SPI_OK_DELETE)
873  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
874 
875  /* Inserts go last. */
876  resetStringInfo(&querybuf);
877  appendStringInfo(&querybuf,
878  "INSERT INTO %s SELECT (diff.newdata).* "
879  "FROM %s diff WHERE tid IS NULL",
880  matviewname, diffname);
881  if (SPI_exec(querybuf.data, 0) != SPI_OK_INSERT)
882  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
883 
884  /* We're done maintaining the materialized view. */
886  table_close(tempRel, NoLock);
887  table_close(matviewRel, NoLock);
888 
889  /* Clean up temp tables. */
890  resetStringInfo(&querybuf);
891  appendStringInfo(&querybuf, "DROP TABLE %s, %s", diffname, tempname);
892  if (SPI_exec(querybuf.data, 0) != SPI_OK_UTILITY)
893  elog(ERROR, "SPI_exec failed: %s", querybuf.data);
894 
895  /* Close SPI context. */
896  if (SPI_finish() != SPI_OK_FINISH)
897  elog(ERROR, "SPI_finish failed");
898 }
#define NameStr(name)
Definition: c.h:737
signed short int16
Definition: c.h:495
#define OidIsValid(objectId)
Definition: c.h:766
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:177
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:133
void list_free(List *list)
Definition: list.c:1546
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3366
Oid get_opfamily_member(Oid opfamily, Oid lefttype, Oid righttype, int16 strategy)
Definition: lsyscache.c:166
static char * make_temptable_name_n(char *tempname, int n)
Definition: matview.c:571
static bool is_usable_unique_index(Relation indexRel)
Definition: matview.c:916
static void CloseMatViewIncrementalMaintenance(void)
Definition: matview.c:982
static void OpenMatViewIncrementalMaintenance(void)
Definition: matview.c:976
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:312
#define SECURITY_LOCAL_USERID_CHANGE
Definition: miscadmin.h:311
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:642
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
#define lfirst_oid(lc)
Definition: pg_list.h:174
FormData_pg_opclass * Form_pg_opclass
Definition: pg_opclass.h:83
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
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:511
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RelationGetNamespace(relation)
Definition: rel.h:546
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4804
char * quote_qualified_identifier(const char *qualifier, const char *ident)
Definition: ruleutils.c:12924
void generate_operator_clause(StringInfo buf, const char *leftop, Oid leftoptype, Oid opoid, const char *rightop, Oid rightoptype)
Definition: ruleutils.c:13250
uint64 SPI_processed
Definition: spi.c:44
SPITupleTable * SPI_tuptable
Definition: spi.c:45
int SPI_connect(void)
Definition: spi.c:94
int SPI_finish(void)
Definition: spi.c:182
int SPI_exec(const char *src, long tcount)
Definition: spi.c:630
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1220
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:596
#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
#define BTEqualStrategyNumber
Definition: stratnum.h:31
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:78
Definition: pg_list.h:54
struct HeapTupleData * rd_indextuple
Definition: rel.h:194
TupleDesc rd_att
Definition: rel.h:112
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26
Definition: c.h:717
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:724
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:627
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References appendStringInfo(), appendStringInfoString(), Assert, attnum, BTEqualStrategyNumber, CloseMatViewIncrementalMaintenance(), StringInfoData::data, DatumGetPointer(), elog, ereport, errcode(), errdetail(), errmsg(), ERROR, generate_operator_clause(), get_namespace_name(), get_opfamily_member(), GETSTRUCT, HeapTupleIsValid, i, index_close(), index_open(), initStringInfo(), is_usable_unique_index(), lfirst_oid, list_free(), make_temptable_name_n(), NameStr, NoLock, ObjectIdGetDatum(), OidIsValid, OpenMatViewIncrementalMaintenance(), palloc0(), quote_qualified_identifier(), RelationData::rd_att, RelationData::rd_index, RelationData::rd_indextuple, RelationGetIndexList(), RelationGetNamespace, RelationGetNumberOfAttributes, RelationGetRelationName, ReleaseSysCache(), resetStringInfo(), RowExclusiveLock, SearchSysCache1(), SECURITY_LOCAL_USERID_CHANGE, SECURITY_RESTRICTED_OPERATION, SetUserIdAndSecContext(), SPI_connect(), SPI_exec(), SPI_execute(), SPI_finish(), SPI_getvalue(), SPI_OK_DELETE, SPI_OK_FINISH, SPI_OK_INSERT, SPI_OK_SELECT, SPI_OK_UTILITY, SPI_processed, SPI_tuptable, SysCacheGetAttrNotNull(), table_close(), table_open(), SPITupleTable::tupdesc, TupleDescAttr, SPITupleTable::vals, and oidvector::values.

Referenced by RefreshMatViewByOid().

◆ refresh_matview_datafill()

static uint64 refresh_matview_datafill ( DestReceiver dest,
Query query,
const char *  queryString,
bool  is_create 
)
static

Definition at line 405 of file matview.c.

407 {
408  List *rewritten;
409  PlannedStmt *plan;
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, queryString,
443  dest, NULL, NULL, 0);
444 
445  /* call ExecutorStart to prepare the plan for execution */
446  ExecutorStart(queryDesc, 0);
447 
448  /* run the plan */
449  ExecutorRun(queryDesc, ForwardScanDirection, 0, true);
450 
451  processed = queryDesc->estate->es_processed;
452 
453  /* and clean up */
454  ExecutorFinish(queryDesc);
455  ExecutorEnd(queryDesc);
456 
457  FreeQueryDesc(queryDesc);
458 
460 
461  return processed;
462 }
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:465
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:405
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:120
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:295
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define copyObject(obj)
Definition: nodes.h:224
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3307
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define plan(x)
Definition: pg_regress.c:162
PlannedStmt * pg_plan_query(Query *querytree, const char *query_string, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:894
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
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
List * QueryRewrite(Query *parsetree)
@ ForwardScanDirection
Definition: sdir.h:28
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:712
void PopActiveSnapshot(void)
Definition: snapmgr.c:743
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:700
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:770
#define InvalidSnapshot
Definition: snapshot.h:123
uint64 es_processed
Definition: execnodes.h:679
EState * estate
Definition: execdesc.h:48

References AcquireRewriteLocks(), CHECK_FOR_INTERRUPTS, copyObject, CreateQueryDesc(), CURSOR_OPT_PARALLEL_OK, generate_unaccent_rules::dest, elog, ERROR, EState::es_processed, QueryDesc::estate, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), InvalidSnapshot, linitial, list_length(), pg_plan_query(), plan, PopActiveSnapshot(), PushCopiedSnapshot(), QueryRewrite(), and UpdateActiveSnapshotCommandId().

Referenced by RefreshMatViewByOid().

◆ RefreshMatViewByOid()

ObjectAddress RefreshMatViewByOid ( Oid  matviewOid,
bool  is_create,
bool  skipData,
bool  concurrent,
const char *  queryString,
QueryCompletion qc 
)

Definition at line 165 of file matview.c.

168 {
169  Relation matviewRel;
170  RewriteRule *rule;
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)
199  ereport(ERROR,
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))
206  ereport(ERROR,
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)
212  ereport(ERROR,
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)
271  ereport(ERROR,
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 
329  dest = CreateTransientRelDestReceiver(OIDNewHeap);
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 }
Oid GetDefaultTablespace(char relpersistence, bool partitioned)
Definition: tablespace.c:1143
Oid make_new_heap(Oid OIDOldHeap, Oid NewTableSpace, Oid NewAccessMethod, char relpersistence, LOCKMODE lockmode)
Definition: cluster.c:688
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
Definition: cmdtag.h:37
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define PG_RE_THROW()
Definition: elog.h:412
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
int NewGUCNestLevel(void)
Definition: guc.c:2234
void RestrictSearchPath(void)
Definition: guc.c:2245
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:2261
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108
#define AccessShareLock
Definition: lockdefs.h:36
static void refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner, int save_sec_context)
Definition: matview.c:614
DestReceiver * CreateTransientRelDestReceiver(Oid transientoid)
Definition: matview.c:465
static uint64 refresh_matview_datafill(DestReceiver *dest, Query *query, const char *queryString, bool is_create)
Definition: matview.c:405
void SetMatViewPopulatedState(Relation relation, bool newstate)
Definition: matview.c:79
static void refresh_by_heap_swap(Oid matviewOid, Oid OIDNewHeap, char relpersistence)
Definition: matview.c:906
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:635
@ CMD_SELECT
Definition: nodes.h:265
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
#define linitial_node(type, l)
Definition: pg_list.h:181
void pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
void pgstat_count_truncate(Relation rel)
#define RelationIsPopulated(relation)
Definition: rel.h:677
RuleLock * rd_rules
Definition: rel.h:115
RewriteRule ** rules
Definition: prs2lock.h:43
int numLocks
Definition: prs2lock.h:42
Definition: localtime.c:73
void CheckTableNotInUse(Relation rel, const char *stmt)
Definition: tablecmds.c:4276

References AccessExclusiveLock, AccessShareLock, Assert, AtEOXact_GUC(), CheckTableNotInUse(), CMD_SELECT, CreateTransientRelDestReceiver(), generate_unaccent_rules::dest, elog, ereport, errcode(), errhint(), errmsg(), ERROR, ExclusiveLock, get_namespace_name(), GetDefaultTablespace(), GetUserIdAndSecContext(), index_close(), index_open(), is_usable_unique_index(), lfirst_oid, linitial_node, list_free(), list_length(), LockRelationOid(), make_new_heap(), matview_maintenance_depth, NewGUCNestLevel(), NoLock, RuleLock::numLocks, ObjectAddressSet, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pgstat_count_heap_insert(), pgstat_count_truncate(), quote_qualified_identifier(), RelationData::rd_rel, RelationData::rd_rules, refresh_by_heap_swap(), refresh_by_match_merge(), refresh_matview_datafill(), RelationGetIndexList(), RelationGetNamespace, RelationGetRelationName, RelationIsPopulated, RestrictSearchPath(), RuleLock::rules, SECURITY_RESTRICTED_OPERATION, SetMatViewPopulatedState(), SetQueryCompletion(), SetUserIdAndSecContext(), table_close(), and table_open().

Referenced by ExecCreateTableAs(), and ExecRefreshMatView().

◆ SetMatViewPopulatedState()

void SetMatViewPopulatedState ( Relation  relation,
bool  newstate 
)

Definition at line 79 of file matview.c.

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 }
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
static struct state * newstate(struct nfa *nfa)
Definition: regc_nfa.c:137
#define RelationGetRelid(relation)
Definition: rel.h:505
ItemPointerData t_self
Definition: htup.h:65
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:91
void CommandCounterIncrement(void)
Definition: xact.c:1099

References Assert, CatalogTupleUpdate(), CommandCounterIncrement(), elog, ERROR, GETSTRUCT, heap_freetuple(), HeapTupleIsValid, newstate(), ObjectIdGetDatum(), RelationData::rd_rel, RelationGetRelid, RowExclusiveLock, SearchSysCacheCopy1, HeapTupleData::t_self, table_close(), and table_open().

Referenced by intorel_startup(), and RefreshMatViewByOid().

◆ transientrel_destroy()

static void transientrel_destroy ( DestReceiver self)
static

Definition at line 554 of file matview.c.

555 {
556  pfree(self);
557 }
void pfree(void *pointer)
Definition: mcxt.c:1521

References pfree().

Referenced by CreateTransientRelDestReceiver().

◆ transientrel_receive()

static bool transientrel_receive ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 509 of file matview.c.

510 {
511  DR_transientrel *myState = (DR_transientrel *) self;
512 
513  /*
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...
520  */
521 
523  slot,
524  myState->output_cid,
525  myState->ti_options,
526  myState->bistate);
527 
528  /* We know this is a newly created relation, so there are no indexes */
529 
530  return true;
531 }
Relation transientrel
Definition: matview.c:50
BulkInsertState bistate
Definition: matview.c:53
CommandId output_cid
Definition: matview.c:51
int ti_options
Definition: matview.c:52
static void table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid, int options, struct BulkInsertStateData *bistate)
Definition: tableam.h:1402

References DR_transientrel::bistate, DR_transientrel::output_cid, table_tuple_insert(), DR_transientrel::ti_options, and DR_transientrel::transientrel.

Referenced by CreateTransientRelDestReceiver().

◆ transientrel_shutdown()

static void transientrel_shutdown ( DestReceiver self)
static

Definition at line 537 of file matview.c.

538 {
539  DR_transientrel *myState = (DR_transientrel *) self;
540 
541  FreeBulkInsertState(myState->bistate);
542 
544 
545  /* close transientrel, but keep lock until commit */
546  table_close(myState->transientrel, NoLock);
547  myState->transientrel = NULL;
548 }
void FreeBulkInsertState(BulkInsertState bistate)
Definition: heapam.c:1935
static void table_finish_bulk_insert(Relation rel, int options)
Definition: tableam.h:1595

References DR_transientrel::bistate, FreeBulkInsertState(), NoLock, table_close(), table_finish_bulk_insert(), DR_transientrel::ti_options, and DR_transientrel::transientrel.

Referenced by CreateTransientRelDestReceiver().

◆ transientrel_startup()

static void transientrel_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

Definition at line 483 of file matview.c.

484 {
485  DR_transientrel *myState = (DR_transientrel *) self;
486  Relation transientrel;
487 
488  transientrel = table_open(myState->transientoid, NoLock);
489 
490  /*
491  * Fill private fields of myState for use by later routines
492  */
493  myState->transientrel = transientrel;
494  myState->output_cid = GetCurrentCommandId(true);
496  myState->bistate = GetBulkInsertState();
497 
498  /*
499  * Valid smgr_targblock implies something already wrote to the relation.
500  * This may be harmless, but this function hasn't planned for it.
501  */
503 }
#define InvalidBlockNumber
Definition: block.h:33
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:1918
#define RelationGetTargetBlock(relation)
Definition: rel.h:601
Oid transientoid
Definition: matview.c:48
#define TABLE_INSERT_FROZEN
Definition: tableam.h:261
#define TABLE_INSERT_SKIP_FSM
Definition: tableam.h:260
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:828

References Assert, DR_transientrel::bistate, GetBulkInsertState(), GetCurrentCommandId(), InvalidBlockNumber, NoLock, DR_transientrel::output_cid, RelationGetTargetBlock, TABLE_INSERT_FROZEN, TABLE_INSERT_SKIP_FSM, table_open(), DR_transientrel::ti_options, DR_transientrel::transientoid, and DR_transientrel::transientrel.

Referenced by CreateTransientRelDestReceiver().

Variable Documentation

◆ matview_maintenance_depth

int matview_maintenance_depth = 0
static