Skip to main content
Run expensive, wall-clock-driven work in a separate Cloud processor service. Do not run it inside the indexer’s batch transaction.
A query started from the batch handler shares the processor transaction. A lock wait, timeout, or serialization failure can roll back the whole batch and stop indexing until the query completes or fails.
This pattern is useful for:
  • leaderboards and rankings recalculated every few minutes;
  • materialized summaries and exports;
  • retention or cleanup jobs;
  • reconciliation against an external system.
It is not needed for lightweight calculations that naturally belong to one indexing batch.

Architecture

Use two named processor services that share the same Cloud Postgres add-on:
  1. The indexer writes canonical blockchain data.
  2. The background service opens its own database pool, waits for the next wall-clock interval, and runs the aggregate in its own transaction.
  3. An advisory lock prevents two copies of the job from overlapping.
  4. The job exposes Prometheus metrics so its runs and failures are observable.
Both services receive the Cloud-provided DB_URL. They run as separate processes, so an aggregation failure does not roll back the indexer’s batch.

Add a background command

Add a command for the job to commands.json:
commands.json
The deployment-level init command applies migrations before either processor service starts. The background command should not run migrations itself.

Declare both processor services

squid.yaml
Processor names must be unique. Each named processor runs and is billed as a separate service. The scale.processor.profile setting applies to the processor services in the deployment.

Implement the scheduler safely

Install the database and metrics dependencies used by the job:
The following skeleton uses one Postgres session for the advisory lock and transaction. Replace the aggregate query with your own:
src/jobs/aggregate.ts
READ COMMITTED is an example, not a universal default. Choose the weakest isolation level that preserves your aggregate’s correctness, and test it against concurrent indexer writes.

Production checklist

  • Make the query safe to retry.
  • Prevent overlapping runs with an advisory lock or equivalent mechanism.
  • Keep the job’s database pool separate from the indexer’s pool.
  • Set a statement timeout appropriate for the job.
  • Record success, failure, duration, and last-success metrics.
  • Alert when the last-success timestamp becomes stale.
  • Test the query on production-sized data before enabling the schedule.
  • Review the additional processor and database load in Billing and Usage.
If the job needs to replay blockchain history rather than aggregate existing database rows, follow Schema changes and backfills.