PostgreSQL Source Code  git master
spginsert.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/tableam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/smgr.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for spginsert.c:

Go to the source code of this file.

Data Structures

struct  SpGistBuildState
 

Functions

static void spgistBuildCallback (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
 
IndexBuildResultspgbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void spgbuildempty (Relation index)
 
bool spginsert (Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
 

Function Documentation

◆ spgbuild()

IndexBuildResult* spgbuild ( Relation  heap,
Relation  index,
IndexInfo indexInfo 
)

Definition at line 75 of file spginsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, BufferGetBlockNumber(), BufferGetPage, CurrentMemoryContext, elog, END_CRIT_SECTION, ERROR, IndexBuildResult::heap_tuples, IndexBuildResult::index_tuples, SpGistBuildState::indtuples, initSpGistState(), SpGistState::isBuild, log_newpage_range(), MAIN_FORKNUM, MarkBufferDirty(), MemoryContextDelete(), palloc0(), RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, reltuples, SPGIST_LEAF, SPGIST_METAPAGE_BLKNO, SPGIST_NULL_BLKNO, SPGIST_NULLS, SPGIST_ROOT_BLKNO, spgistBuildCallback(), SpGistInitBuffer(), SpGistInitMetapage(), SpGistNewBuffer(), SpGistUpdateMetaPage(), SpGistBuildState::spgstate, START_CRIT_SECTION, table_index_build_scan(), SpGistBuildState::tmpCtx, and UnlockReleaseBuffer().

Referenced by spghandler().

76 {
77  IndexBuildResult *result;
78  double reltuples;
79  SpGistBuildState buildstate;
80  Buffer metabuffer,
81  rootbuffer,
82  nullbuffer;
83 
84  if (RelationGetNumberOfBlocks(index) != 0)
85  elog(ERROR, "index \"%s\" already contains data",
87 
88  /*
89  * Initialize the meta page and root pages
90  */
91  metabuffer = SpGistNewBuffer(index);
92  rootbuffer = SpGistNewBuffer(index);
93  nullbuffer = SpGistNewBuffer(index);
94 
98 
100 
101  SpGistInitMetapage(BufferGetPage(metabuffer));
102  MarkBufferDirty(metabuffer);
103  SpGistInitBuffer(rootbuffer, SPGIST_LEAF);
104  MarkBufferDirty(rootbuffer);
106  MarkBufferDirty(nullbuffer);
107 
108 
110 
111  UnlockReleaseBuffer(metabuffer);
112  UnlockReleaseBuffer(rootbuffer);
113  UnlockReleaseBuffer(nullbuffer);
114 
115  /*
116  * Now insert all the heap data into the index
117  */
118  initSpGistState(&buildstate.spgstate, index);
119  buildstate.spgstate.isBuild = true;
120  buildstate.indtuples = 0;
121 
123  "SP-GiST build temporary context",
125 
126  reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
127  spgistBuildCallback, (void *) &buildstate,
128  NULL);
129 
130  MemoryContextDelete(buildstate.tmpCtx);
131 
132  SpGistUpdateMetaPage(index);
133 
134  /*
135  * We didn't write WAL records as we built the index, so if WAL-logging is
136  * required, write all pages to the WAL now.
137  */
138  if (RelationNeedsWAL(index))
139  {
141  0, RelationGetNumberOfBlocks(index),
142  true);
143  }
144 
145  result = (IndexBuildResult *) palloc0(sizeof(IndexBuildResult));
146  result->heap_tuples = reltuples;
147  result->index_tuples = buildstate.indtuples;
148 
149  return result;
150 }
static void spgistBuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: spginsert.c:43
void SpGistUpdateMetaPage(Relation index)
Definition: spgutils.c:276
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1458
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define SPGIST_NULLS
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
SpGistState spgstate
Definition: spginsert.c:35
#define SPGIST_ROOT_BLKNO
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3388
#define ERROR
Definition: elog.h:43
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1499
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define SPGIST_NULL_BLKNO
#define SPGIST_METAPAGE_BLKNO
void initSpGistState(SpGistState *state, Relation index)
Definition: spgutils.c:181
#define RelationGetRelationName(relation)
Definition: rel.h:456
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void SpGistInitBuffer(Buffer b, uint16 f)
Definition: spgutils.c:549
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
void * palloc0(Size size)
Definition: mcxt.c:980
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:198
Buffer SpGistNewBuffer(Relation index)
Definition: spgutils.c:211
#define Assert(condition)
Definition: c.h:739
void SpGistInitMetapage(Page page)
Definition: spgutils.c:559
#define RelationNeedsWAL(relation)
Definition: rel.h:524
void log_newpage_range(Relation rel, ForkNumber forkNum, BlockNumber startblk, BlockNumber endblk, bool page_std)
Definition: xloginsert.c:1042
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2613
#define elog(elevel,...)
Definition: elog.h:228
MemoryContext tmpCtx
Definition: spginsert.c:37
#define SPGIST_LEAF
int Buffer
Definition: buf.h:23
float4 reltuples
Definition: pg_class.h:63
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32

◆ spgbuildempty()

void spgbuildempty ( Relation  index)

Definition at line 156 of file spginsert.c.

References INIT_FORKNUM, log_newpage(), RelFileNodeBackend::node, PageSetChecksumInplace(), palloc(), RelationData::rd_smgr, SMgrRelationData::smgr_rnode, smgrimmedsync(), smgrwrite(), SPGIST_LEAF, SPGIST_METAPAGE_BLKNO, SPGIST_NULL_BLKNO, SPGIST_NULLS, SPGIST_ROOT_BLKNO, SpGistInitMetapage(), and SpGistInitPage().

Referenced by spghandler().

157 {
158  Page page;
159 
160  /* Construct metapage. */
161  page = (Page) palloc(BLCKSZ);
162  SpGistInitMetapage(page);
163 
164  /*
165  * Write the page and log it unconditionally. This is important
166  * particularly for indexes created on tablespaces and databases whose
167  * creation happened after the last redo pointer as recovery removes any
168  * of their existing content when the corresponding create records are
169  * replayed.
170  */
173  (char *) page, true);
175  SPGIST_METAPAGE_BLKNO, page, true);
176 
177  /* Likewise for the root page. */
179 
182  (char *) page, true);
184  SPGIST_ROOT_BLKNO, page, true);
185 
186  /* Likewise for the null-tuples root page. */
188 
191  (char *) page, true);
193  SPGIST_NULL_BLKNO, page, true);
194 
195  /*
196  * An immediate sync is required even if we xlog'd the pages, because the
197  * writes did not go through shared buffers and therefore a concurrent
198  * checkpoint may have moved the redo pointer past our xlog record.
199  */
201 }
struct SMgrRelationData * rd_smgr
Definition: rel.h:56
#define SPGIST_NULLS
#define SPGIST_ROOT_BLKNO
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
#define SPGIST_NULL_BLKNO
void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:530
#define SPGIST_METAPAGE_BLKNO
RelFileNode node
Definition: relfilenode.h:74
void SpGistInitMetapage(Page page)
Definition: spgutils.c:559
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1198
void SpGistInitPage(Page page, uint16 f)
Definition: spgutils.c:534
void * palloc(Size size)
Definition: mcxt.c:949
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:972
#define SPGIST_LEAF
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:637
Pointer Page
Definition: bufpage.h:78

