PostgreSQL Source Code  git master
spginsert.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * spginsert.c
4  * Externally visible index creation/insertion routines
5  *
6  * All the actual insertion logic is in spgdoinsert.c.
7  *
8  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  * IDENTIFICATION
12  * src/backend/access/spgist/spginsert.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 #include "postgres.h"
18 
19 #include "access/genam.h"
20 #include "access/spgist_private.h"
21 #include "access/spgxlog.h"
22 #include "access/xlog.h"
23 #include "access/xloginsert.h"
24 #include "catalog/index.h"
25 #include "miscadmin.h"
26 #include "storage/bufmgr.h"
27 #include "storage/smgr.h"
28 #include "utils/memutils.h"
29 #include "utils/rel.h"
30 
31 
32 typedef struct
33 {
34  SpGistState spgstate; /* SPGiST's working state */
35  int64 indtuples; /* total number of tuples indexed */
36  MemoryContext tmpCtx; /* per-tuple temporary context */
38 
39 
40 /* Callback to process one heap tuple during IndexBuildHeapScan */
41 static void
43  bool *isnull, bool tupleIsAlive, void *state)
44 {
45  SpGistBuildState *buildstate = (SpGistBuildState *) state;
46  MemoryContext oldCtx;
47 
48  /* Work in temp context, and reset it after each tuple */
49  oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
50 
51  /*
52  * Even though no concurrent insertions can be happening, we still might
53  * get a buffer-locking failure due to bgwriter or checkpointer taking a
54  * lock on some buffer. So we need to be willing to retry. We can flush
55  * any temp data when retrying.
56  */
57  while (!spgdoinsert(index, &buildstate->spgstate, &htup->t_self,
58  *values, *isnull))
59  {
60  MemoryContextReset(buildstate->tmpCtx);
61  }
62 
63  /* Update total tuple count */
64  buildstate->indtuples += 1;
65 
66  MemoryContextSwitchTo(oldCtx);
67  MemoryContextReset(buildstate->tmpCtx);
68 }
69 
70 /*
71  * Build an SP-GiST index.
72  */
75 {
76  IndexBuildResult *result;
77  double reltuples;
78  SpGistBuildState buildstate;
79  Buffer metabuffer,
80  rootbuffer,
81  nullbuffer;
82 
83  if (RelationGetNumberOfBlocks(index) != 0)
84  elog(ERROR, "index \"%s\" already contains data",
86 
87  /*
88  * Initialize the meta page and root pages
89  */
90  metabuffer = SpGistNewBuffer(index);
91  rootbuffer = SpGistNewBuffer(index);
92  nullbuffer = SpGistNewBuffer(index);
93 
97 
99 
100  SpGistInitMetapage(BufferGetPage(metabuffer));
101  MarkBufferDirty(metabuffer);
102  SpGistInitBuffer(rootbuffer, SPGIST_LEAF);
103  MarkBufferDirty(rootbuffer);
105  MarkBufferDirty(nullbuffer);
106 
107  if (RelationNeedsWAL(index))
108  {
109  XLogRecPtr recptr;
110 
111  XLogBeginInsert();
112 
113  /*
114  * Replay will re-initialize the pages, so don't take full pages
115  * images. No other data to log.
116  */
120 
121  recptr = XLogInsert(RM_SPGIST_ID, XLOG_SPGIST_CREATE_INDEX);
122 
123  PageSetLSN(BufferGetPage(metabuffer), recptr);
124  PageSetLSN(BufferGetPage(rootbuffer), recptr);
125  PageSetLSN(BufferGetPage(nullbuffer), recptr);
126  }
127 
129 
130  UnlockReleaseBuffer(metabuffer);
131  UnlockReleaseBuffer(rootbuffer);
132  UnlockReleaseBuffer(nullbuffer);
133 
134  /*
135  * Now insert all the heap data into the index
136  */
137  initSpGistState(&buildstate.spgstate, index);
138  buildstate.spgstate.isBuild = true;
139  buildstate.indtuples = 0;
140 
142  "SP-GiST build temporary context",
144 
145  reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
146  spgistBuildCallback, (void *) &buildstate,
147  NULL);
148 
149  MemoryContextDelete(buildstate.tmpCtx);
150 
151  SpGistUpdateMetaPage(index);
152 
153  result = (IndexBuildResult *) palloc0(sizeof(IndexBuildResult));
154  result->heap_tuples = reltuples;
155  result->index_tuples = buildstate.indtuples;
156 
157  return result;
158 }
159 
160 /*
161  * Build an empty SPGiST index in the initialization fork
162  */
163 void
165 {
166  Page page;
167 
168  /* Construct metapage. */
169  page = (Page) palloc(BLCKSZ);
170  SpGistInitMetapage(page);
171 
172  /*
173  * Write the page and log it unconditionally. This is important
174  * particularly for indexes created on tablespaces and databases whose
175  * creation happened after the last redo pointer as recovery removes any
176  * of their existing content when the corresponding create records are
177  * replayed.
178  */
181  (char *) page, true);
183  SPGIST_METAPAGE_BLKNO, page, true);
184 
185  /* Likewise for the root page. */
187 
190  (char *) page, true);
192  SPGIST_ROOT_BLKNO, page, true);
193 
194  /* Likewise for the null-tuples root page. */
196 
199  (char *) page, true);
201  SPGIST_NULL_BLKNO, page, true);
202 
203  /*
204  * An immediate sync is required even if we xlog'd the pages, because the
205  * writes did not go through shared buffers and therefore a concurrent
206  * checkpoint may have moved the redo pointer past our xlog record.
207  */
209 }
210 
211 /*
212  * Insert one new tuple into an SPGiST index.
213  */
214 bool
216  ItemPointer ht_ctid, Relation heapRel,
217  IndexUniqueCheck checkUnique,
218  IndexInfo *indexInfo)
219 {
220  SpGistState spgstate;
221  MemoryContext oldCtx;
222  MemoryContext insertCtx;
223 
225  "SP-GiST insert temporary context",
227  oldCtx = MemoryContextSwitchTo(insertCtx);
228 
229  initSpGistState(&spgstate, index);
230 
231  /*
232  * We might have to repeat spgdoinsert() multiple times, if conflicts
233  * occur with concurrent insertions. If so, reset the insertCtx each time
234  * to avoid cumulative memory consumption. That means we also have to
235  * redo initSpGistState(), but it's cheap enough not to matter.
236  */
237  while (!spgdoinsert(index, &spgstate, ht_ctid, *values, *isnull))
238  {
239  MemoryContextReset(insertCtx);
240  initSpGistState(&spgstate, index);
241  }
242 
243  SpGistUpdateMetaPage(index);
244 
245  MemoryContextSwitchTo(oldCtx);
246  MemoryContextDelete(insertCtx);
247 
248  /* return false since we've not done any unique check */
249  return false;
250 }
void SpGistUpdateMetaPage(Relation index)
Definition: spgutils.c:270
bool spgdoinsert(Relation index, SpGistState *state, ItemPointer heapPtr, Datum datum, bool isnull)
Definition: spgdoinsert.c:1891
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
#define SPGIST_NULLS
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define REGBUF_WILL_INIT
Definition: xloginsert.h:32
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
bool spginsert(Relation index, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: spginsert.c:215
SpGistState spgstate
Definition: spginsert.c:34
#define SPGIST_ROOT_BLKNO
Definition: type.h:89
IndexUniqueCheck
Definition: genam.h:111
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3332
#define ERROR
Definition: elog.h:43
#define XLOG_SPGIST_CREATE_INDEX
Definition: spgxlog.h:21
double IndexBuildHeapScan(Relation heapRelation, Relation indexRelation, IndexInfo *indexInfo, bool allow_sync, IndexBuildCallback callback, void *callback_state, HeapScanDesc scan)
Definition: index.c:2418
ItemPointerData t_self
Definition: htup.h:65
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
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 REGBUF_STANDARD
Definition: xloginsert.h:34
#define SPGIST_METAPAGE_BLKNO
void initSpGistState(SpGistState *state, Relation index)
Definition: spgutils.c:175
#define RelationGetRelationName(relation)
Definition: rel.h:441
static void spgistBuildCallback(Relation index, HeapTuple htup, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: spginsert.c:42
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void spgbuildempty(Relation index)
Definition: spginsert.c:164
void SpGistInitBuffer(Buffer b, uint16 f)
Definition: spgutils.c:544
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
void * palloc0(Size size)
Definition: mcxt.c:955
uintptr_t Datum
Definition: postgres.h:367
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199
Buffer SpGistNewBuffer(Relation index)
Definition: spgutils.c:205
RelFileNode node
Definition: relfilenode.h:74
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
Definition: regguts.h:298
void SpGistInitMetapage(Page page)
Definition: spgutils.c:554
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1195
#define RelationNeedsWAL(relation)
Definition: rel.h:510
IndexBuildResult * spgbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: spginsert.c:74
static Datum values[MAXATTR]
Definition: bootstrap.c:164
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
void SpGistInitPage(Page page, uint16 f)
Definition: spgutils.c:529
void * palloc(Size size)
Definition: mcxt.c:924
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:972
MemoryContext tmpCtx
Definition: spginsert.c:36
#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
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:734
float4 reltuples
Definition: pg_class.h:44
Pointer Page
Definition: bufpage.h:74
double index_tuples
Definition: genam.h:33
double heap_tuples
Definition: genam.h:32