| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2026 Tiger Data, Inc. | ||
| 3 | * Licensed under the PostgreSQL License. See LICENSE for details. | ||
| 4 | * | ||
| 5 | * parallel_backend.c - PostgreSQL implementations of the build's back-end | ||
| 6 | * seams. | ||
| 7 | * | ||
| 8 | * The build driver is shared between the PG extension and the standalone | ||
| 9 | * engine; the parts that genuinely differ by back-end are expressed as | ||
| 10 | * same-named seam functions, with the PG versions here and the thread-based | ||
| 11 | * versions under src/standalone. It holds the PG implementations of all the | ||
| 12 | * build seams: DSM setup over shm_toc, ParallelContext launch/teardown, worker | ||
| 13 | * attach/detach, the heap parallel scan, and per-worker count reduction. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #include <postgres.h> | ||
| 17 | |||
| 18 | #include "vs_config.h" | ||
| 19 | |||
| 20 | #include <access/parallel.h> | ||
| 21 | #include <access/table.h> | ||
| 22 | #include <access/tableam.h> | ||
| 23 | #include <catalog/index.h> | ||
| 24 | #include <catalog/pg_operator_d.h> | ||
| 25 | #include <catalog/pg_type_d.h> | ||
| 26 | #include <executor/instrument.h> | ||
| 27 | #include <executor/tuptable.h> | ||
| 28 | #include <miscadmin.h> | ||
| 29 | #include <optimizer/plancat.h> | ||
| 30 | #include <pgstat.h> | ||
| 31 | #include <storage/buffile.h> | ||
| 32 | #include <storage/latch.h> | ||
| 33 | #include <storage/proc.h> | ||
| 34 | #include <storage/spin.h> | ||
| 35 | #include <tcop/tcopprot.h> | ||
| 36 | #include <utils/rel.h> | ||
| 37 | #include <utils/snapmgr.h> | ||
| 38 | #include <utils/tuplesort.h> | ||
| 39 | #include <utils/wait_event.h> | ||
| 40 | |||
| 41 | #include "build.h" | ||
| 42 | #include "index/index_build.h" | ||
| 43 | #include "index/parallel_build.h" | ||
| 44 | #include "pg/bufstorage.h" | ||
| 45 | #include "quant/matrix.h" | ||
| 46 | #include "quant/rabitq.h" | ||
| 47 | #include "support_pg.h" | ||
| 48 | #include "typeinfo.h" | ||
| 49 | #include "types/vec32.h" | ||
| 50 | |||
| 51 | /* | ||
| 52 | * PG-specific shared build state: the neutral PrismBuildShared plus the | ||
| 53 | * relation identity, query id, and the spinlock guarding its counters. A | ||
| 54 | * ParallelTableScanDesc is appended after it in the DSM segment. The base is | ||
| 55 | * the first member, so the PrismBuildShared * the workers look up out of the | ||
| 56 | * toc is recovered here as a PrismBuildSharedPg *. | ||
| 57 | */ | ||
| 58 | typedef struct PrismBuildSharedPg | ||
| 59 | { | ||
| 60 | PrismBuildShared base; | ||
| 61 | Oid heaprelid; | ||
| 62 | Oid indexrelid; | ||
| 63 | int64 queryid; | ||
| 64 | /* DSM handle of the dedicated sample segment (sample-region seam). */ | ||
| 65 | dsm_handle sample_handle; | ||
| 66 | /* DSM handle of the per-child subtree ring, created by the leader after | ||
| 67 | * root assignment (when the per-child sample counts that bound the slot | ||
| 68 | * size are known) and published before the ring barrier. */ | ||
| 69 | dsm_handle subtree_ring_handle; | ||
| 70 | /* DSM handle of the exact centroid collection, created by the leader | ||
| 71 | * after the streaming tree write and published before the tree-ready | ||
| 72 | * barrier (exact-centroid seam). */ | ||
| 73 | dsm_handle exact_centroids_handle; | ||
| 74 | slock_t mutex; | ||
| 75 | /* Striped locks guarding the shared leaf-refinement accumulator. */ | ||
| 76 | slock_t accum_locks[PRISM_REFINE_LOCK_STRIPES]; | ||
| 77 | } PrismBuildSharedPg; | ||
| 78 | |||
| 79 | #define ParallelTableScanFromVsShared(shared) \ | ||
| 80 | ((ParallelTableScanDesc)((char *)(shared) + \ | ||
| 81 | BUFFERALIGN(sizeof(PrismBuildSharedPg)))) | ||
| 82 | |||
| 83 | /* | ||
| 84 | * Bridges PostgreSQL's heap-tuple callback to the back-end-neutral scan | ||
| 85 | * callback: skip nulls and hand the vector's data pointer (into the varlena, | ||
| 86 | * no copy) plus its tid to the shared logic. | ||
| 87 | * | ||
| 88 | * A halfvec column cannot be handed over no-copy -- the shared logic is | ||
| 89 | * float32 -- so it is widened into buf, reused across tuples. The callback | ||
| 90 | * consumes the pointer before returning, so one buffer per participant is | ||
| 91 | * enough. | ||
| 92 | */ | ||
| 93 | typedef struct PrismPgScanAdapter | ||
| 94 | { | ||
| 95 | PrismBuildScanCb cb; | ||
| 96 | void *state; | ||
| 97 | Vec32Access input; | ||
| 98 | MemoryContext tuple_ctx; | ||
| 99 | } PrismPgScanAdapter; | ||
| 100 | |||
| 101 | static void | ||
| 102 | 232960 | prism_pg_scan_adapter( | |
| 103 | Relation index, | ||
| 104 | ItemPointer tid, | ||
| 105 | Datum *values, | ||
| 106 | bool *isnull, | ||
| 107 | bool tuple_is_alive, | ||
| 108 | void *adapter_state) | ||
| 109 | { | ||
| 110 | 232960 | PrismPgScanAdapter *a = (PrismPgScanAdapter *)adapter_state; | |
| 111 | |||
| 112 | 232960 | (void)index; | |
| 113 | 232960 | (void)tuple_is_alive; | |
| 114 | |||
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232960 times.
|
232960 | if (isnull[0]) |
| 116 | ✗ | return; | |
| 117 | |||
| 118 | 232960 | MemoryContext old_ctx = MemoryContextSwitchTo(a->tuple_ctx); | |
| 119 | 232960 | Vec32Ref vref = vec32_read(&a->input, values[0]); | |
| 120 | 232960 | MemoryContextSwitchTo(old_ctx); | |
| 121 | 232960 | a->cb(a->state, *tid, vref.data); | |
| 122 | 232960 | MemoryContextReset(a->tuple_ctx); | |
| 123 | } | ||
| 124 | |||
| 125 | /* | ||
| 126 | * Scan every vector via the shared parallel table scan, invoking cb per live | ||
| 127 | * tuple. The standalone back-end provides a same-named function that iterates | ||
| 128 | * its in-memory vector array (work-stealing) instead. allow_sync/progress | ||
| 129 | * are PostgreSQL table_index_build_scan flags, ignored in standalone; progress | ||
| 130 | * gates pg_stat_progress_create_index reporting, so only the leader sets it | ||
| 131 | * (otherwise every participant would inflate the tuples-scanned counter). | ||
| 132 | */ | ||
| 133 | double | ||
| 134 | 228 | prism_build_scan( | |
| 135 | Relation heap, | ||
| 136 | Relation index, | ||
| 137 | struct IndexInfo *indexInfo, | ||
| 138 | PrismBuildShared *shared, | ||
| 139 | bool allow_sync, | ||
| 140 | bool progress, | ||
| 141 | PrismBuildScanCb cb, | ||
| 142 | void *state) | ||
| 143 | { | ||
| 144 | 228 | Dimension dim = (Dimension)TupleDescAttr(index->rd_att, 0)->atttypmod; | |
| 145 | /* Build-time path: resolved directly, since the per-backend cache reads a | ||
| 146 | * metadata page this build has not written yet. */ | ||
| 147 | 456 | PrismPgScanAdapter actx = { | |
| 148 | .cb = cb, | ||
| 149 | .state = state, | ||
| 150 | 228 | .input = vec32_access( | |
| 151 | prism_index_type_info(index), dim, CurrentMemoryContext), | ||
| 152 | 228 | .tuple_ctx = AllocSetContextCreate( | |
| 153 | CurrentMemoryContext, | ||
| 154 | "prism parallel scan tuple", | ||
| 155 | ALLOCSET_DEFAULT_SIZES), | ||
| 156 | }; | ||
| 157 | |||
| 158 | #if PG_VERSION_NUM >= 190000 | ||
| 159 | TableScanDesc scan = table_beginscan_parallel( | ||
| 160 | heap, ParallelTableScanFromVsShared(shared), SO_NONE); | ||
| 161 | #else | ||
| 162 | 228 | TableScanDesc scan = table_beginscan_parallel( | |
| 163 | heap, ParallelTableScanFromVsShared(shared)); | ||
| 164 | #endif | ||
| 165 | |||
| 166 | 228 | double reltuples = table_index_build_scan( | |
| 167 | heap, | ||
| 168 | index, | ||
| 169 | indexInfo, | ||
| 170 | allow_sync, | ||
| 171 | progress, | ||
| 172 | prism_pg_scan_adapter, | ||
| 173 | &actx, | ||
| 174 | scan); | ||
| 175 | 228 | MemoryContextDelete(actx.tuple_ctx); | |
| 176 | 228 | return reltuples; | |
| 177 | } | ||
| 178 | |||
| 179 | /* | ||
| 180 | * Relation lock modes a parallel worker uses for the heap and index. A | ||
| 181 | * concurrent build (CREATE INDEX CONCURRENTLY) must take weak locks: the | ||
| 182 | * leader holds only ShareUpdateExclusive on the heap, and acquiring a strong | ||
| 183 | * index lock in a worker logs it for hot-standby and assigns an XID, which is | ||
| 184 | * illegal in a parallel worker. Matches PostgreSQL's btree parallel build. The | ||
| 185 | * same pair is used to open the relations on attach and to close them on | ||
| 186 | * detach. | ||
| 187 | */ | ||
| 188 | static void | ||
| 189 | 178 | worker_lockmodes(bool concurrent, LOCKMODE *heapmode, LOCKMODE *indexmode) | |
| 190 | { | ||
| 191 | 352 | *heapmode = concurrent ? ShareUpdateExclusiveLock : ShareLock; | |
| 192 | 174 | *indexmode = concurrent ? RowExclusiveLock : AccessExclusiveLock; | |
| 193 | } | ||
| 194 | |||
| 195 | /* Per-worker build memory context (see prism_pbuild_worker_attach). */ | ||
| 196 | static MemoryContext vs_pbuild_worker_ctx = NULL; | ||
| 197 | static MemoryContext vs_pbuild_worker_oldctx = NULL; | ||
| 198 | |||
| 199 | /* | ||
| 200 | * Join the parallel build: look up the shared state, open the heap and index, | ||
| 201 | * start per-worker instrumentation, and attach to the phase barrier. The | ||
| 202 | * standalone back-end provides a same-named function that takes the shared | ||
| 203 | * state and vectors directly and joins a thread barrier. | ||
| 204 | */ | ||
| 205 | void | ||
| 206 | 90 | prism_pbuild_worker_attach(shm_toc *toc, PrismPBuildWorker *w) | |
| 207 | { | ||
| 208 | 90 | PrismBuildShared *shared = | |
| 209 | 90 | shm_toc_lookup(toc, PRISM_DSM_KEY_SHARED, false); | |
| 210 | 90 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 211 | 90 | Barrier *barrier = shm_toc_lookup(toc, PRISM_DSM_KEY_BARRIER, false); | |
| 212 | |||
| 213 | 90 | char *sharedquery = shm_toc_lookup(toc, PRISM_DSM_KEY_QUERY_TEXT, true); | |
| 214 | 90 | debug_query_string = sharedquery; | |
| 215 | 90 | pgstat_report_activity(STATE_RUNNING, debug_query_string); | |
| 216 | 90 | pgstat_report_query_id(pg->queryid, false); | |
| 217 | |||
| 218 | 90 | LOCKMODE heapmode, indexmode; | |
| 219 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2 times.
|
90 | worker_lockmodes(shared->concurrent, &heapmode, &indexmode); |
| 220 | |||
| 221 | 90 | w->shared = shared; | |
| 222 | 90 | w->barrier = barrier; | |
| 223 | 90 | w->heapRel = table_open(pg->heaprelid, heapmode); | |
| 224 | 90 | w->indexRel = index_open(pg->indexrelid, indexmode); | |
| 225 | 90 | w->worker_id = ParallelWorkerNumber + 1; | |
| 226 | 90 | w->dim = shared->dim; | |
| 227 | |||
| 228 | /* | ||
| 229 | * All of this worker's build allocations (routing state, per-child | ||
| 230 | * subtree blobs, batch buffers) go into a named context so | ||
| 231 | * pg_backend_memory_contexts attributes them to the build and they are | ||
| 232 | * reclaimed together at detach. A file-static is safe for the same | ||
| 233 | * reason as vs_pbuild_snapshot: one build per worker, non-reentrant. | ||
| 234 | */ | ||
| 235 | 90 | vs_pbuild_worker_ctx = AllocSetContextCreate( | |
| 236 | CurrentMemoryContext, "vs worker build", ALLOCSET_DEFAULT_SIZES); | ||
| 237 | 90 | vs_pbuild_worker_oldctx = MemoryContextSwitchTo(vs_pbuild_worker_ctx); | |
| 238 | |||
| 239 | 90 | InstrStartParallelQuery(); | |
| 240 | |||
| 241 | /* | ||
| 242 | * Attach to the dynamic phase barrier (before the first phase). The leader | ||
| 243 | * attaches before launching workers, so the party tracks all participants | ||
| 244 | * that actually start and stays in lockstep regardless of how many workers | ||
| 245 | * PostgreSQL launched. | ||
| 246 | */ | ||
| 247 | 90 | BarrierAttach(barrier); | |
| 248 | 90 | } | |
| 249 | |||
| 250 | /* | ||
| 251 | * Leave the parallel build: report this worker's buffer/WAL usage back to the | ||
| 252 | * leader and close the relations. The standalone back-end's same-named | ||
| 253 | * function joins the thread and is otherwise a no-op. | ||
| 254 | */ | ||
| 255 | void | ||
| 256 | 88 | prism_pbuild_worker_detach(shm_toc *toc, PrismPBuildWorker *w) | |
| 257 | { | ||
| 258 | 88 | MemoryContextSwitchTo(vs_pbuild_worker_oldctx); | |
| 259 | 88 | MemoryContextDelete(vs_pbuild_worker_ctx); | |
| 260 | 88 | vs_pbuild_worker_ctx = NULL; | |
| 261 | 88 | vs_pbuild_worker_oldctx = NULL; | |
| 262 | |||
| 263 | 88 | BufferUsage *bufferusage = | |
| 264 | 88 | shm_toc_lookup(toc, PRISM_DSM_KEY_BUFFER_USAGE, false); | |
| 265 | 88 | WalUsage *walusage = shm_toc_lookup(toc, PRISM_DSM_KEY_WAL_USAGE, false); | |
| 266 | 88 | InstrEndParallelQuery( | |
| 267 | 88 | &bufferusage[ParallelWorkerNumber], | |
| 268 | 88 | &walusage[ParallelWorkerNumber]); | |
| 269 | |||
| 270 | 88 | LOCKMODE heapmode, indexmode; | |
| 271 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 2 times.
|
88 | worker_lockmodes(w->shared->concurrent, &heapmode, &indexmode); |
| 272 | 88 | index_close(w->indexRel, indexmode); | |
| 273 | 88 | table_close(w->heapRel, heapmode); | |
| 274 | 88 | } | |
| 275 | |||
| 276 | /* | ||
| 277 | * Page-backed routing storage seam (see parallel_build.h). PG workers are | ||
| 278 | * separate processes, so each opens its own VsStorage on the worker's index | ||
| 279 | * relation; the leader's storage pointer cannot cross the process boundary, so | ||
| 280 | * publish is a no-op here. | ||
| 281 | */ | ||
| 282 | void | ||
| 283 | 45 | prism_pbuild_publish_storage(PrismBuildShared *shared, VsStorage *s) | |
| 284 | { | ||
| 285 | 45 | (void)shared; | |
| 286 | 45 | (void)s; | |
| 287 | 45 | } | |
| 288 | |||
| 289 | VsStorage * | ||
| 290 | 88 | prism_pbuild_worker_storage(PrismPBuildWorker *w) | |
| 291 | { | ||
| 292 | /* No table relation needed (routing reads index pages only, no rerank). */ | ||
| 293 | 88 | VsPgStorage *s = palloc(sizeof(VsPgStorage)); | |
| 294 | 88 | vs_pg_storage_init(s, w->indexRel, NULL, w->shared->metric); | |
| 295 | 88 | s->build_mode = true; /* reads only; matches the leader's build storage */ | |
| 296 | 88 | return &s->base; /* base is the first member */ | |
| 297 | } | ||
| 298 | |||
| 299 | void | ||
| 300 | 88 | prism_pbuild_worker_storage_release(VsStorage *s) | |
| 301 | { | ||
| 302 | /* The route helper releases every page it reads, so no buffer stays | ||
| 303 | * pinned; just free the wrapper (allocated in the worker's memory | ||
| 304 | * context). */ | ||
| 305 | 88 | pfree(s); | |
| 306 | 88 | } | |
| 307 | |||
| 308 | /* | ||
| 309 | * Snapshot used to initialize the parallel heap scan for CREATE INDEX | ||
| 310 | * CONCURRENTLY (an MVCC snapshot; SnapshotAny/NULL for a normal build). It | ||
| 311 | * must stay registered for the whole parallel operation and is released at | ||
| 312 | * teardown. A file-static is safe: an index build is single-threaded and | ||
| 313 | * non-reentrant in the leader backend, and every parallel-build exit path runs | ||
| 314 | * prism_pbuild_teardown. | ||
| 315 | */ | ||
| 316 | static Snapshot vs_pbuild_snapshot = NULL; | ||
| 317 | |||
| 318 | /* | ||
| 319 | * Tear the parallel context down and leave parallel mode. The standalone | ||
| 320 | * back-end provides a same-named function that joins its worker threads and | ||
| 321 | * frees the shared arena instead. | ||
| 322 | */ | ||
| 323 | void | ||
| 324 | 46 | prism_pbuild_teardown(ParallelContext *pcxt) | |
| 325 | { | ||
| 326 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 45 times.
|
46 | if (vs_pbuild_snapshot != NULL) |
| 327 | { | ||
| 328 | 1 | UnregisterSnapshot(vs_pbuild_snapshot); | |
| 329 | 1 | vs_pbuild_snapshot = NULL; | |
| 330 | } | ||
| 331 | 46 | DestroyParallelContext(pcxt); | |
| 332 | 46 | ExitParallelMode(); | |
| 333 | 46 | } | |
| 334 | |||
| 335 | /* | ||
| 336 | * Leader-only subtree blob store (see parallel_build.h): a BufFile temp | ||
| 337 | * file. Small blob sets never leave the kernel page cache; large ones spill | ||
| 338 | * to pgsql_tmp automatically, so the store adds no unbounded memory. Blobs | ||
| 339 | * are length-prefixed and read back strictly in append order. | ||
| 340 | */ | ||
| 341 | struct PrismBlobStore | ||
| 342 | { | ||
| 343 | BufFile *file; | ||
| 344 | }; | ||
| 345 | |||
| 346 | PrismBlobStore * | ||
| 347 | 223 | prism_pbuild_blobstore_begin(void) | |
| 348 | { | ||
| 349 | 223 | PrismBlobStore *bs = palloc(sizeof(PrismBlobStore)); | |
| 350 | 223 | bs->file = BufFileCreateTemp(false); | |
| 351 | 223 | return bs; | |
| 352 | } | ||
| 353 | |||
| 354 | void | ||
| 355 | 3930 | prism_pbuild_blobstore_put(PrismBlobStore *bs, const void *blob, uint64_t size) | |
| 356 | { | ||
| 357 | 3930 | BufFileWrite(bs->file, &size, sizeof(size)); | |
| 358 | 3930 | BufFileWrite(bs->file, blob, (size_t)size); | |
| 359 | 3930 | } | |
| 360 | |||
| 361 | void | ||
| 362 | 223 | prism_pbuild_blobstore_rewind(PrismBlobStore *bs) | |
| 363 | { | ||
| 364 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 223 times.
|
223 | if (BufFileSeek(bs->file, 0, 0, SEEK_SET) != 0) |
| 365 | ✗ | ereport(ERROR, | |
| 366 | (errcode_for_file_access(), | ||
| 367 | errmsg("could not rewind subtree blob store"))); | ||
| 368 | 223 | } | |
| 369 | |||
| 370 | uint64_t | ||
| 371 | 3930 | prism_pbuild_blobstore_get(PrismBlobStore *bs, void *buf, uint64_t max_size) | |
| 372 | { | ||
| 373 | 3930 | uint64_t size; | |
| 374 | 3930 | BufFileReadExact(bs->file, &size, sizeof(size)); | |
| 375 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3930 times.
|
3930 | if (size > max_size) |
| 376 | ✗ | ereport(ERROR, | |
| 377 | (errcode(ERRCODE_INTERNAL_ERROR), | ||
| 378 | errmsg("subtree blob larger than its slot (%llu > %llu)", | ||
| 379 | (unsigned long long)size, | ||
| 380 | (unsigned long long)max_size))); | ||
| 381 | 3930 | BufFileReadExact(bs->file, buf, (size_t)size); | |
| 382 | 3930 | return size; | |
| 383 | } | ||
| 384 | |||
| 385 | void | ||
| 386 | 223 | prism_pbuild_blobstore_end(PrismBlobStore *bs) | |
| 387 | { | ||
| 388 | 223 | BufFileClose(bs->file); | |
| 389 | 223 | pfree(bs); | |
| 390 | 223 | } | |
| 391 | |||
| 392 | /* | ||
| 393 | * Launch the worker participants and wait until they have all attached to the | ||
| 394 | * barrier (so the dynamic party reaches launched+1 before the leader advances | ||
| 395 | * the first phase). Returns false — after tearing the context down — if no | ||
| 396 | * workers started, so the caller falls back to a serial build. The standalone | ||
| 397 | * back-end provides a same-named function that spawns threads and joins them | ||
| 398 | * at the barrier instead. | ||
| 399 | */ | ||
| 400 | bool | ||
| 401 | 47 | prism_pbuild_launch( | |
| 402 | ParallelContext *pcxt, Barrier *barrier, PrismBuildShared *shared) | ||
| 403 | { | ||
| 404 | 47 | LaunchParallelWorkers(pcxt); | |
| 405 | |||
| 406 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
|
47 | if (pcxt->nworkers_launched == 0) |
| 407 | { | ||
| 408 | 1 | WaitForParallelWorkersToFinish(pcxt); | |
| 409 | 1 | prism_pbuild_teardown(pcxt); | |
| 410 | 1 | return false; | |
| 411 | } | ||
| 412 | |||
| 413 | /* | ||
| 414 | * Workers attach to the barrier dynamically, so the party (1 leader + N | ||
| 415 | * launched) is not final until they all have; if the leader arrived first | ||
| 416 | * it could advance the phase alone and strand late workers. | ||
| 417 | * WaitForParallelWorkersToAttach surfaces a startup failure as an error | ||
| 418 | * rather than a hang; then poll until the live participant count is whole. | ||
| 419 | */ | ||
| 420 | 46 | WaitForParallelWorkersToAttach(pcxt); | |
| 421 |
2/2✓ Branch 2 taken 233 times.
✓ Branch 3 taken 46 times.
|
279 | while (BarrierParticipants(barrier) < pcxt->nworkers_launched + 1) |
| 422 | { | ||
| 423 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 231 times.
|
233 | CHECK_FOR_INTERRUPTS(); |
| 424 | 233 | (void)WaitLatch( | |
| 425 | MyLatch, | ||
| 426 | WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, | ||
| 427 | 1L, | ||
| 428 | WAIT_EVENT_PARALLEL_CREATE_INDEX_SCAN); | ||
| 429 | 233 | ResetLatch(MyLatch); | |
| 430 | } | ||
| 431 | |||
| 432 | /* The launch can fall short of the plan (the parallel-worker pool under | ||
| 433 | * max_parallel_workers is shared with concurrent queries). Narrow the | ||
| 434 | * participant count to the party that attached so the phases partition | ||
| 435 | * their work over participants that exist; the per-participant DSM | ||
| 436 | * regions keep their planned size and leave the tail slots unused. */ | ||
| 437 | 46 | shared->nparticipants = pcxt->nworkers_launched + 1; | |
| 438 | 46 | return true; | |
| 439 | } | ||
| 440 | |||
| 441 | /* | ||
| 442 | * Allocate and populate the parallel build's shared state: the DSM segment and | ||
| 443 | * its regions (shared header, barrier, sample/centroid/assignment slots, the | ||
| 444 | * tree blob, the per-worker page queues, usage counters). Does not launch | ||
| 445 | * workers; the caller does. Coarse PG block — the standalone back-end provides | ||
| 446 | * a same-named function over a heap arena. Returns false (after tearing the | ||
| 447 | * parallel context down) if the DSM segment could not be created. | ||
| 448 | */ | ||
| 449 | bool | ||
| 450 | 47 | prism_pbuild_setup_shared( | |
| 451 | PrismPBuildLeader *lead, | ||
| 452 | Relation heap, | ||
| 453 | Relation index, | ||
| 454 | const PrismBuildConfig *config, | ||
| 455 | int nworkers) | ||
| 456 | { | ||
| 457 | 47 | Dimension dim = config->dim; | |
| 458 | 47 | uint32_t nlist = config->nlist; | |
| 459 | 47 | int nparticipants = nworkers + 1; | |
| 460 | 47 | uint64_t rabitq_seed = VS_RABITQ_BUILD_SEED; | |
| 461 | 47 | uint32_t fan_out = config->fan_out > 0 ? config->fan_out | |
| 462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
|
47 | : prism_auto_fan_out(0, nlist, 0); |
| 463 | 47 | uint32_t km_k = fan_out < nlist ? fan_out : nlist; | |
| 464 | |||
| 465 | /* | ||
| 466 | * Size the k-means sample set. The samples live in one shared-memory | ||
| 467 | * region (total_samples * dim floats) that must stay resident for the | ||
| 468 | * whole tree build -- root k-means and every subtree -- so its size is the | ||
| 469 | * build's dominant memory cost. The ideal is ~256 samples per list, but at | ||
| 470 | * fine nlist that can dwarf available RAM (nlist=480k -> 123M samples -> | ||
| 471 | * ~360 GB), far beyond what a DSM segment can hold. | ||
| 472 | * | ||
| 473 | * Bound it by maintenance_work_mem: that is the build's memory budget and | ||
| 474 | * the knob operators already raise for large index builds. When the budget | ||
| 475 | * is smaller than the ideal the stride sampler simply draws a coarser (but | ||
| 476 | * still uniform) subsample to fit. estimate_rel_size() caps it to the rows | ||
| 477 | * that actually exist so small tables don't over-allocate; it is only a | ||
| 478 | * hint -- the budget is the hard bound, so an inaccurate estimate cannot | ||
| 479 | * over-commit shared memory. | ||
| 480 | */ | ||
| 481 | 47 | uint64_t want_samples = (uint64_t)nlist * 256; | |
| 482 | |||
| 483 | /* maintenance_work_mem is in kB; reserve it for the sample region. */ | ||
| 484 | 47 | uint64_t mem_bytes = (uint64_t)maintenance_work_mem * UINT64CONST(1024); | |
| 485 | 47 | uint64_t vec_nbytes = (uint64_t)dim * sizeof(float); | |
| 486 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | uint64_t budget = vec_nbytes > 0 ? mem_bytes / vec_nbytes : want_samples; |
| 487 | 47 | if (budget < 10000) | |
| 488 | budget = 10000; /* k-means needs a workable minimum */ | ||
| 489 | |||
| 490 | 47 | uint64_t total64 = Min(want_samples, budget); | |
| 491 | |||
| 492 | 47 | BlockNumber est_pages; | |
| 493 | 47 | double est_tuples; | |
| 494 | 47 | double allvisfrac; | |
| 495 | 47 | estimate_rel_size(heap, NULL, &est_pages, &est_tuples, &allvisfrac); | |
| 496 |
3/4✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✓ Branch 3 taken 7 times.
|
47 | if (est_tuples > 0.0 && (double)total64 > est_tuples) |
| 497 | 40 | total64 = (uint64_t)est_tuples; | |
| 498 | |||
| 499 | /* total64 <= want_samples = nlist*256 <= 512M (nlist reloption max 2M), so | ||
| 500 | * it always fits a uint32. */ | ||
| 501 | 47 | uint32_t total_samples = (uint32_t)Max(total64, UINT64CONST(1)); | |
| 502 | |||
| 503 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
47 | if (want_samples > budget && est_tuples > (double)budget) |
| 504 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | elog(LOG, |
| 505 | VS_EXTENSION_NAME | ||
| 506 | ": k-means sample set limited to %u of the " | ||
| 507 | "ideal " UINT64_FORMAT " vectors by maintenance_work_mem " | ||
| 508 | "(%d kB); raise maintenance_work_mem for finer centroid " | ||
| 509 | "training on large tables", | ||
| 510 | total_samples, | ||
| 511 | want_samples, | ||
| 512 | maintenance_work_mem); | ||
| 513 | |||
| 514 | 47 | uint32_t max_per_worker = (total_samples + nparticipants - 1) / | |
| 515 | nparticipants; | ||
| 516 | |||
| 517 | /* | ||
| 518 | * The refine decision itself is the leader's, made after clustering when | ||
| 519 | * the actual leaf count is known (see PrismBuildShared.refine); setup only | ||
| 520 | * publishes the gate inputs. The pass is single-shot by design: refine | ||
| 521 | * routes every row over the centroid PAGES, which it never rewrites (it | ||
| 522 | * updates the heads' encode references, which routing does not read), so | ||
| 523 | * the row-to-leaf assignment is a fixed point -- a second pass would | ||
| 524 | * rescan the whole table to recompute the exact same means. | ||
| 525 | */ | ||
| 526 | |||
| 527 | 47 | EnterParallelMode(); | |
| 528 | |||
| 529 | 47 | ParallelContext *pcxt = CreateParallelContext( | |
| 530 | VS_MODULE_NAME, "prism_parallel_build_main", nworkers); | ||
| 531 | |||
| 532 | /* | ||
| 533 | * The heap scan's snapshot. A normal build sees all tuples (SnapshotAny); | ||
| 534 | * CREATE INDEX CONCURRENTLY must use an MVCC snapshot so it indexes only | ||
| 535 | * tuples visible to it (heapam asserts SnapshotAny <-> a valid OldestXmin, | ||
| 536 | * so the concurrent path must not pass SnapshotAny). Register it for the | ||
| 537 | * duration — its serialized size also affects the DSM estimate below — and | ||
| 538 | * release it in prism_pbuild_teardown. Mirrors PostgreSQL's nbtsort.c. | ||
| 539 | */ | ||
| 540 | 47 | Snapshot snapshot = config->concurrent | |
| 541 | 1 | ? RegisterSnapshot(GetTransactionSnapshot()) | |
| 542 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
|
47 | : SnapshotAny; |
| 543 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1 times.
|
47 | vs_pbuild_snapshot = (snapshot != SnapshotAny) ? snapshot : NULL; |
| 544 | 47 | Size est_shared = add_size( | |
| 545 | BUFFERALIGN(sizeof(PrismBuildSharedPg)), | ||
| 546 | table_parallelscan_estimate(heap, snapshot)); | ||
| 547 | |||
| 548 | 47 | shm_toc_estimate_chunk(&pcxt->estimator, est_shared); | |
| 549 | 47 | shm_toc_estimate_chunk(&pcxt->estimator, sizeof(Barrier)); | |
| 550 | /* K-means shared centroids + norms (root level, k=km_k) */ | ||
| 551 | 47 | shm_toc_estimate_chunk( | |
| 552 | &pcxt->estimator, prism_dsm_centroids_size(km_k, dim)); | ||
| 553 | /* K-means per-worker accumulators */ | ||
| 554 | 47 | shm_toc_estimate_chunk( | |
| 555 | &pcxt->estimator, | ||
| 556 | prism_dsm_km_workers_size(nparticipants, km_k, dim)); | ||
| 557 | /* Root assignments: per-worker uint32_t[max_per_worker] */ | ||
| 558 | 47 | shm_toc_estimate_chunk( | |
| 559 | &pcxt->estimator, | ||
| 560 | prism_dsm_root_assign_size(nparticipants, max_per_worker)); | ||
| 561 | /* Shared coordinator for the cluster-keyed posting sort (sort seam). Sized | ||
| 562 | * for the planned participant count (upper bound on launched workers). */ | ||
| 563 | 47 | shm_toc_estimate_chunk( | |
| 564 | &pcxt->estimator, prism_pbuild_sort_shared_size(nparticipants)); | ||
| 565 | |||
| 566 | /* Page-backed routing: the global mean, published by the leader before the | ||
| 567 | * tree-ready barrier. The posting-head base (leaf c's head = first_posting | ||
| 568 | * + c) is a scalar in PrismBuildShared, so no O(nlist) head array is | ||
| 569 | * shared. | ||
| 570 | */ | ||
| 571 | 47 | shm_toc_estimate_chunk(&pcxt->estimator, (Size)vec_nbytes); | |
| 572 | |||
| 573 | /* The leaf-refinement accumulator overlays the sample segment once the | ||
| 574 | * samples are dead (see the sample-region seam), so it needs no DSM chunk | ||
| 575 | * of its own; its tile capacity is bounded by the memory budget, by | ||
| 576 | * MaxAllocSize, and by the region it overlays. */ | ||
| 577 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
|
47 | Size samp_sz = prism_dsm_samples_size(nparticipants, max_per_worker, dim); |
| 578 | 47 | uint64_t refine_cap_bytes = | |
| 579 | 47 | Min((uint64_t)maintenance_work_mem * 1024, (uint64_t)MaxAllocSize); | |
| 580 | 47 | if (refine_cap_bytes > | |
| 581 | 47 | (uint64_t)samp_sz - offsetof(PrismDsmRefineAccum, sums)) | |
| 582 | refine_cap_bytes = (uint64_t)samp_sz - | ||
| 583 | offsetof(PrismDsmRefineAccum, sums); | ||
| 584 | 47 | uint32_t refine_tile_cap = | |
| 585 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
|
47 | prism_refine_tile_leaves(nlist, dim, refine_cap_bytes); |
| 586 | |||
| 587 | 47 | shm_toc_estimate_chunk( | |
| 588 | &pcxt->estimator, mul_size(sizeof(WalUsage), pcxt->nworkers)); | ||
| 589 | 47 | shm_toc_estimate_chunk( | |
| 590 | &pcxt->estimator, mul_size(sizeof(BufferUsage), pcxt->nworkers)); | ||
| 591 | |||
| 592 | 47 | int querylen = 0; | |
| 593 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | if (debug_query_string) |
| 594 | { | ||
| 595 | 47 | querylen = strlen(debug_query_string); | |
| 596 | 47 | shm_toc_estimate_chunk(&pcxt->estimator, querylen + 1); | |
| 597 | } | ||
| 598 | |||
| 599 | /* nkeys: shared, barrier, centroids, km_workers, root_assign, | ||
| 600 | * sortshared, wal, buffer, global_mean + optionally query_text (samples | ||
| 601 | * and the subtree ring live in their own segments; the refine | ||
| 602 | * accumulator overlays the samples) */ | ||
| 603 | 47 | int nkeys = 9; | |
| 604 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | if (debug_query_string) |
| 605 | 47 | nkeys++; | |
| 606 | 47 | shm_toc_estimate_keys(&pcxt->estimator, nkeys); | |
| 607 | |||
| 608 | /* Total bytes the leader is about to commit to the DSM segment (sum of all | ||
| 609 | * estimated chunks), for the planned-allocation introspection line. */ | ||
| 610 | 47 | Size dsm_total = pcxt->estimator.space_for_chunks; | |
| 611 | |||
| 612 | 47 | InitializeParallelDSM(pcxt); | |
| 613 | |||
| 614 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
|
47 | if (pcxt->seg == NULL) |
| 615 | { | ||
| 616 | ✗ | prism_pbuild_teardown(pcxt); | |
| 617 | ✗ | return false; | |
| 618 | } | ||
| 619 | |||
| 620 | /* ---- Populate shared state ---- */ | ||
| 621 | 47 | PrismBuildSharedPg *pg = shm_toc_allocate(pcxt->toc, est_shared); | |
| 622 | 47 | PrismBuildShared *shared = &pg->base; | |
| 623 | 47 | pg->heaprelid = RelationGetRelid(heap); | |
| 624 | 47 | pg->indexrelid = RelationGetRelid(index); | |
| 625 | 47 | pg->queryid = pgstat_get_my_query_id(); | |
| 626 | 47 | shared->concurrent = config->concurrent; | |
| 627 | 47 | shared->dim = dim; | |
| 628 | 47 | shared->metric = config->metric; | |
| 629 | 47 | shared->nlist = nlist; | |
| 630 | 47 | shared->fan_out = fan_out; /* resolved (auto if config 0) */ | |
| 631 | 47 | shared->subtree_slot_size = 0; /* leader sizes the ring post-assign */ | |
| 632 | 47 | shared->soar_lambda = config->soar_lambda; | |
| 633 | 47 | shared->boundary_epsilon = config->boundary_epsilon; | |
| 634 | 47 | shared->fastscan = config->fastscan; | |
| 635 | 47 | shared->centroid_format = config->centroid_format; | |
| 636 | 47 | shared->rabitq_seed = rabitq_seed; | |
| 637 | 47 | shared->nparticipants = nparticipants; | |
| 638 | 47 | shared->work_mem_kb = maintenance_work_mem; | |
| 639 | 47 | shared->max_samples_per_worker = max_per_worker; | |
| 640 | 47 | shared->km_max_iterations = 20; | |
| 641 | 47 | shared->km_tolerance = 1e-4f; | |
| 642 | 47 | shared->km_k = km_k; | |
| 643 | 47 | shared->km_converged = false; | |
| 644 | 47 | shared->refine_threshold = (uint32_t)prism_leaf_refine_threshold; | |
| 645 | 47 | shared->refine = false; /* leader decides post-clustering */ | |
| 646 | 47 | shared->refine_tile_cap = refine_tile_cap; | |
| 647 | /* Build routes for accuracy, not query speed (see PRISM_BUILD_CENTROID_* | ||
| 648 | * in posting_build.h): decouple from the query-tuned GUCs. */ | ||
| 649 | 47 | shared->centroid_error_scale = PRISM_BUILD_CENTROID_ERROR_SCALE; | |
| 650 | 47 | shared->centroid_beam_scale = PRISM_BUILD_CENTROID_BEAM_SCALE; | |
| 651 | 47 | shared->fastscan_bits = prism_fastscan_bits; | |
| 652 | 47 | SpinLockInit(&pg->mutex); | |
| 653 |
2/2✓ Branch 0 taken 12032 times.
✓ Branch 1 taken 47 times.
|
12079 | for (int i = 0; i < PRISM_REFINE_LOCK_STRIPES; i++) |
| 654 | 12032 | SpinLockInit(&pg->accum_locks[i]); | |
| 655 | 47 | shared->reltuples = 0.0; | |
| 656 | 47 | shared->indtuples = 0.0; | |
| 657 | 47 | shared->soar_dupes = 0.0; | |
| 658 | /* The scan descriptor sits in the same chunk, right after the struct; | ||
| 659 | * est_shared sized it for both. The address is therefore inside the | ||
| 660 | * allocation while being past the end of pg->base, which is what the | ||
| 661 | * checker measures against. Mirrors nbtsort.c. */ | ||
| 662 | /* coverity[OVERRUN] */ | ||
| 663 | 47 | table_parallelscan_initialize( | |
| 664 | heap, ParallelTableScanFromVsShared(shared), snapshot); | ||
| 665 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_SHARED, shared); | |
| 666 | |||
| 667 | /* | ||
| 668 | * Barrier for phase synchronization. It is a *dynamic* barrier (init with | ||
| 669 | * 0 parties): the leader and every launched worker join via BarrierAttach, | ||
| 670 | * so the party tracks the participants that actually start. A non-zero | ||
| 671 | * (static) party is wrong here — BarrierAttach forbids it | ||
| 672 | * (Assert(!static_party)), and a fixed planned count would deadlock when | ||
| 673 | * PostgreSQL launches fewer workers than planned. The leader attaches in | ||
| 674 | * do_parallel_build before launching workers. | ||
| 675 | */ | ||
| 676 | 47 | Barrier *barrier = shm_toc_allocate(pcxt->toc, sizeof(Barrier)); | |
| 677 | 47 | BarrierInit(barrier, 0); | |
| 678 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_BARRIER, barrier); | |
| 679 | |||
| 680 | /* Sample slots, in a dedicated DSM segment so the build's largest | ||
| 681 | * working set can be handed back (last detach destroys it) before the | ||
| 682 | * posting sort claims its own budget -- see the sample-region seam. | ||
| 683 | * Workers attach via the handle published in the shared state. */ | ||
| 684 | 47 | dsm_segment *sample_seg = dsm_create(samp_sz, 0); | |
| 685 | 47 | PrismDsmSamples *dsm_samples = dsm_segment_address(sample_seg); | |
| 686 | /* Only the header + per-participant counts are read before being written; | ||
| 687 | * the sample data is filled by the sampling pass and read back bounded by | ||
| 688 | * those counts, so zeroing the (multi-GB) data region is wasted work. */ | ||
| 689 | 47 | memset(dsm_samples, | |
| 690 | 0, | ||
| 691 | 47 | MAXALIGN(sizeof(PrismDsmSamples)) + | |
| 692 | (size_t)nparticipants * sizeof(uint32_t)); | ||
| 693 | 47 | dsm_samples->nparticipants = nparticipants; | |
| 694 | 47 | dsm_samples->max_per_worker = max_per_worker; | |
| 695 | 47 | dsm_samples->dim = dim; | |
| 696 | 47 | pg->sample_handle = dsm_segment_handle(sample_seg); | |
| 697 | |||
| 698 | /* Shared centroids + norms (root k-means, k=km_k) */ | ||
| 699 | 47 | Size cent_sz = prism_dsm_centroids_size(km_k, dim); | |
| 700 | 47 | char *centroids_base = shm_toc_allocate(pcxt->toc, cent_sz); | |
| 701 | 47 | memset(centroids_base, 0, cent_sz); | |
| 702 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_CENTROIDS, centroids_base); | |
| 703 | 47 | float *cents = prism_dsm_centroids(centroids_base); | |
| 704 | |||
| 705 | /* Per-worker k-means accumulators */ | ||
| 706 | 47 | Size km_sz = prism_dsm_km_workers_size(nparticipants, km_k, dim); | |
| 707 | 47 | char *km_workers_base = shm_toc_allocate(pcxt->toc, km_sz); | |
| 708 | 47 | memset(km_workers_base, 0, km_sz); | |
| 709 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_KM_WORKERS, km_workers_base); | |
| 710 | |||
| 711 | /* Root assignment slots */ | ||
| 712 | 47 | Size ra_sz = prism_dsm_root_assign_size(nparticipants, max_per_worker); | |
| 713 | 47 | PrismDsmRootAssign *dsm_ra = shm_toc_allocate(pcxt->toc, ra_sz); | |
| 714 | 47 | memset(dsm_ra, 0, ra_sz); | |
| 715 | 47 | dsm_ra->nparticipants = nparticipants; | |
| 716 | 47 | dsm_ra->max_per_worker = max_per_worker; | |
| 717 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_ROOT_ASSIGN, dsm_ra); | |
| 718 | |||
| 719 | /* Shared coordinator for the cluster-keyed posting sort (sort seam). Sized | ||
| 720 | * for the planned participant count; the leader calls | ||
| 721 | * prism_pbuild_sort_shared_init with the actual launched count after | ||
| 722 | * launch. | ||
| 723 | */ | ||
| 724 | 47 | Size sort_sz = prism_pbuild_sort_shared_size(nparticipants); | |
| 725 | 47 | void *sortshared = shm_toc_allocate(pcxt->toc, sort_sz); | |
| 726 | 47 | memset(sortshared, 0, sort_sz); | |
| 727 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_SORTSHARED, sortshared); | |
| 728 | |||
| 729 | /* Page-backed routing region (filled by the leader before the tree-ready | ||
| 730 | * barrier): the global mean. The posting-head base is a scalar in | ||
| 731 | * PrismBuildShared, so there is no O(nlist) head array here. */ | ||
| 732 | 47 | float *dsm_gmean = shm_toc_allocate(pcxt->toc, (Size)vec_nbytes); | |
| 733 | 47 | memset(dsm_gmean, 0, (Size)vec_nbytes); | |
| 734 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_GLOBAL_MEAN, dsm_gmean); | |
| 735 | |||
| 736 | 47 | WalUsage *walusage = shm_toc_allocate( | |
| 737 | 47 | pcxt->toc, mul_size(sizeof(WalUsage), pcxt->nworkers)); | |
| 738 | 47 | memset(walusage, 0, mul_size(sizeof(WalUsage), pcxt->nworkers)); | |
| 739 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_WAL_USAGE, walusage); | |
| 740 | |||
| 741 | 47 | BufferUsage *bufferusage = shm_toc_allocate( | |
| 742 | 47 | pcxt->toc, mul_size(sizeof(BufferUsage), pcxt->nworkers)); | |
| 743 | 47 | memset(bufferusage, 0, mul_size(sizeof(BufferUsage), pcxt->nworkers)); | |
| 744 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_BUFFER_USAGE, bufferusage); | |
| 745 | |||
| 746 |
1/2✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
|
47 | if (debug_query_string) |
| 747 | { | ||
| 748 | 47 | char *sq = shm_toc_allocate(pcxt->toc, querylen + 1); | |
| 749 | 47 | memcpy(sq, debug_query_string, querylen + 1); | |
| 750 | 47 | shm_toc_insert(pcxt->toc, PRISM_DSM_KEY_QUERY_TEXT, sq); | |
| 751 | } | ||
| 752 | |||
| 753 | 47 | lead->pcxt = pcxt; | |
| 754 | 47 | lead->shared = shared; | |
| 755 | 47 | lead->barrier = barrier; | |
| 756 | 47 | lead->dsm_samples = dsm_samples; | |
| 757 | 47 | lead->sample_seg = sample_seg; | |
| 758 | 47 | lead->centroids_base = centroids_base; | |
| 759 | 47 | lead->cents = cents; | |
| 760 | 47 | lead->km_workers_base = km_workers_base; | |
| 761 | 47 | lead->dsm_ra = dsm_ra; | |
| 762 | 47 | lead->walusage = walusage; | |
| 763 | 47 | lead->bufferusage = bufferusage; | |
| 764 | 47 | lead->nparticipants = nparticipants; | |
| 765 | 47 | lead->km_k = km_k; | |
| 766 | 47 | lead->max_per_worker = max_per_worker; | |
| 767 | 47 | lead->dim = dim; | |
| 768 | 47 | lead->nlist = nlist; | |
| 769 | 47 | lead->rabitq_seed = rabitq_seed; | |
| 770 | 47 | lead->fan_out = fan_out; | |
| 771 | 47 | lead->dsm_total = dsm_total; | |
| 772 | 47 | return true; | |
| 773 | } | ||
| 774 | |||
| 775 | /* | ||
| 776 | * Accumulate one worker's tuple counts into the shared state under the lock. | ||
| 777 | * Back-end seam: only the spinlock is PG-specific (it lives in the derived | ||
| 778 | * struct); the standalone version locks a pthread mutex around the same adds. | ||
| 779 | */ | ||
| 780 | void | ||
| 781 | 88 | prism_pbuild_worker_add_counts( | |
| 782 | PrismBuildShared *shared, | ||
| 783 | double indtuples, | ||
| 784 | double soar_dupes, | ||
| 785 | double heap_tuples) | ||
| 786 | { | ||
| 787 | 88 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 788 | |||
| 789 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
|
88 | SpinLockAcquire(&pg->mutex); |
| 790 | 88 | shared->indtuples += indtuples; | |
| 791 | 88 | shared->soar_dupes += soar_dupes; | |
| 792 | 88 | shared->reltuples += heap_tuples; | |
| 793 | 88 | SpinLockRelease(&pg->mutex); | |
| 794 | 88 | } | |
| 795 | |||
| 796 | /* | ||
| 797 | * Sample-region seam (see parallel_build.h): the samples live in their own | ||
| 798 | * DSM segment; workers attach by the handle the leader published, and every | ||
| 799 | * participant detaches independently once the refine pass is done -- the | ||
| 800 | * last detach destroys the segment and returns the build's largest working | ||
| 801 | * set before the posting sort claims its budget. | ||
| 802 | */ | ||
| 803 | PrismDsmSamples * | ||
| 804 | 90 | prism_pbuild_samples_attach( | |
| 805 | shm_toc *toc, PrismBuildShared *shared, void **seg_out) | ||
| 806 | { | ||
| 807 | 90 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 808 | |||
| 809 | 90 | (void)toc; | |
| 810 | 90 | dsm_segment *seg = dsm_attach(pg->sample_handle); | |
| 811 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (seg == NULL) |
| 812 | ✗ | ereport(ERROR, | |
| 813 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), | ||
| 814 | errmsg("could not attach to prism sample segment"))); | ||
| 815 | 90 | *seg_out = seg; | |
| 816 | 90 | return (PrismDsmSamples *)dsm_segment_address(seg); | |
| 817 | } | ||
| 818 | |||
| 819 | void | ||
| 820 | 134 | prism_pbuild_samples_release(PrismDsmSamples *samples, void *seg) | |
| 821 | { | ||
| 822 | 134 | (void)samples; | |
| 823 |
1/2✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
|
134 | if (seg != NULL) |
| 824 | 134 | dsm_detach((dsm_segment *)seg); | |
| 825 | 134 | } | |
| 826 | |||
| 827 | /* | ||
| 828 | * Subtree-ring seam: the ring of nparticipants subtree slots lives in its | ||
| 829 | * own DSM segment, created by the leader only after root assignment -- the | ||
| 830 | * per-child sample counts known then bound the slot far below the analytic | ||
| 831 | * worst case. The handle travels in shared; every participant detaches when | ||
| 832 | * the streaming pass is done, and the last detach frees the memory. | ||
| 833 | */ | ||
| 834 | char * | ||
| 835 | 20 | prism_pbuild_subtree_ring_create( | |
| 836 | PrismBuildShared *shared, | ||
| 837 | int nparticipants, | ||
| 838 | uint64_t slot_size, | ||
| 839 | void **seg_out) | ||
| 840 | { | ||
| 841 | 20 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 842 | |||
| 843 | 20 | dsm_segment *seg = dsm_create( | |
| 844 | prism_dsm_child_subtrees_size(nparticipants, slot_size), 0); | ||
| 845 | 20 | pg->subtree_ring_handle = dsm_segment_handle(seg); | |
| 846 | 20 | shared->subtree_slot_size = slot_size; | |
| 847 | 20 | *seg_out = seg; | |
| 848 | 20 | return (char *)dsm_segment_address(seg); | |
| 849 | } | ||
| 850 | |||
| 851 | char * | ||
| 852 | 38 | prism_pbuild_subtree_ring_attach(PrismBuildShared *shared, void **seg_out) | |
| 853 | { | ||
| 854 | 38 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 855 | |||
| 856 | 38 | dsm_segment *seg = dsm_attach(pg->subtree_ring_handle); | |
| 857 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | if (seg == NULL) |
| 858 | ✗ | ereport(ERROR, | |
| 859 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), | ||
| 860 | errmsg("could not attach to prism subtree segment"))); | ||
| 861 | 38 | *seg_out = seg; | |
| 862 | 38 | return (char *)dsm_segment_address(seg); | |
| 863 | } | ||
| 864 | |||
| 865 | void | ||
| 866 | 55 | prism_pbuild_subtree_ring_release(void *seg) | |
| 867 | { | ||
| 868 |
1/2✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 | if (seg != NULL) |
| 869 | 55 | dsm_detach((dsm_segment *)seg); | |
| 870 | 55 | } | |
| 871 | |||
| 872 | /* | ||
| 873 | * Exact-centroid seam: the exact centroid collection lives in its own DSM | ||
| 874 | * segment, created by the leader only after the streaming tree write (its | ||
| 875 | * size — a few MB — is known then). The handle travels in shared; every | ||
| 876 | * participant detaches when its page-backed routing is done, and the last | ||
| 877 | * detach frees the memory. | ||
| 878 | */ | ||
| 879 | char * | ||
| 880 | 45 | prism_pbuild_exact_centroids_create( | |
| 881 | PrismBuildShared *shared, uint64_t nbytes, void **seg_out) | ||
| 882 | { | ||
| 883 | 45 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 884 | |||
| 885 | 45 | dsm_segment *seg = dsm_create(nbytes, 0); | |
| 886 | 45 | pg->exact_centroids_handle = dsm_segment_handle(seg); | |
| 887 | 45 | *seg_out = seg; | |
| 888 | 45 | return (char *)dsm_segment_address(seg); | |
| 889 | } | ||
| 890 | |||
| 891 | char * | ||
| 892 | 88 | prism_pbuild_exact_centroids_attach(PrismBuildShared *shared, void **seg_out) | |
| 893 | { | ||
| 894 | 88 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 895 | |||
| 896 | 88 | dsm_segment *seg = dsm_attach(pg->exact_centroids_handle); | |
| 897 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (seg == NULL) |
| 898 | ✗ | ereport(ERROR, | |
| 899 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), | ||
| 900 | errmsg("could not attach to prism exact-centroid " | ||
| 901 | "segment"))); | ||
| 902 | 88 | *seg_out = seg; | |
| 903 | 88 | return (char *)dsm_segment_address(seg); | |
| 904 | } | ||
| 905 | |||
| 906 | void | ||
| 907 | 133 | prism_pbuild_exact_centroids_release(void *seg) | |
| 908 | { | ||
| 909 |
1/2✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
|
133 | if (seg != NULL) |
| 910 | 133 | dsm_detach((dsm_segment *)seg); | |
| 911 | 133 | } | |
| 912 | |||
| 913 | /* | ||
| 914 | * Re-initialize the parallel scan for the posting pass; the sampling pass | ||
| 915 | * consumed the first one. Back-end seam: the standalone version resets its | ||
| 916 | * work-stealing cursor. | ||
| 917 | */ | ||
| 918 | void | ||
| 919 | 47 | prism_pbuild_rescan(Relation heap, PrismBuildShared *shared) | |
| 920 | { | ||
| 921 | 47 | table_parallelscan_reinitialize( | |
| 922 | heap, ParallelTableScanFromVsShared(shared)); | ||
| 923 | 47 | } | |
| 924 | |||
| 925 | /* | ||
| 926 | * Striped-lock seam for the shared leaf-refinement accumulator. Contention is | ||
| 927 | * low because rows spread across nleaves leaves into PRISM_REFINE_LOCK_STRIPES | ||
| 928 | * stripes. | ||
| 929 | */ | ||
| 930 | void | ||
| 931 | 12600 | prism_pbuild_accum_lock(PrismBuildShared *shared, uint32_t stripe) | |
| 932 | { | ||
| 933 | 12600 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 934 |
2/2✓ Branch 1 taken 197 times.
✓ Branch 2 taken 12403 times.
|
12600 | SpinLockAcquire(&pg->accum_locks[stripe]); |
| 935 | 12600 | } | |
| 936 | |||
| 937 | void | ||
| 938 | 12600 | prism_pbuild_accum_unlock(PrismBuildShared *shared, uint32_t stripe) | |
| 939 | { | ||
| 940 | 12600 | PrismBuildSharedPg *pg = (PrismBuildSharedPg *)shared; | |
| 941 | 12600 | SpinLockRelease(&pg->accum_locks[stripe]); | |
| 942 | 12600 | } | |
| 943 | |||
| 944 | /* ---------------------------------------------------------------- | ||
| 945 | * Posting sort seam (PG back-end) — parallel tuplesort | ||
| 946 | * | ||
| 947 | * Each entry is sorted by a uint32 cluster id; the payload is a fixed-size | ||
| 948 | * opaque blob carried in a bytea column. Workers feed partial runs into the | ||
| 949 | * shared Sharedsort (in the build DSM); the leader merges and reads them back | ||
| 950 | * grouped by cluster. Memory is bounded by maintenance_work_mem (tuplesort | ||
| 951 | * spills past it). The standalone back-end provides the same-named seam over | ||
| 952 | * in-memory arrays. | ||
| 953 | * ---------------------------------------------------------------- */ | ||
| 954 | struct PrismSorter | ||
| 955 | { | ||
| 956 | Tuplesortstate *ts; | ||
| 957 | TupleDesc tupdesc; | ||
| 958 | TupleTableSlot *slot; | ||
| 959 | SortCoordinate coord; | ||
| 960 | uint32_t entry_size; | ||
| 961 | bytea *payload; /* reusable scratch: VARHDRSZ + entry_size */ | ||
| 962 | bool is_leader; | ||
| 963 | }; | ||
| 964 | |||
| 965 | Size | ||
| 966 | 94 | prism_pbuild_sort_shared_size(int nparticipants) | |
| 967 | { | ||
| 968 | 94 | return tuplesort_estimate_shared(nparticipants); | |
| 969 | } | ||
| 970 | |||
| 971 | void | ||
| 972 | 45 | prism_pbuild_sort_shared_init(void *region, int nparticipants, void *seg) | |
| 973 | { | ||
| 974 | 45 | tuplesort_initialize_shared( | |
| 975 | (Sharedsort *)region, nparticipants, (dsm_segment *)seg); | ||
| 976 | 45 | } | |
| 977 | |||
| 978 | PrismSorter * | ||
| 979 | 337 | prism_pbuild_sort_begin( | |
| 980 | void *region, | ||
| 981 | void *seg, | ||
| 982 | int participant, | ||
| 983 | int nparticipants, | ||
| 984 | bool is_leader, | ||
| 985 | uint32_t entry_size, | ||
| 986 | int work_mem_kb) | ||
| 987 | { | ||
| 988 | 337 | (void)participant; | |
| 989 | 337 | PrismSorter *s = palloc0(sizeof(PrismSorter)); | |
| 990 | 337 | s->entry_size = entry_size; | |
| 991 | 337 | s->is_leader = is_leader; | |
| 992 | |||
| 993 | 337 | s->tupdesc = CreateTemplateTupleDesc(2); | |
| 994 | 337 | TupleDescInitEntry(s->tupdesc, 1, "cluster", INT4OID, -1, 0); | |
| 995 | 337 | TupleDescInitEntry(s->tupdesc, 2, "payload", BYTEAOID, -1, 0); | |
| 996 | #if PG_VERSION_NUM >= 190000 | ||
| 997 | TupleDescFinalize(s->tupdesc); | ||
| 998 | #endif | ||
| 999 | |||
| 1000 | /* region == NULL: a plain, non-parallel sort (the serial build) — no | ||
| 1001 | * coordinate, no attach. Otherwise a parallel participant: a leader | ||
| 1002 | * merging runs, or a worker producing one (which attaches to the shared | ||
| 1003 | * fileset). */ | ||
| 1004 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 204 times.
|
337 | if (region != NULL) |
| 1005 | { | ||
| 1006 | 133 | s->coord = palloc0(sizeof(SortCoordinateData)); | |
| 1007 | 133 | s->coord->sharedsort = (Sharedsort *)region; | |
| 1008 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 88 times.
|
133 | if (is_leader) |
| 1009 | { | ||
| 1010 | 45 | s->coord->isWorker = false; | |
| 1011 | 45 | s->coord->nParticipants = nparticipants; | |
| 1012 | } | ||
| 1013 | else | ||
| 1014 | { | ||
| 1015 | 88 | s->coord->isWorker = true; | |
| 1016 | 88 | s->coord->nParticipants = -1; | |
| 1017 | } | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | 337 | AttrNumber attNums[1] = {1}; | |
| 1021 | 337 | Oid sortOps[1] = {Int4LessOperator}; | |
| 1022 | 337 | Oid sortColls[1] = {InvalidOid}; | |
| 1023 | 337 | bool nullsF[1] = {false}; | |
| 1024 | 337 | s->ts = tuplesort_begin_heap( | |
| 1025 | s->tupdesc, | ||
| 1026 | 1, | ||
| 1027 | attNums, | ||
| 1028 | sortOps, | ||
| 1029 | sortColls, | ||
| 1030 | nullsF, | ||
| 1031 | work_mem_kb, | ||
| 1032 | s->coord, | ||
| 1033 | TUPLESORT_NONE); | ||
| 1034 | |||
| 1035 | /* Workers attach to the shared fileset; the leader holds it via the | ||
| 1036 | * backend's dsm reference and must not attach (per tuplesort.h). */ | ||
| 1037 |
2/2✓ Branch 0 taken 88 times.
✓ Branch 1 taken 249 times.
|
337 | if (region != NULL && !is_leader) |
| 1038 | 88 | tuplesort_attach_shared((Sharedsort *)region, (dsm_segment *)seg); | |
| 1039 | |||
| 1040 | 337 | s->slot = MakeSingleTupleTableSlot(s->tupdesc, &TTSOpsMinimalTuple); | |
| 1041 | 337 | s->payload = (bytea *)palloc(VARHDRSZ + entry_size); | |
| 1042 | 337 | SET_VARSIZE(s->payload, VARHDRSZ + entry_size); | |
| 1043 | 337 | return s; | |
| 1044 | } | ||
| 1045 | |||
| 1046 | void | ||
| 1047 | 407640 | prism_pbuild_sort_put(PrismSorter *s, uint32_t cluster, const void *entry) | |
| 1048 | { | ||
| 1049 | 407640 | memcpy(VARDATA(s->payload), entry, s->entry_size); | |
| 1050 | 407640 | ExecClearTuple(s->slot); | |
| 1051 | 407640 | s->slot->tts_values[0] = Int32GetDatum((int32)cluster); | |
| 1052 | 407640 | s->slot->tts_isnull[0] = false; | |
| 1053 | 407640 | s->slot->tts_values[1] = PointerGetDatum(s->payload); | |
| 1054 | 407640 | s->slot->tts_isnull[1] = false; | |
| 1055 | 407640 | ExecStoreVirtualTuple(s->slot); | |
| 1056 | 407640 | tuplesort_puttupleslot(s->ts, s->slot); | |
| 1057 | 407640 | } | |
| 1058 | |||
| 1059 | void | ||
| 1060 | 337 | prism_pbuild_sort_performsort(PrismSorter *s) | |
| 1061 | { | ||
| 1062 | 337 | tuplesort_performsort(s->ts); | |
| 1063 | 337 | } | |
| 1064 | |||
| 1065 | bool | ||
| 1066 | 407889 | prism_pbuild_sort_getnext( | |
| 1067 | PrismSorter *s, uint32_t *cluster, const void **entry) | ||
| 1068 | { | ||
| 1069 | 407889 | bool isnull; | |
| 1070 |
2/2✓ Branch 1 taken 407640 times.
✓ Branch 2 taken 249 times.
|
407889 | if (!tuplesort_gettupleslot(s->ts, true, false, s->slot, NULL)) |
| 1071 | return false; | ||
| 1072 | 407640 | *cluster = (uint32_t)DatumGetInt32(slot_getattr(s->slot, 1, &isnull)); | |
| 1073 | 407640 | bytea *pl = DatumGetByteaPP(slot_getattr(s->slot, 2, &isnull)); | |
| 1074 |
2/2✓ Branch 0 taken 399611 times.
✓ Branch 1 taken 8029 times.
|
407640 | *entry = VARDATA_ANY(pl); |
| 1075 | 407640 | return true; | |
| 1076 | } | ||
| 1077 | |||
| 1078 | void | ||
| 1079 | 337 | prism_pbuild_sort_end(PrismSorter *s) | |
| 1080 | { | ||
| 1081 | 337 | tuplesort_end(s->ts); | |
| 1082 | 337 | ExecDropSingleTupleTableSlot(s->slot); | |
| 1083 | 337 | FreeTupleDesc(s->tupdesc); | |
| 1084 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 204 times.
|
337 | if (s->coord != NULL) /* NULL for a non-parallel (serial) sort */ |
| 1085 | 133 | pfree(s->coord); | |
| 1086 | 337 | pfree(s->payload); | |
| 1087 | 337 | pfree(s); | |
| 1088 | 337 | } | |
| 1089 |