Skip to main content

Long-running tasks and visibility timeouts

A visibility timeout reserves a delivery while a consumer processes it. If the consumer disappears, expiry lets another consumer recover the work. The SDK normally extends active deliveries automatically, so processing can outlive the original timeout without selecting a timeout as long as the slowest possible task.

Automatic extension requires database communication. It is not a processing deadline, and it cannot stop application code that ignores cancellation.

Process beyond the initial timeoutโ€‹

The following handler bodies fit the handler examples configured with a 30-second visibility timeout and automatic extension enabled. Each waits for 45 seconds to make extension observable, or stops when the delivery's cancellation signal fires. The wait stands in for cancellation-aware work.

func(ctx context.Context, message *postgremq.Message) {
select {
case <-time.After(45*time.Second):
fmt.Println("Finished", message.ID)
case <-ctx.Done():
return
}
}

Keep the connection open beyond the demonstration's duration; replace the consumer example's 30-second run timer with a longer interval. With a healthy database, another worker must not receive this delivery merely because the initial 30 seconds passed. A cancellation path does not establish that the work succeeded; normal handler cancellation returns unfinished work according to the retry policy.

Use an application deadline as well when work must finish within a fixed budget. Automatic extension can otherwise keep renewing a handler that makes no useful progress.

Choose a timeout and renewal marginโ€‹

The timeout balances recovery after worker loss against tolerance for latency and interruptions. A very long timeout delays abandoned-work recovery. A very short timeout increases renewal pressure and gives pool waits or database contention less margin.

The default visibility timeout is 30 seconds in all three SDKs. Automatic extension is enabled for both stream and handler consumers. The default renewal threshold is 0.5; it schedules renewal around halfway through the configured interval. Keep application and database clocks synchronized and test under realistic latency.

A transiently busy renewal is not itself a loss of ownership. If the SDK cannot confirm ownership before its deadline, it signals the application to stop. Do not continue external effects on the assumption that the database will eventually become reachable.

Manual extensionโ€‹

Use manual renewal only when the application can own its timing and failure handling. Disable automatic extension on the consumer with Go WithNoAutoExtension() plus a nonzero WithVT, Rust .auto_extend(false), or TypeScript autoExtension: { enabled: false }.

For a currently owned delivery named message, these calls set a new deadline 60 seconds from the database's current time:

newDeadline, err := message.SetVT(ctx, 60)
if err != nil { return err }
fmt.Println(newDeadline)

The values are durations from now, not additions to the old deadline. The lease must still be valid; manual extension does not reclaim an expired delivery. Treat failure as a reason to assess ownership and stop unsafe effects.

Do not mix manual extension with automatic extension casually. Automatic renewal retains its own configured interval and schedule: a later renewal can replace a longer manual deadline, and a shorter manual deadline is not a reliable way to reschedule the background actor.

Consumer acknowledgements explains completion, and Lease-loss troubleshooting covers diagnosing renewal failure. During shutdown, the connection normally continues extension while draining within its configured budget.