一键导入
writing-queries
// Writes and modifies sqlc queries for the xmtpd PostgreSQL database. Use when creating, editing, or reviewing .sql query files in pkg/db/sqlc/, when the user mentions sqlc, database queries, or adding new database operations.
// Writes and modifies sqlc queries for the xmtpd PostgreSQL database. Use when creating, editing, or reviewing .sql query files in pkg/db/sqlc/, when the user mentions sqlc, database queries, or adding new database operations.
| name | writing-queries |
| description | Writes and modifies sqlc queries for the xmtpd PostgreSQL database. Use when creating, editing, or reviewing .sql query files in pkg/db/sqlc/, when the user mentions sqlc, database queries, or adding new database operations. |
sqlc.yaml at repo rootpkg/db/sqlc/*.sqlpkg/db/queries/dev/gen/allQueries are grouped by domain into separate .sql files (e.g., envelopes.sql, payer_reports.sql, congestion.sql). New queries go in the file matching their domain; create a new file only if no existing file fits.
Four types used in this project:
:one — single row (InsertGatewayEnvelope, GetLatestSequenceId):many — multiple rows (SelectVectorClock, FetchPayerReports):exec — no return (InsertPayerLedgerEvent, SetReportAttestationStatus):execrows — returns affected count (DeleteStagedOriginatorEnvelope, InsertAddressLog)Format: -- name: VerbNoun :type in PascalCase.
Verbs observed: Select, Get, Insert, Delete, Update, Set, Find, Fetch, Build, Count, Clear, Increment, Revoke.
Examples: FindOrCreatePayer, DeleteExpiredEnvelopesBatch, IncrementOriginatorCongestion.
Three styles, in order of preference:
@named_param — preferred for most queries (vast majority of codebase)sqlc.narg('name') — for nullable/optional filter parameters (generates sql.NullXXX in Go)sqlc.arg(name) — when used alongside positional $1 in same query, or when explicit naming is needed with type castsPositional $1 — only in configuration.sql and mixed with sqlc.arg() in congestion.sql.
-- for non-obvious logicInsertGatewayEnvelope pattern:
WITH m AS (INSERT ... RETURNING 1),
b AS (INSERT ... RETURNING 1)
SELECT ...
FindOrCreatePayer pattern:
WITH ins AS (
INSERT ... ON CONFLICT DO NOTHING RETURNING id
), u AS (
SELECT id FROM ins
UNION ALL
SELECT id FROM table WHERE ...
)
SELECT id FROM u LIMIT 1
Idempotent inserts: InsertNodeInfo, InsertOrIgnorePayerReport.
Increment counters (IncrementOriginatorCongestion, IncrementUnsettledUsage), conditional updates with WHERE clause (InsertAddressLog).
Return inserted/affected data. RETURNING * (InsertStagedOriginatorEnvelope), RETURNING id (FindOrCreatePayer).
@param::TYPE[] with ANY or unnest:
ANY(@topics::BYTEA[]) (SelectNewestFromTopics)unnest(@addresses::TEXT[]) for batch operationssqlc.narg('name')::TYPE IS NULL OR condition
Used in FetchPayerReports.
@param::BIGINT = 0 OR condition
Used in SumOriginatorCongestion, GetPayerUnsettledUsage.
LIMIT NULLIF(@row_limit::INT, 0)
0 = no limit.
Per (topic, originator): SelectGatewayEnvelopesByTopics uses CROSS JOIN LATERAL for per-(topic, originator) index probes on gem_topic_orig_seq_idx, with a second per-originator LATERAL for blob join cache locality. Callers must include ALL originators in cursor arrays — use FillMissingOriginators(vc, allOriginators) on the VectorClock before SetVectorClockByTopics.
Per originator: SelectGatewayEnvelopesByOriginators uses CROSS JOIN LATERAL for per-originator cursor-based pagination.
Concurrent-safe batch processing: GetNextAvailableNonce, DeleteExpiredEnvelopesBatch.
WITH to_delete AS (
SELECT ... LIMIT @batch_size FOR UPDATE SKIP LOCKED
)
DELETE ... USING to_delete ... RETURNING ...
COUNT(...) OVER (PARTITION BY ...) in FetchPayerReports attestations_count.
Newest per group: SelectNewestFromTopics.
pg_advisory_xact_lock(), pg_try_advisory_xact_lock().
SELECT * FROM function_name(params) or SELECT function_name(params).
COALESCE(SUM(amount), 0)::BIGINT
Used in GetPayerBalance, SumOriginatorCongestion.
encode(inbox_id, 'hex'), decode(@inbox_id, 'hex') in identity_updates.sql.
InsertSavePoint, InsertSavePointRelease, InsertSavePointRollback in partitions.sql.
dev/gen/all to regenerate Go codepkg/db/queries/ compilesdev/test for integration testsUse when creating, modifying, or reviewing E2E tests in pkg/e2e/tests/. Triggers on "e2e test", "add e2e test", "new e2e test", or when working with files under pkg/e2e/tests/.
Use when creating branches, committing changes, pushing, or opening pull requests. Triggers on "make a PR", "open a PR", "create a branch", "push this", "commit and push", or any request to ship code to GitHub.
Creates and modifies PostgreSQL database migrations for xmtpd using golang-migrate. Use when adding or altering tables, columns, indexes, functions, triggers, constraints, or partitions, or when the user mentions migrations or schema changes.
Use when writing, modifying, or reviewing .go files, implementing new Go functions or packages, or when Go code style and conventions are relevant.