◆ spginsert()

bool spginsert ( Relation  index,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
IndexInfo indexInfo 
)

Definition at line 207 of file spginsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CurrentMemoryContext, initSpGistState(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), spgdoinsert(), and SpGistUpdateMetaPage().

Referenced by spghandler().

211 {
212  SpGistState spgstate;
213  MemoryContext oldCtx;
214  MemoryContext insertCtx;
215 
217  "SP-GiST insert temporary context",
219  oldCtx = MemoryContextSwitchTo(insertCtx);
220 
221  initSpGistState(&spgstate, index);
222 
223  /*
224  * We might have to repeat spgdoinsert() multiple times, if conflicts
225  * occur with concurrent insertions. If so, reset the insertCtx each time
226  * to avoid cumulative memory consumption. That means we also have to
227  * redo initSpGistState(), but it's cheap enough not to matter.
228  */
229  while (!spgdoinsert(index, &spgstate, ht_ctid, *values, *isnull))
230  {
231  MemoryContextReset(insertCtx);
232  initSpGistState(&spgstate, index);
233  }
234 
235  SpGistUpdateMetaPage(index);
236 
237  MemoryContextSwitchTo(oldCtx);
238  MemoryContextDelete(insertCtx);
239 
240  /* return false since we've not done any unique check */
241  return false;
242 }
void SpGistUpdateMetaPage(Relation index)
Definition: spgutils.c:276
bool spgdoinsert(Relation index, SpGistState *state, ItemPointer heapPtr, Datum datum, bool isnull)
Definition: spgdoinsert.c:1891
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void initSpGistState(SpGistState *state, Relation index)
Definition: spgutils.c:181
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static Datum values[MAXATTR]
Definition: bootstrap.c:167

◆ spgistBuildCallback()

static void spgistBuildCallback ( Relation  index,
ItemPointer  tid,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 43 of file spginsert.c.

References SpGistBuildState::indtuples, MemoryContextReset(), MemoryContextSwitchTo(), spgdoinsert(), SpGistBuildState::spgstate, and SpGistBuildState::tmpCtx.

Referenced by spgbuild().

45 {
46  SpGistBuildState *buildstate = (SpGistBuildState *) state;
47  MemoryContext oldCtx;
48 
49  /* Work in temp context, and reset it after each tuple */
50  oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
51 
52  /*
53  * Even though no concurrent insertions can be happening, we still might
54  * get a buffer-locking failure due to bgwriter or checkpointer taking a
55  * lock on some buffer. So we need to be willing to retry. We can flush
56  * any temp data when retrying.
57  */
58  while (!spgdoinsert(index, &buildstate->spgstate, tid,
59  *values, *isnull))
60  {
61  MemoryContextReset(buildstate->tmpCtx);
62  }
63 
64  /* Update total tuple count */
65  buildstate->indtuples += 1;
66 
67  MemoryContextSwitchTo(oldCtx);
68  MemoryContextReset(buildstate->tmpCtx);
69 }
bool spgdoinsert(Relation index, SpGistState *state, ItemPointer heapPtr, Datum datum, bool isnull)
Definition: spgdoinsert.c:1891
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
SpGistState spgstate
Definition: spginsert.c:35
Definition: regguts.h:298
static Datum values[MAXATTR]
Definition: bootstrap.c:167
MemoryContext tmpCtx
Definition: spginsert.c:37