PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
spginsert.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/spgist_private.h"
#include "access/spgxlog.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, HeapTuple htup, 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

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

Definition at line 70 of file spginsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, BufferGetBlockNumber(), BufferGetPage, CurrentMemoryContext, elog, END_CRIT_SECTION, ERROR, IndexBuildResult::heap_tuples, IndexBuildResult::index_tuples, IndexBuildHeapScan(), initSpGistState(), SpGistState::isBuild, MarkBufferDirty(), MemoryContextDelete(), PageSetLSN, palloc0(), REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetNumberOfBlocks, RelationGetRelationName, RelationNeedsWAL, result, SPGIST_LEAF, SPGIST_METAPAGE_BLKNO, SPGIST_NULL_BLKNO, SPGIST_NULLS, SPGIST_ROOT_BLKNO, spgistBuildCallback(), SpGistInitBuffer(), SpGistInitMetapage(), SpGistNewBuffer(), SpGistUpdateMetaPage(), SpGistBuildState::spgstate, START_CRIT_SECTION, SpGistBuildState::tmpCtx, UnlockReleaseBuffer(), XLOG_SPGIST_CREATE_INDEX, XLogBeginInsert(), XLogInsert(), and XLogRegisterBuffer().

Referenced by spghandler().

71 {
73  double reltuples;
74  SpGistBuildState buildstate;
75  Buffer metabuffer,
76  rootbuffer,
77  nullbuffer;
78 
79  if (RelationGetNumberOfBlocks(index) != 0)
80  elog(ERROR, "index \"%s\" already contains data",
82 
83  /*
84  * Initialize the meta page and root pages
85  */
86  metabuffer = SpGistNewBuffer(index);
87  rootbuffer = SpGistNewBuffer(index);
88  nullbuffer = SpGistNewBuffer(index);
89 
93 
95 
96  SpGistInitMetapage(BufferGetPage(metabuffer));
97  MarkBufferDirty(metabuffer);
98  SpGistInitBuffer(rootbuffer, SPGIST_LEAF);
99  MarkBufferDirty(rootbuffer);
101  MarkBufferDirty(nullbuffer);
102 
103  if (RelationNeedsWAL(index))
104  {
105  XLogRecPtr recptr;
106 
107  XLogBeginInsert();
108 
109  /*
110  * Replay will re-initialize the pages, so don't take full pages
111  * images. No other data to log.
112  */
113  XLogRegisterBuffer(0, metabuffer, REGBUF_WILL_INIT);
116 
117  recptr = XLogInsert(RM_SPGIST_ID, XLOG_SPGIST_CREATE_INDEX);
118 
119  PageSetLSN(BufferGetPage(metabuffer), recptr);
120  PageSetLSN(BufferGetPage(rootbuffer), recptr);
121  PageSetLSN(BufferGetPage(nullbuffer), recptr);
122  }
123 
125 
126  UnlockReleaseBuffer(metabuffer);
127  UnlockReleaseBuffer(rootbuffer);
128  UnlockReleaseBuffer(nullbuffer);
129 
130  /*
131  * Now insert all the heap data into the index
132  */
133  initSpGistState(&buildstate.spgstate, index);
134  buildstate.spgstate.isBuild = true;
135 
137  "SP-GiST build temporary context",
139 
140  reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
141  spgistBuildCallback, (void *) &buildstate);
142 
143  MemoryContextDelete(buildstate.tmpCtx);
144 
145  SpGistUpdateMetaPage(index);
146 
147  result = (IndexBuildResult *) palloc0(sizeof(IndexBuildResult));
148  result->heap_tuples = result->index_tuples = reltuples;
149 
150  return result;
151 }
void SpGistUpdateMetaPage(Relation index)
Definition: spgutils.c:252
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
#define END_CRIT_SECTION()
Definition: miscadmin.h:135
#define SPGIST_NULLS
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:133
return result
Definition: formatting.c:1633
SpGistState spgstate
Definition: spginsert.c:34
#define SPGIST_ROOT_BLKNO
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
#define XLOG_SPGIST_CREATE_INDEX
Definition: spgxlog.h:21
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
#define SPGIST_NULL_BLKNO
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define SPGIST_METAPAGE_BLKNO
void initSpGistState(SpGistState *state, Relation index)
Definition: spgutils.c:158
#define RelationGetRelationName(relation)
Definition: rel.h:436
static void spgistBuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: spginsert.c:41
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
void SpGistInitBuffer(Buffer b, uint16 f)
Definition: spgutils.c:514
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void * palloc0(Size size)
Definition: mcxt.c:878
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
Buffer SpGistNewBuffer(Relation index)
Definition: spgutils.c:187
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
void SpGistInitMetapage(Page page)
Definition: spgutils.c:524
#define RelationNeedsWAL(relation)
Definition: rel.h:505
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, IndexBuildCallback callback, void *callback_state)
Definition: index.c:2169
MemoryContext tmpCtx
Definition: spginsert.c:35
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define SPGIST_LEAF
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
int Buffer
Definition: buf.h:23
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32
void spgbuildempty ( Relation  index)

