Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * ginbulk.c
4 : : * routines for fast build of inverted index
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 : : * Portions Copyright (c) 1994, Regents of the University of California
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/access/gin/ginbulk.c
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include <limits.h>
18 : :
19 : : #include "access/gin_private.h"
20 : : #include "utils/datum.h"
21 : : #include "utils/memutils.h"
22 : :
23 : :
24 : : #define DEF_NENTRY 2048 /* GinEntryAccumulator allocation quantum */
25 : : #define DEF_NPTR 5 /* ItemPointer initial allocation quantum */
26 : :
27 : :
28 : : /* Combiner function for rbtree.c */
29 : : static void
2548 tgl@sss.pgh.pa.us 30 :CBC 1265165 : ginCombineData(RBTNode *existing, const RBTNode *newdata, void *arg)
31 : : {
5408 32 : 1265165 : GinEntryAccumulator *eo = (GinEntryAccumulator *) existing;
33 : 1265165 : const GinEntryAccumulator *en = (const GinEntryAccumulator *) newdata;
5723 bruce@momjian.us 34 : 1265165 : BuildAccumulator *accum = (BuildAccumulator *) arg;
35 : :
36 : : /*
37 : : * Note this code assumes that newdata contains only one itempointer.
38 : : */
5408 tgl@sss.pgh.pa.us 39 [ + + ]: 1265165 : if (eo->count >= eo->maxcount)
40 : : {
3709 teodor@sigaev.ru 41 [ - + ]: 44959 : if (eo->maxcount > INT_MAX)
3709 teodor@sigaev.ru 42 [ # # ]:UBC 0 : ereport(ERROR,
43 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
44 : : errmsg("posting list is too long"),
45 : : errhint("Reduce \"maintenance_work_mem\".")));
46 : :
5738 teodor@sigaev.ru 47 :CBC 44959 : accum->allocatedMemory -= GetMemoryChunkSpace(eo->list);
5408 tgl@sss.pgh.pa.us 48 : 44959 : eo->maxcount *= 2;
49 : 44959 : eo->list = (ItemPointerData *)
3709 teodor@sigaev.ru 50 : 44959 : repalloc_huge(eo->list, sizeof(ItemPointerData) * eo->maxcount);
5738 51 : 44959 : accum->allocatedMemory += GetMemoryChunkSpace(eo->list);
52 : : }
53 : :
54 : : /* If item pointers are not ordered, they will need to be sorted later */
2995 peter_e@gmx.net 55 [ + - ]: 1265165 : if (eo->shouldSort == false)
56 : : {
57 : : int res;
58 : :
5408 tgl@sss.pgh.pa.us 59 : 1265165 : res = ginCompareItemPointers(eo->list + eo->count - 1, en->list);
6964 bruce@momjian.us 60 [ - + ]: 1265165 : Assert(res != 0);
61 : :
62 [ - + ]: 1265165 : if (res > 0)
2995 peter_e@gmx.net 63 :UBC 0 : eo->shouldSort = true;
64 : : }
65 : :
5408 tgl@sss.pgh.pa.us 66 :CBC 1265165 : eo->list[eo->count] = en->list[0];
67 : 1265165 : eo->count++;
5738 teodor@sigaev.ru 68 : 1265165 : }
69 : :
70 : : /* Comparator function for rbtree.c */
71 : : static int
2548 tgl@sss.pgh.pa.us 72 : 14518227 : cmpEntryAccumulator(const RBTNode *a, const RBTNode *b, void *arg)
73 : : {
5408 74 : 14518227 : const GinEntryAccumulator *ea = (const GinEntryAccumulator *) a;
75 : 14518227 : const GinEntryAccumulator *eb = (const GinEntryAccumulator *) b;
5723 bruce@momjian.us 76 : 14518227 : BuildAccumulator *accum = (BuildAccumulator *) arg;
77 : :
5408 tgl@sss.pgh.pa.us 78 : 29036454 : return ginCompareAttEntries(accum->ginstate,
79 : 14518227 : ea->attnum, ea->key, ea->category,
80 : 14518227 : eb->attnum, eb->key, eb->category);
81 : : }
82 : :
83 : : /* Allocator function for rbtree.c */
84 : : static RBTNode *
5567 85 : 259563 : ginAllocEntryAccumulator(void *arg)
86 : : {
87 : 259563 : BuildAccumulator *accum = (BuildAccumulator *) arg;
88 : : GinEntryAccumulator *ea;
89 : :
90 : : /*
91 : : * Allocate memory by rather big chunks to decrease overhead. We have no
92 : : * need to reclaim RBTNodes individually, so this costs nothing.
93 : : */
5408 94 [ + + + + ]: 259563 : if (accum->entryallocator == NULL || accum->eas_used >= DEF_NENTRY)
95 : : {
96 : 312 : accum->entryallocator = palloc(sizeof(GinEntryAccumulator) * DEF_NENTRY);
5567 97 : 312 : accum->allocatedMemory += GetMemoryChunkSpace(accum->entryallocator);
5408 98 : 312 : accum->eas_used = 0;
99 : : }
100 : :
101 : : /* Allocate new RBTNode from current chunk */
102 : 259563 : ea = accum->entryallocator + accum->eas_used;
103 : 259563 : accum->eas_used++;
104 : :
2548 105 : 259563 : return (RBTNode *) ea;
106 : : }
107 : :
108 : : void
5738 teodor@sigaev.ru 109 : 240 : ginInitBA(BuildAccumulator *accum)
110 : : {
111 : : /* accum->ginstate is intentionally not set here */
112 : 240 : accum->allocatedMemory = 0;
113 : 240 : accum->entryallocator = NULL;
5408 tgl@sss.pgh.pa.us 114 : 240 : accum->eas_used = 0;
2548 115 : 240 : accum->tree = rbt_create(sizeof(GinEntryAccumulator),
116 : : cmpEntryAccumulator,
117 : : ginCombineData,
118 : : ginAllocEntryAccumulator,
119 : : NULL, /* no freefunc needed */
120 : : accum);
7119 teodor@sigaev.ru 121 : 240 : }
122 : :
123 : : /*
124 : : * This is basically the same as datumCopy(), but extended to count
125 : : * palloc'd space in accum->allocatedMemory.
126 : : */
127 : : static Datum
6318 tgl@sss.pgh.pa.us 128 : 259489 : getDatumCopy(BuildAccumulator *accum, OffsetNumber attnum, Datum value)
129 : : {
130 : : CompactAttribute *att;
131 : : Datum res;
132 : :
312 drowley@postgresql.o 133 : 259489 : att = TupleDescCompactAttr(accum->ginstate->origTupdesc, attnum - 1);
6318 tgl@sss.pgh.pa.us 134 [ + + ]: 259489 : if (att->attbyval)
7044 135 : 249838 : res = value;
136 : : else
137 : : {
6318 138 : 9651 : res = datumCopy(value, false, att->attlen);
6330 139 : 9651 : accum->allocatedMemory += GetMemoryChunkSpace(DatumGetPointer(res));
140 : : }
7044 141 : 259489 : return res;
142 : : }
143 : :
144 : : /*
145 : : * Find/store one entry from indexed value.
146 : : */
147 : : static void
5408 148 : 1524728 : ginInsertBAEntry(BuildAccumulator *accum,
149 : : ItemPointer heapptr, OffsetNumber attnum,
150 : : Datum key, GinNullCategory category)
151 : : {
152 : : GinEntryAccumulator eatmp;
153 : : GinEntryAccumulator *ea;
154 : : bool isNew;
155 : :
156 : : /*
157 : : * For the moment, fill only the fields of eatmp that will be looked at by
158 : : * cmpEntryAccumulator or ginCombineData.
159 : : */
160 : 1524728 : eatmp.attnum = attnum;
161 : 1524728 : eatmp.key = key;
162 : 1524728 : eatmp.category = category;
163 : : /* temporarily set up single-entry itempointer list */
164 : 1524728 : eatmp.list = heapptr;
165 : :
2548 166 : 1524728 : ea = (GinEntryAccumulator *) rbt_insert(accum->tree, (RBTNode *) &eatmp,
167 : : &isNew);
168 : :
5567 169 [ + + ]: 1524728 : if (isNew)
170 : : {
171 : : /*
172 : : * Finish initializing new tree entry, including making permanent
173 : : * copies of the datum (if it's not null) and itempointer.
174 : : */
5408 175 [ + + ]: 259563 : if (category == GIN_CAT_NORM_KEY)
176 : 259489 : ea->key = getDatumCopy(accum, attnum, key);
177 : 259563 : ea->maxcount = DEF_NPTR;
178 : 259563 : ea->count = 1;
2995 peter_e@gmx.net 179 : 259563 : ea->shouldSort = false;
5567 tgl@sss.pgh.pa.us 180 : 259563 : ea->list =
181 : 259563 : (ItemPointerData *) palloc(sizeof(ItemPointerData) * DEF_NPTR);
182 : 259563 : ea->list[0] = *heapptr;
183 : 259563 : accum->allocatedMemory += GetMemoryChunkSpace(ea->list);
184 : : }
185 : : else
186 : : {
187 : : /*
188 : : * ginCombineData did everything needed.
189 : : */
190 : : }
7049 teodor@sigaev.ru 191 : 1524728 : }
192 : :
193 : : /*
194 : : * Insert the entries for one heap pointer.
195 : : *
196 : : * Since the entries are being inserted into a balanced binary tree, you
197 : : * might think that the order of insertion wouldn't be critical, but it turns
198 : : * out that inserting the entries in sorted order results in a lot of
199 : : * rebalancing operations and is slow. To prevent this, we attempt to insert
200 : : * the nodes in an order that will produce a nearly-balanced tree if the input
201 : : * is in fact sorted.
202 : : *
203 : : * We do this as follows. First, we imagine that we have an array whose size
204 : : * is the smallest power of two greater than or equal to the actual array
205 : : * size. Second, we insert the middle entry of our virtual array into the
206 : : * tree; then, we insert the middles of each half of our virtual array, then
207 : : * middles of quarters, etc.
208 : : */
209 : : void
5408 tgl@sss.pgh.pa.us 210 : 667650 : ginInsertBAEntries(BuildAccumulator *accum,
211 : : ItemPointer heapptr, OffsetNumber attnum,
212 : : Datum *entries, GinNullCategory *categories,
213 : : int32 nentries)
214 : : {
215 : 667650 : uint32 step = nentries;
216 : :
217 [ - + ]: 667650 : if (nentries <= 0)
7049 teodor@sigaev.ru 218 :UBC 0 : return;
219 : :
6062 tgl@sss.pgh.pa.us 220 [ + - - + ]:CBC 667650 : Assert(ItemPointerIsValid(heapptr) && attnum >= FirstOffsetNumber);
221 : :
222 : : /*
223 : : * step will contain largest power of 2 and <= nentries
224 : : */
5723 bruce@momjian.us 225 : 667650 : step |= (step >> 1);
226 : 667650 : step |= (step >> 2);
227 : 667650 : step |= (step >> 4);
228 : 667650 : step |= (step >> 8);
5738 teodor@sigaev.ru 229 : 667650 : step |= (step >> 16);
230 : 667650 : step >>= 1;
5723 bruce@momjian.us 231 : 667650 : step++;
232 : :
233 [ + + ]: 1680455 : while (step > 0)
234 : : {
235 : : int i;
236 : :
5408 tgl@sss.pgh.pa.us 237 [ + + + - ]: 2537533 : for (i = step - 1; i < nentries && i >= 0; i += step << 1 /* *2 */ )
238 : 1524728 : ginInsertBAEntry(accum, heapptr, attnum,
239 : 1524728 : entries[i], categories[i]);
240 : :
5723 bruce@momjian.us 241 : 1012805 : step >>= 1; /* /2 */
242 : : }
243 : : }
244 : :
245 : : static int
6964 bruce@momjian.us 246 :UBC 0 : qsortCompareItemPointers(const void *a, const void *b)
247 : : {
5490 tgl@sss.pgh.pa.us 248 : 0 : int res = ginCompareItemPointers((ItemPointer) a, (ItemPointer) b);
249 : :
250 : : /* Assert that there are no equal item pointers being sorted */
6964 bruce@momjian.us 251 [ # # ]: 0 : Assert(res != 0);
7119 teodor@sigaev.ru 252 : 0 : return res;
253 : : }
254 : :
255 : : /* Prepare to read out the rbtree contents using ginGetBAEntry */
256 : : void
5567 tgl@sss.pgh.pa.us 257 :CBC 240 : ginBeginBAScan(BuildAccumulator *accum)
258 : : {
2548 259 : 240 : rbt_begin_iterate(accum->tree, LeftRightWalk, &accum->tree_walk);
5567 260 : 240 : }
261 : :
262 : : /*
263 : : * Get the next entry in sequence from the BuildAccumulator's rbtree.
264 : : * This consists of a single key datum and a list (array) of one or more
265 : : * heap TIDs in which that key is found. The list is guaranteed sorted.
266 : : */
267 : : ItemPointerData *
5408 268 : 259803 : ginGetBAEntry(BuildAccumulator *accum,
269 : : OffsetNumber *attnum, Datum *key, GinNullCategory *category,
270 : : uint32 *n)
271 : : {
272 : : GinEntryAccumulator *entry;
273 : : ItemPointerData *list;
274 : :
2548 275 : 259803 : entry = (GinEntryAccumulator *) rbt_iterate(&accum->tree_walk);
276 : :
6964 bruce@momjian.us 277 [ + + ]: 259803 : if (entry == NULL)
5408 tgl@sss.pgh.pa.us 278 : 240 : return NULL; /* no more entries */
279 : :
6318 280 : 259563 : *attnum = entry->attnum;
5408 281 : 259563 : *key = entry->key;
282 : 259563 : *category = entry->category;
6964 bruce@momjian.us 283 : 259563 : list = entry->list;
5408 tgl@sss.pgh.pa.us 284 : 259563 : *n = entry->count;
285 : :
286 [ + - - + ]: 259563 : Assert(list != NULL && entry->count > 0);
287 : :
288 [ - + - - ]: 259563 : if (entry->shouldSort && entry->count > 1)
5408 tgl@sss.pgh.pa.us 289 :UBC 0 : qsort(list, entry->count, sizeof(ItemPointerData),
290 : : qsortCompareItemPointers);
291 : :
7119 teodor@sigaev.ru 292 :CBC 259563 : return list;
293 : : }
|