Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * ginbulk.c
4 : : * routines for fast build of inverted index
5 : : *
6 : : *
7 : : * Portions Copyright (c) 1996-2026, 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
30 : 194154 : ginCombineData(RBTNode *existing, const RBTNode *newdata, void *arg)
31 : : {
32 : 194154 : GinEntryAccumulator *eo = (GinEntryAccumulator *) existing;
33 : 194154 : const GinEntryAccumulator *en = (const GinEntryAccumulator *) newdata;
34 : 194154 : BuildAccumulator *accum = (BuildAccumulator *) arg;
35 : :
36 : : /*
37 : : * Note this code assumes that newdata contains only one itempointer.
38 : : */
39 [ + + ]: 194154 : if (eo->count >= eo->maxcount)
40 : : {
41 [ + - ]: 11659 : if (eo->maxcount > INT_MAX)
42 [ # # # # ]: 0 : ereport(ERROR,
43 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
44 : : errmsg("posting list is too long"),
45 : : errhint("Reduce \"maintenance_work_mem\".")));
46 : :
47 : 11659 : accum->allocatedMemory -= GetMemoryChunkSpace(eo->list);
48 : 11659 : eo->maxcount *= 2;
49 : 11659 : eo->list = (ItemPointerData *)
50 : 11659 : repalloc_huge(eo->list, sizeof(ItemPointerData) * eo->maxcount);
51 : 11659 : accum->allocatedMemory += GetMemoryChunkSpace(eo->list);
52 : 11659 : }
53 : :
54 : : /* If item pointers are not ordered, they will need to be sorted later */
55 [ - + ]: 194154 : if (eo->shouldSort == false)
56 : : {
57 : 194154 : int res;
58 : :
59 : 194154 : res = ginCompareItemPointers(eo->list + eo->count - 1, en->list);
60 [ + - ]: 194154 : Assert(res != 0);
61 : :
62 [ + - ]: 194154 : if (res > 0)
63 : 0 : eo->shouldSort = true;
64 : 194154 : }
65 : :
66 : 194154 : eo->list[eo->count] = en->list[0];
67 : 194154 : eo->count++;
68 : 194154 : }
69 : :
70 : : /* Comparator function for rbtree.c */
71 : : static int
72 : 3782289 : cmpEntryAccumulator(const RBTNode *a, const RBTNode *b, void *arg)
73 : : {
74 : 3782289 : const GinEntryAccumulator *ea = (const GinEntryAccumulator *) a;
75 : 3782289 : const GinEntryAccumulator *eb = (const GinEntryAccumulator *) b;
76 : 3782289 : BuildAccumulator *accum = (BuildAccumulator *) arg;
77 : :
78 : 11346867 : return ginCompareAttEntries(accum->ginstate,
79 : 3782289 : ea->attnum, ea->key, ea->category,
80 : 3782289 : eb->attnum, eb->key, eb->category);
81 : 3782289 : }
82 : :
83 : : /* Allocator function for rbtree.c */
84 : : static RBTNode *
85 : 75500 : ginAllocEntryAccumulator(void *arg)
86 : : {
87 : 75500 : BuildAccumulator *accum = (BuildAccumulator *) arg;
88 : 75500 : 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 : : */
94 [ + + + + ]: 75500 : if (accum->entryallocator == NULL || accum->eas_used >= DEF_NENTRY)
95 : : {
96 : 49 : accum->entryallocator = palloc_array(GinEntryAccumulator, DEF_NENTRY);
97 : 49 : accum->allocatedMemory += GetMemoryChunkSpace(accum->entryallocator);
98 : 49 : accum->eas_used = 0;
99 : 49 : }
100 : :
101 : : /* Allocate new RBTNode from current chunk */
102 : 75500 : ea = accum->entryallocator + accum->eas_used;
103 : 75500 : accum->eas_used++;
104 : :
105 : 151000 : return (RBTNode *) ea;
106 : 75500 : }
107 : :
108 : : void
109 : 22 : ginInitBA(BuildAccumulator *accum)
110 : : {
111 : : /* accum->ginstate is intentionally not set here */
112 : 22 : accum->allocatedMemory = 0;
113 : 22 : accum->entryallocator = NULL;
114 : 22 : accum->eas_used = 0;
115 : 22 : accum->tree = rbt_create(sizeof(GinEntryAccumulator),
116 : : cmpEntryAccumulator,
117 : : ginCombineData,
118 : : ginAllocEntryAccumulator,
119 : : NULL, /* no freefunc needed */
120 : 22 : accum);
121 : 22 : }
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
128 : 75477 : getDatumCopy(BuildAccumulator *accum, OffsetNumber attnum, Datum value)
129 : : {
130 : 75477 : CompactAttribute *att;
131 : 75477 : Datum res;
132 : :
133 : 75477 : att = TupleDescCompactAttr(accum->ginstate->origTupdesc, attnum - 1);
134 [ + + ]: 75477 : if (att->attbyval)
135 : 72980 : res = value;
136 : : else
137 : : {
138 : 2497 : res = datumCopy(value, false, att->attlen);
139 : 2497 : accum->allocatedMemory += GetMemoryChunkSpace(DatumGetPointer(res));
140 : : }
141 : 150954 : return res;
142 : 75477 : }
143 : :
144 : : /*
145 : : * Find/store one entry from indexed value.
146 : : */
147 : : static void
148 : 269654 : ginInsertBAEntry(BuildAccumulator *accum,
149 : : ItemPointer heapptr, OffsetNumber attnum,
150 : : Datum key, GinNullCategory category)
151 : : {
152 : 269654 : GinEntryAccumulator eatmp;
153 : 269654 : GinEntryAccumulator *ea;
154 : 269654 : 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 : 269654 : eatmp.attnum = attnum;
161 : 269654 : eatmp.key = key;
162 : 269654 : eatmp.category = category;
163 : : /* temporarily set up single-entry itempointer list */
164 : 269654 : eatmp.list = heapptr;
165 : :
166 : 269654 : ea = (GinEntryAccumulator *) rbt_insert(accum->tree, (RBTNode *) &eatmp,
167 : : &isNew);
168 : :
169 [ + + ]: 269654 : 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 : : */
175 [ + + ]: 75500 : if (category == GIN_CAT_NORM_KEY)
176 : 75477 : ea->key = getDatumCopy(accum, attnum, key);
177 : 75500 : ea->maxcount = DEF_NPTR;
178 : 75500 : ea->count = 1;
179 : 75500 : ea->shouldSort = false;
180 : 75500 : ea->list = palloc_array(ItemPointerData, DEF_NPTR);
181 : 75500 : ea->list[0] = *heapptr;
182 : 75500 : accum->allocatedMemory += GetMemoryChunkSpace(ea->list);
183 : 75500 : }
184 : : else
185 : : {
186 : : /*
187 : : * ginCombineData did everything needed.
188 : : */
189 : : }
190 : 269654 : }
191 : :
192 : : /*
193 : : * Insert the entries for one heap pointer.
194 : : *
195 : : * Since the entries are being inserted into a balanced binary tree, you
196 : : * might think that the order of insertion wouldn't be critical, but it turns
197 : : * out that inserting the entries in sorted order results in a lot of
198 : : * rebalancing operations and is slow. To prevent this, we attempt to insert
199 : : * the nodes in an order that will produce a nearly-balanced tree if the input
200 : : * is in fact sorted.
201 : : *
202 : : * We do this as follows. First, we imagine that we have an array whose size
203 : : * is the smallest power of two greater than or equal to the actual array
204 : : * size. Second, we insert the middle entry of our virtual array into the
205 : : * tree; then, we insert the middles of each half of our virtual array, then
206 : : * middles of quarters, etc.
207 : : */
208 : : void
209 : 79015 : ginInsertBAEntries(BuildAccumulator *accum,
210 : : ItemPointer heapptr, OffsetNumber attnum,
211 : : Datum *entries, GinNullCategory *categories,
212 : : int32 nentries)
213 : : {
214 : 79015 : uint32 step = nentries;
215 : :
216 [ - + ]: 79015 : if (nentries <= 0)
217 : 0 : return;
218 : :
219 [ + - ]: 79015 : Assert(ItemPointerIsValid(heapptr) && attnum >= FirstOffsetNumber);
220 : :
221 : : /*
222 : : * step will contain largest power of 2 and <= nentries
223 : : */
224 : 79015 : step |= (step >> 1);
225 : 79015 : step |= (step >> 2);
226 : 79015 : step |= (step >> 4);
227 : 79015 : step |= (step >> 8);
228 : 79015 : step |= (step >> 16);
229 : 79015 : step >>= 1;
230 : 79015 : step++;
231 : :
232 [ + + ]: 240798 : while (step > 0)
233 : : {
234 : 161783 : int i;
235 : :
236 [ + + + + ]: 431437 : for (i = step - 1; i < nentries && i >= 0; i += step << 1 /* *2 */ )
237 : 539308 : ginInsertBAEntry(accum, heapptr, attnum,
238 : 269654 : entries[i], categories[i]);
239 : :
240 : 161783 : step >>= 1; /* /2 */
241 : 161783 : }
242 [ - + ]: 79015 : }
243 : :
244 : : static int
245 : 0 : qsortCompareItemPointers(const void *a, const void *b)
246 : : {
247 : 0 : int res = ginCompareItemPointers((ItemPointer) a, (ItemPointer) b);
248 : :
249 : : /* Assert that there are no equal item pointers being sorted */
250 [ # # ]: 0 : Assert(res != 0);
251 : 0 : return res;
252 : 0 : }
253 : :
254 : : /* Prepare to read out the rbtree contents using ginGetBAEntry */
255 : : void
256 : 22 : ginBeginBAScan(BuildAccumulator *accum)
257 : : {
258 : 22 : rbt_begin_iterate(accum->tree, LeftRightWalk, &accum->tree_walk);
259 : 22 : }
260 : :
261 : : /*
262 : : * Get the next entry in sequence from the BuildAccumulator's rbtree.
263 : : * This consists of a single key datum and a list (array) of one or more
264 : : * heap TIDs in which that key is found. The list is guaranteed sorted.
265 : : */
266 : : ItemPointerData *
267 : 75522 : ginGetBAEntry(BuildAccumulator *accum,
268 : : OffsetNumber *attnum, Datum *key, GinNullCategory *category,
269 : : uint32 *n)
270 : : {
271 : 75522 : GinEntryAccumulator *entry;
272 : 75522 : ItemPointerData *list;
273 : :
274 : 75522 : entry = (GinEntryAccumulator *) rbt_iterate(&accum->tree_walk);
275 : :
276 [ + + ]: 75522 : if (entry == NULL)
277 : 22 : return NULL; /* no more entries */
278 : :
279 : 75500 : *attnum = entry->attnum;
280 : 75500 : *key = entry->key;
281 : 75500 : *category = entry->category;
282 : 75500 : list = entry->list;
283 : 75500 : *n = entry->count;
284 : :
285 [ + - ]: 75500 : Assert(list != NULL && entry->count > 0);
286 : :
287 [ - + # # ]: 75500 : if (entry->shouldSort && entry->count > 1)
288 : 0 : qsort(list, entry->count, sizeof(ItemPointerData),
289 : : qsortCompareItemPointers);
290 : :
291 : 75500 : return list;
292 : 75522 : }
|