Definition at line 157 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().

158 {
159  Page page;
160 
161  /* Construct metapage. */
162  page = (Page) palloc(BLCKSZ);
163  SpGistInitMetapage(page);
164 
165  /*
166  * Write the page and log it unconditionally. This is important
167  * particularly for indexes created on tablespaces and databases whose
168  * creation happened after the last redo pointer as recovery removes any
169  * of their existing content when the corresponding create records are
170  * replayed.
171  */
174  (char *) page, true);
176  SPGIST_METAPAGE_BLKNO, page, false);
177 
178  /* Likewise for the root page. */
180 
183  (char *) page, true);
185  SPGIST_ROOT_BLKNO, page, true);
186 
187  /* Likewise for the null-tuples root page. */
189 
192  (char *) page, true);
194  SPGIST_NULL_BLKNO, page, true);
195 
196  /*
197  * An immediate sync is required even if we xlog'd the pages, because the
198  * writes did not go through shared buffers and therefore a concurrent
199  * checkpoint may have moved the redo pointer past our xlog record.
200  */
202 }
struct SMgrRelationData * rd_smgr
Definition: rel.h:87
#define SPGIST_NULLS
#define SPGIST_ROOT_BLKNO
RelFileNodeBackend smgr_rnode
Definition: smgr.h:43
#define SPGIST_NULL_BLKNO
void smgrwrite(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:647
#define SPGIST_METAPAGE_BLKNO
RelFileNode node
Definition: relfilenode.h:74
void SpGistInitMetapage(Page page)
Definition: spgutils.c:524
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1199
void SpGistInitPage(Page page, uint16 f)
Definition: spgutils.c:499
void * palloc(Size size)
Definition: mcxt.c:849
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:734
Pointer Page
Definition: bufpage.h:74
bool spginsert ( Relation  index,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
IndexInfo indexInfo 
)

Definition at line 208 of file spginsert.c.

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

Referenced by spghandler().

212 {
213  SpGistState spgstate;
214  MemoryContext oldCtx;
215  MemoryContext insertCtx;
216 
218  "SP-GiST insert temporary context",
220  oldCtx = MemoryContextSwitchTo(insertCtx);
221 
222  initSpGistState(&spgstate, index);
223 
224  /*
225  * We might have to repeat spgdoinsert() multiple times, if conflicts
226  * occur with concurrent insertions. If so, reset the insertCtx each time
227  * to avoid cumulative memory consumption. That means we also have to
228  * redo initSpGistState(), but it's cheap enough not to matter.
229  */
230  while (!spgdoinsert(index, &spgstate, ht_ctid, *values, *isnull))
231  {
232  MemoryContextReset(insertCtx);
233  initSpGistState(&spgstate, index);
234  }
235 
236  SpGistUpdateMetaPage(index);
237 
238  MemoryContextSwitchTo(oldCtx);
239  MemoryContextDelete(insertCtx);
240 
241  /* return false since we've not done any unique check */
242  return false;
243 }
void SpGistUpdateMetaPage(Relation index)
Definition: spgutils.c:252
bool spgdoinsert(Relation index, SpGistState *state, ItemPointer heapPtr, Datum datum, bool isnull)
Definition: spgdoinsert.c:1891
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
void initSpGistState(SpGistState *state, Relation index)
Definition: spgutils.c:158
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
static Datum values[MAXATTR]
Definition: bootstrap.c:163
static void spgistBuildCallback ( Relation  index,
HeapTuple  htup,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 41 of file spginsert.c.

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

Referenced by spgbuild().

43 {
44  SpGistBuildState *buildstate = (SpGistBuildState *) state;
45  MemoryContext oldCtx;
46 
47  /* Work in temp context, and reset it after each tuple */
48  oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
49 
50  /*
51  * Even though no concurrent insertions can be happening, we still might
52  * get a buffer-locking failure due to bgwriter or checkpointer taking a
53  * lock on some buffer. So we need to be willing to retry. We can flush
54  * any temp data when retrying.
55  */
56  while (!spgdoinsert(index, &buildstate->spgstate, &htup->t_self,
57  *values, *isnull))
58  {
59  MemoryContextReset(buildstate->tmpCtx);
60  }
61 
62  MemoryContextSwitchTo(oldCtx);
63  MemoryContextReset(buildstate->tmpCtx);
64 }
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:135
SpGistState spgstate
Definition: spginsert.c:34
ItemPointerData t_self
Definition: htup.h:65
Definition: regguts.h:298
static Datum values[MAXATTR]
Definition: bootstrap.c:163
MemoryContext tmpCtx
Definition: spginsert.c:35