Skip to content

Instantly share code, notes, and snippets.

@ryogrid
Created September 7, 2025 13:20
Show Gist options
  • Save ryogrid/af4c9ce3fb89a9f196ecd2e2109b8fc6 to your computer and use it in GitHub Desktop.
Save ryogrid/af4c9ce3fb89a9f196ecd2e2109b8fc6 to your computer and use it in GitHub Desktop.
PGのシンボル全てLLMに解説させる計画の成果物の例

brin_bloom_add_value

Overview

Examines an index tuple and updates its bloom filter by adding a new value from a heap tuple, returning whether the bloom filter was modified.

Definition

Datum brin_bloom_add_value(PG_FUNCTION_ARGS)

Detailed Description

This function is a BRIN operator function that maintains bloom filter summaries for index ranges. It processes a new value from a heap tuple and determines if it needs to update the existing bloom filter:

  1. Initialization: If this is the first non-null value for the range, initializes a new bloom filter using calculated ndistinct and false positive rate parameters
  2. Hash computation: Computes a hash value for the new input using the appropriate hash function for the column's data type
  3. Filter update: Adds the hash value to the bloom filter and determines if the filter was actually modified
  4. State management: Updates the BrinValues structure with the new or modified bloom filter

The function handles the transition from all-null state to having actual data and ensures the bloom filter is properly maintained throughout the life of the BRIN index.

Parameters / Member Variables

  • PG_FUNCTION_ARGS: Standard PostgreSQL function argument structure containing:
    • bdesc: BRIN descriptor with index metadata
    • column: BrinValues structure containing current bloom filter state
    • newval: New datum value to potentially add to the bloom filter
    • isnull: Boolean indicating if the new value is null (used for assertions)

Dependencies

  • Functions called/Symbols referenced:
    • brin_bloom_get_ndistinct
    • BloomGetFalsePositiveRate
    • bloom_init
    • bloom_get_procinfo
    • bloom_add_value
    • FunctionCall1Coll
    • DatumGetUInt32
    • PG_DETOAST_DATUM
  • Called from (representative examples):
    • This is a BRIN operator function called by the BRIN indexing infrastructure

Notes and Other Information

  • This is a PostgreSQL internal function that follows the PG_FUNCTION_ARGS convention for operator functions
  • The function is designed to be called during BRIN index construction and maintenance
  • Uses assertions to verify that null values are not processed (Assert(!isnull))
  • Handles TOAST decompression for existing bloom filters that may be stored compressed
  • The return value indicates whether the bloom filter summary was actually updated, which is important for BRIN's change tracking
  • The function manages memory by ensuring the updated bloom filter is properly stored back in the BrinValues structure

brin_bloom_consistent

Overview

Determines whether scan keys are consistent with an index tuple's bloom filter, used during BRIN index scans to eliminate page ranges that cannot contain matching values.

Definition

Datum brin_bloom_consistent(PG_FUNCTION_ARGS)

Detailed Description

This function is a BRIN operator function that implements the consistency check for bloom filter-based BRIN indexes. It evaluates whether a given page range (represented by its bloom filter summary) might contain tuples matching the scan conditions:

  1. Filter extraction: Retrieves the bloom filter from the BrinValues structure, handling TOAST decompression if necessary
  2. Multi-key evaluation: Iterates through all scan keys, applying the appropriate strategy for each
  3. Hash-based lookup: For equality strategies, computes the hash of the search value and checks if it might be contained in the bloom filter
  4. Early termination: Stops processing as soon as any key fails the consistency check (short-circuit evaluation)

The function assumes all keys match initially and looks for evidence that the page range can be eliminated from the scan.

Parameters / Member Variables

  • PG_FUNCTION_ARGS: Standard PostgreSQL function argument structure containing:
    • bdesc: BRIN descriptor with index metadata
    • column: BrinValues structure containing the bloom filter for this range
    • keys: Array of ScanKey structures representing the search conditions
    • nkeys: Number of scan keys to process

Dependencies

  • Functions called/Symbols referenced:
    • bloom_get_procinfo
    • bloom_contains_value
    • FunctionCall1Coll
    • DatumGetUInt32
    • PG_DETOAST_DATUM
    • BloomEqualStrategyNumber
    • PROCNUM_HASH
  • Called from (representative examples):
    • This is a BRIN operator function called by the BRIN scanning infrastructure

Notes and Other Information

  • This is a PostgreSQL internal function following the PG_FUNCTION_ARGS convention for operator functions
  • Currently only supports BloomEqualStrategyNumber strategy (equality comparisons)
  • Uses assertions to ensure NULL keys are not processed, as they should be filtered out by bringetbitmap
  • The function implements a conservative approach: it may return false positives (indicating a match when none exists) but never false negatives
  • Early termination optimization: stops checking additional keys once any key fails the consistency check
  • Error handling for unsupported strategies with elog(ERROR, ...)
  • The bloom filter's probabilistic nature means this function may indicate matches for ranges that don't actually contain the sought value, but it will never miss ranges that do contain the value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment