← Back to Pull requests

Add lower_columns index option for case-insensitive lookups #171

Closed
alan-lee-12 closed this pull request without merging on Nov 20, 2026 20:12 UTC
View on GitHub
alan-lee-12 alan-lee-12 commented on Nov 18, 2026 22:14 UTC
Author

What

Adds a typed index option lower_columns to MessageOptions.Index:

// columns (entries of `columns`) to index as lower(column), for
// case-insensitive equality lookups. BTREE only.
repeated string lower_columns = 7;

Listed entries of columns are wrapped in lower() in the generated index expression. Columns is still populated as before; the expression goes through the existing OverrideExpression path, using the same string-breakout form as partial_deleted_at_is_null, so io.ColumnName applies the top-level or nested column prefix at runtime.

Example (example/models/food/v1/food.proto, PastaIngredient):

option (pgdb.v1.msg).indexes = {
  name: "pasta_id_ci_alive_only"
  method: INDEX_METHOD_BTREE
  columns: ["tenant_id", "pasta_id"]
  lower_columns: ["pasta_id"]
  partial_deleted_at_is_null: true
};

Generated literal:

Columns:            []string{io.ColumnName("tenant_id"), io.ColumnName("pasta_id")},
OverrideExpression: "" + io.ColumnName("tenant_id") + ", lower(" + io.ColumnName("pasta_id") + ")",
WherePredicate:     "" + io.ColumnName("deleted_at") + " IS NULL",

index2sql output (partitioned table, so no CONCURRENTLY):

USING
  BTREE
(
pb$tenant_id, lower(pb$pasta_id)
)
WHERE pb$deleted_at IS NULL

With a nested prefix (Nested("11$")) the expression renders as pb$tenant_id, lower(pb$11$pasta_id). Generated column names are lower snake case or numeric behind the pb$ prefix, so the unquoted identifiers are valid. Postgres stores them as lower("pb$pasta_id") in pg_indexes.

Validation

Generation panics, in the same way as the existing "could not find field for index" case, when:

  • a lower_columns entry is not listed in columns

  • method is not INDEX_METHOD_BTREE

  • bit_hamming_ops is set

  • the target is not a singular, text-backed string field. Oneof names, repeated fields, non-string types and MESSAGE_BEHAVIOR_INET_ADDR strings (stored as inet) are rejected.
  • Existing options behave the same as before. Indexes without lower_columns generate identical code.

    Safe query builder

    getSafeFields (internal/pgdb/pgdb_query_builder.go) skips a column of an index when that index lowers it, so a lower(x) index adds no plain-column safe operators (Eq/Gt/Lt/IsNotEmpty, ...) for x. Other columns of the same index, such as tenant_id, still get operators as before. The existing logic grants operators per column with no btree-prefix ordering, so this is a per-column skip. Lowered columns are carried on the generator-internal indexContext.LoweredColumns. The runtime pgdb_v1.Index struct is unchanged.

    The example adds PastaIngredient.label, which only a lower_columns index covers. The builder has no safe Label() method, but Unsafe().Label() is still there. pasta_id, which plain indexes also cover, keeps its safe operators, and its generated code is unchanged.

    Tests

  • internal/pgdb/pgdb_index_test.go:

  • - TestValidateLowerColumns covers the accept path and the not-in-columns, non-BTREE and bit_hamming_ops failures.
    - TestExtraIndexesLowerColumns builds a pgs AST from an in-memory descriptor set and runs extraIndexes on it. It covers a top-level column and a dotted nested column. The nested case profile.handle produces lower(" + io.ColumnName("8$handle") + "). It also checks that each of these is rejected: oneof name, int64, repeated string, inet-backed string and google.protobuf.StringValue.
  • example/models/food/v1/lower_columns_test.go:

  • - TestLowerColumnsIndex checks Columns, OverrideExpression and WherePredicate for V13 and V17, the nested prefix, and the exact IndexSchema DDL.
    - TestLowerColumnsIndexPostgres applies CreateSchema to a real Postgres and checks that all three lower() indexes exist.
    - TestLowerColumnsSafeOperators checks that there is no safe Label(), that Unsafe().Label() exists, and that PastaId keeps Eq/Gt/Gte/Lt/Lte/IsNotEmpty.
  • TestSchemaFoodPasta: the partial-index count for PastaIngredient goes from 1 to 2.
  • Commands run:

  • make generate and make example: regenerated pgdb/v1/pgdb.pb.go and the food example. The only example changes are in food, and running both again produces no further diff.

  • make lint (buf lint): pass.

  • LANG=C.UTF-8 make test: everything passes except internal/pgtest TestPostgreSQLWithConfig ("did not find PostgreSQL executables installed"). That failure comes from the local environment: the test looks for Postgres at fixed install paths. This change doesn't touch it.

  • I couldn't run golangci-lint locally because the local binary crashes under Go 1.27. CI will run it.
  • Notes for reviewers

  • Indexes with dropped: true skip lower_columns validation, because a drop only needs the index name.

  • google.protobuf.StringValue fields are rejected. Only plain string fields are supported.

  • Lowered columns are not offered as indexed plain-column operators in the generated safe query builder. Callers write a raw lower(col) = lower(?) predicate.
  • Motivation: FDE-443 (case-insensitive exact username lookup on a large table without a backfill).

    🤖 Generated with Claude Code

    Diff: 587 lines (+577 / -10) across 10 files Reviewer: None requested Review duration: 2758m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.66) × (1 − Review Confidence (0.58)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.66 · 587 lines diff across 10 files
    0.66
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 10%
    57%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.382
    Shallow Review Depth shallow_review_depth
    0.289
    No Reviewer Assigned no_reviewer_assigned
    0.120
    Real PR Conversation Timeline
    1 comments
    "Review active on #171"
    general weight: 0.35
    Reviewers
    None requested
    Assignees
    alan-lee-12 alan-lee-12
    Labels
    risk: low confidence: 58.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 66.3% (HIGH) · Higher = larger blast radius
    66.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 58.0% · Lower = rushed or shallow review
    58.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.9% · Safe to merge
    27.9%