| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2026 Tiger Data, Inc. | ||
| 3 | * Licensed under the PostgreSQL License. See LICENSE for details. | ||
| 4 | * | ||
| 5 | * posting_convert.c - Convert AoS posting pages to fastscan format | ||
| 6 | * | ||
| 7 | * Walks an AoS posting chain, reads pre-encoded entries, and feeds | ||
| 8 | * them to PrismPostingBuilder via _add_encoded(). The builder | ||
| 9 | * handles group packing, page layout, and chain linking. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "vs_config.h" | ||
| 13 | |||
| 14 | #include <string.h> | ||
| 15 | |||
| 16 | #include "core/log.h" | ||
| 17 | #include "core/memory.h" | ||
| 18 | #include "index/posting_build.h" | ||
| 19 | #include "index/posting_convert.h" | ||
| 20 | #include "quant/fastscan.h" | ||
| 21 | #include "quant/rabitq.h" | ||
| 22 | |||
| 23 | /* | ||
| 24 | * Entries staged out of the AoS chain before any of them is written back. | ||
| 25 | * | ||
| 26 | * The PG storage layer holds one pinned buffer at a time, so reads and | ||
| 27 | * writes cannot interleave: appending to the builder mid-walk would take | ||
| 28 | * over the buffer the walk is reading from. Staging everything first is what | ||
| 29 | * keeps the two apart. | ||
| 30 | */ | ||
| 31 | typedef struct StagedEntry | ||
| 32 | { | ||
| 33 | ItemPointerData tid; | ||
| 34 | float f_add; | ||
| 35 | float f_rescale; | ||
| 36 | float f_error; | ||
| 37 | } StagedEntry; | ||
| 38 | |||
| 39 | typedef struct StageCtx | ||
| 40 | { | ||
| 41 | Dimension dim; | ||
| 42 | uint32_t packed_bytes; | ||
| 43 | uint32_t total_entries; | ||
| 44 | uint32_t entries_cap; | ||
| 45 | StagedEntry *staged; | ||
| 46 | uint8_t *all_bits; | ||
| 47 | BlockNumber head; | ||
| 48 | bool aos_seen; /* an AoS entry page came first */ | ||
| 49 | bool fastscan_base; /* the list starts with fastscan pages */ | ||
| 50 | } StageCtx; | ||
| 51 | |||
| 52 | static bool | ||
| 53 | 343 | stage_page(PrismPostingChainPos *pos, void *state) | |
| 54 | { | ||
| 55 | 343 | StageCtx *ctx = state; | |
| 56 | 343 | const PrismPostingPageOpaque *op = prism_posting_opaque(pos->page); | |
| 57 | 343 | char *content = prism_posting_page_content(pos->page, ctx->dim); | |
| 58 | |||
| 59 | 343 | prism_posting_check_count(pos->blkno, pos->page, ctx->dim); | |
| 60 | |||
| 61 | /* | ||
| 62 | * A list built, converted or split as fastscan starts with its fastscan | ||
| 63 | * pages, and inserts only ever append AoS pages after them. So the first | ||
| 64 | * fastscan page ends the walk: before any AoS page it means the list is | ||
| 65 | * already converted, and after one the chain is not one we write. | ||
| 66 | */ | ||
| 67 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 335 times.
|
343 | if (op->flags & PRISM_POSTING_PAGE_FASTSCAN) |
| 68 | { | ||
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (ctx->aos_seen) |
| 70 | ✗ | vs_error( | |
| 71 | VS_EXTENSION_NAME | ||
| 72 | ": posting list %u has a fastscan page (block %u) after " | ||
| 73 | "AoS ones; the index may be corrupted -- REINDEX it", | ||
| 74 | ctx->head, | ||
| 75 | pos->blkno); | ||
| 76 | 8 | ctx->fastscan_base = true; | |
| 77 | 8 | return false; | |
| 78 | } | ||
| 79 |
3/3✓ Branch 0 taken 325 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4 times.
|
335 | if (!prism_posting_is_reference_only_page(op->flags, ctx->dim)) |
| 80 | 327 | ctx->aos_seen = true; | |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 4478 times.
✓ Branch 1 taken 335 times.
|
4813 | for (uint32_t i = 0; i < op->entry_count; i++) |
| 83 | { | ||
| 84 | 4466 | PrismPostingEntryHeader *src = | |
| 85 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4453 times.
|
4478 | prism_posting_entry_at(content, i, ctx->dim); |
| 86 | |||
| 87 | /* | ||
| 88 | * Leave behind the entries VACUUM has marked dead. Nothing here | ||
| 89 | * carries a dead AoS entry's state into the fresh fastscan chain -- | ||
| 90 | * the destination's tombstone_mask words start at zero, same as any | ||
| 91 | * freshly built page -- so copying the entry would resurrect it as | ||
| 92 | * live. The head's live_count, which the builder stamps from what it | ||
| 93 | * was given, would be wrong by the same number. | ||
| 94 | */ | ||
| 95 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 4465 times.
|
4478 | if (src->meta.flags & PRISM_POSTING_FLAG_DELETED) |
| 96 | 13 | continue; | |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4463 times.
|
4465 | if (ctx->total_entries >= ctx->entries_cap) |
| 99 | { | ||
| 100 | 2 | ctx->entries_cap *= 2; | |
| 101 | 2 | ctx->staged = vs_realloc( | |
| 102 | ✗ | ctx->staged, ctx->entries_cap * sizeof(StagedEntry)); | |
| 103 | 2 | ctx->all_bits = vs_realloc( | |
| 104 | ✗ | ctx->all_bits, | |
| 105 | ✗ | ctx->entries_cap * (size_t)ctx->packed_bytes); | |
| 106 | } | ||
| 107 | |||
| 108 | 4465 | StagedEntry *dst = &ctx->staged[ctx->total_entries]; | |
| 109 | |||
| 110 | 4465 | dst->tid = src->meta.tid; | |
| 111 | 4465 | dst->f_add = src->f_add; | |
| 112 | 4465 | dst->f_rescale = src->f_rescale; | |
| 113 | 4465 | dst->f_error = src->f_error; | |
| 114 | 4465 | memcpy(ctx->all_bits + (size_t)ctx->total_entries * ctx->packed_bytes, | |
| 115 | 4465 | src->bits, | |
| 116 | 4465 | ctx->packed_bytes); | |
| 117 | 4465 | ctx->total_entries++; | |
| 118 | } | ||
| 119 | |||
| 120 | 6 | return true; | |
| 121 | } | ||
| 122 | |||
| 123 | BlockNumber | ||
| 124 | 332 | prism_posting_convert_to_fastscan( | |
| 125 | VsStorage *storage, | ||
| 126 | BlockNumber aos_head, | ||
| 127 | Dimension dim, | ||
| 128 | bool *has_fastscan_base) | ||
| 129 | { | ||
| 130 | 332 | *has_fastscan_base = false; | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 332 times.
|
332 | if (aos_head == InvalidBlockNumber) |
| 132 | ✗ | return InvalidBlockNumber; | |
| 133 | 332 | uint32_t packed_bytes = VS_RABITQ_BYTES(dim); | |
| 134 | |||
| 135 | /* The cluster id from the AoS head, then its encode reference. */ | ||
| 136 | 332 | Page first_page = vs_storage_read_page(storage, aos_head); | |
| 137 | 332 | uint32_t cluster_id = prism_posting_opaque(first_page)->cluster_id; | |
| 138 | 332 | vs_storage_release_page(storage, aos_head); | |
| 139 | |||
| 140 | 332 | float *pt_centroid = vs_alloc(dim * sizeof(float)); | |
| 141 | 332 | prism_posting_reference_read(storage, aos_head, dim, pt_centroid); | |
| 142 | |||
| 143 | 324 | PrismPostingBuilder builder; | |
| 144 | 332 | prism_posting_builder_init_fastscan( | |
| 145 | &builder, storage, NULL, dim, cluster_id, NULL, pt_centroid); | ||
| 146 | |||
| 147 | 332 | StageCtx ctx = { | |
| 148 | .dim = dim, | ||
| 149 | .packed_bytes = packed_bytes, | ||
| 150 | .entries_cap = 256, | ||
| 151 | .head = aos_head, | ||
| 152 | }; | ||
| 153 | 332 | ctx.staged = vs_alloc(ctx.entries_cap * sizeof(StagedEntry)); | |
| 154 | 332 | ctx.all_bits = vs_alloc(ctx.entries_cap * (size_t)packed_bytes); | |
| 155 | |||
| 156 | 332 | prism_posting_chain_walk(storage, aos_head, stage_page, &ctx); | |
| 157 | |||
| 158 | /* The builder writes nothing until it gets entries, so leaving it | ||
| 159 | * unfinished leaves the index untouched. */ | ||
| 160 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 324 times.
|
332 | if (ctx.fastscan_base) |
| 161 | { | ||
| 162 | 8 | *has_fastscan_base = true; | |
| 163 | 8 | vs_free(ctx.staged); | |
| 164 | 8 | vs_free(ctx.all_bits); | |
| 165 | 8 | prism_posting_builder_cleanup(&builder); | |
| 166 | 8 | vs_free(pt_centroid); | |
| 167 | 8 | return InvalidBlockNumber; | |
| 168 | } | ||
| 169 | |||
| 170 | /* Feed staged entries to the fastscan builder */ | ||
| 171 |
2/2✓ Branch 0 taken 4465 times.
✓ Branch 1 taken 324 times.
|
4789 | for (uint32_t i = 0; i < ctx.total_entries; i++) |
| 172 | { | ||
| 173 | 4465 | prism_posting_builder_add_encoded( | |
| 174 | &builder, | ||
| 175 | 12 | ctx.staged[i].tid, | |
| 176 | 12 | ctx.staged[i].f_add, | |
| 177 | 12 | ctx.staged[i].f_rescale, | |
| 178 | 4465 | ctx.staged[i].f_error, | |
| 179 | 4465 | ctx.all_bits + (size_t)i * packed_bytes); | |
| 180 | } | ||
| 181 | |||
| 182 | 324 | vs_free(ctx.staged); | |
| 183 | 324 | vs_free(ctx.all_bits); | |
| 184 | |||
| 185 | 324 | BlockNumber result = prism_posting_builder_finish(&builder); | |
| 186 | 324 | prism_posting_builder_cleanup(&builder); | |
| 187 | 324 | vs_free(pt_centroid); | |
| 188 | |||
| 189 | 324 | return result; | |
| 190 | } | ||
| 191 |