← Back to Pull requests

feat: add cheap Message.PKSK() key helper #145

Merged
loganintech merged 5 files (410 lines) on Jul 01, 2026 19:24 UTC
View on GitHub
loganintech loganintech commented on Jul 01, 2026 19:15 UTC
Author

Motivation

Message only exposed the full Record() for pulling a row's primary key. Record() marshals the proto and builds every column — FTS vectors, search data, pb_data — which is far more work than a caller who just wants the pb$pksk value needs.

Because there was no cheap key accessor, consumers reached for the Dynamo PartitionKey()/SortKey() methods instead. That's both a layering violation (Postgres code depending on the Dynamo generator) and wrong for sharded objects: the Dynamo partition key embeds a shard id (…:<tenant>:<shard>) that Postgres does not store (Postgres keeps …:<tenant>), so PartitionKey()|SortKey() never matches the pb$pksk column for a sharded row.

Change

Add a generated PKSK() string method that builds "pb$pk|pb$sk" directly from the key fields:

func (m *pgdbMessagePasta) PKSK() string {
    var sb strings.Builder
    sb.Reset()
    _, _ = sb.WriteString("models_food_v1_pasta")
    _, _ = sb.WriteString(":")
    _, _ = sb.WriteString(m.self.GetTenantId())
    _, _ = sb.WriteString(":")
    _, _ = sb.WriteString(m.self.GetId())
    cfv2 := sb.String()
    sb.Reset()
    _, _ = sb.WriteString("example")
    cfv3 := sb.String()
    return cfv2 + "|" + cfv3
}

  • Cheap: pure string concatenation of the key fields — no proto.Marshal, no FTS/search computation.

  • Authoritative: reuses the same pk/sk field builders Record() uses, so it always equals Record()["pb$pk"] + "|" + Record()["pb$sk"] — which is exactly what the pb$pksk generated column (pb$pk || '|' || pb$sk) evaluates to. Correct for composite keys, sk_const, and partitioned tables.

  • Error-free: key building can't fail (unlike Record()), so the signature is a plain string.

  • Dialect-independent: the key columns don't vary by dialect; the helper returns the same value for V13 and V17.
  • Nested-only messages don't get it

    Rather than live on the Message interface, PKSK() lives on a new optional interface:

    type PrimaryKeyer interface {
        PKSK() string
    }
    

    Nested-only messages are stored inline (JSONB in a parent row) and have no primary key, so the method simply isn't generated for them — they don't implement PrimaryKeyer. This avoids emitting a useless PKSK() string { return "" } on every nested-only type. Consumers type-assert Message → PrimaryKeyer before calling.

    Implementation

  • pgdb/v1/message.go — new PrimaryKeyer optional interface (not on Message).

  • internal/pgdb/pgdb_message.go — surface the pk/sk field contexts to the template (findFieldByDBName).

  • internal/pgdb/templates/message.tmpl — emit the method only when the message has a primary key.

  • Regenerated example *.pb.pgdb.go.

  • example/models/food/v1/pksk_test.go — asserts PKSK() == Record pb$pk|pb$sk across key shapes and both dialects, and that nested-only messages do not implement PrimaryKeyer.
  • Compatibility

    PKSK() is additive and on a separate optional interface, so existing Message implementers are unaffected. Downstream (c1) will bump this dependency, re-run codegen, and switch its xpgdb.PKSK helper to type-assert PrimaryKeyer and call PKSK().

    🤖 Generated with Claude Code

    Diff: 410 lines (+307 / -103) across 5 files Reviewer: None requested Review duration: 9m
    All Vouch review confidence checks passed
    Residual risk 0.43 = Change Risk (0.67) × (1 − Review Confidence (0.35)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.67 · 410 lines diff across 5 files
    0.67
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 88% · Reviewer familiarity: 10%
    35%
    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.360
    Shallow Review Depth shallow_review_depth
    0.344
    No Reviewer Assigned no_reviewer_assigned
    0.120
    Rapid Merge rapid_merge
    0.100
    Real PR Conversation Timeline
    1 comments
    "Review active on #145"
    general weight: 0.35
    Reviewers
    None requested
    Assignees
    loganintech loganintech
    Labels
    risk: medium confidence: 35.1% 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.9% (HIGH) · Higher = larger blast radius
    66.9%
    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: 35.1% · Lower = rushed or shallow review
    35.1%
    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: 43.4% · Moderate risk
    43.4%