CodeRigor

LESSON

Own pools and bounded query contexts

Reuse one bounded pool and preserve parent cancellation/deadlines when setting query timeouts.

Create a connection pool at the application boundary and reuse it. Opening a new pool per query hides resource ownership and can create unnecessary connections. A bounded pool is useful, but its exact size is a workload decision rather than a universal production default.

A query context should preserve its parent’s cancellation, values and earlier deadline. Adding a timeout must not resurrect canceled work by switching to context.Background. The pure function exercises that relationship; the actual lab observes a canceled PostgreSQL query and verifies the pool remains usable.

Own pools and bounded query contexts
start / 3
Reuse one bounded pool and preserve parent cancellation/deadlines when setting query timeouts.
Own pools and bounded query contexts
Reuse one bounded pool and preserve parent cancellation/deadlines when setting query timeouts.

The lab configures MaxConns2, MinConns0, a five-second connection timeout, a five-minute maximum connection lifetime and thirty-second idle lifetime. Those values are explicit teaching settings. They are not a measured concurrency capacity or a production sizing recommendation.

Release query rows promptly and check rows.Err after iteration. Cancel child contexts when finished. The local cancellation probe uses pg_sleep only inside the owned database; no hosted exercise opens a socket or runs that query.

Go
package main
import ("fmt";"context";"time")
func main(){
parent,cancel:=context.WithCancel(context.Background());cancel();child,stop:=context.WithTimeout(parent,time.Second);defer stop();fmt.Println(child.Err()==context.Canceled)
}

A deadline on a child cannot extend an earlier parent deadline. A canceled parent yields a canceled child. Parent values remain available for contextual information. These are observable Go context semantics that can be checked without a database.

The actual application has one pool, finite contexts and deliberate Close ownership. Query cancellation may discard a connection, so the important postcondition is that the pool can perform subsequent work, not that the same physical connection survives.

Review source, target and evidence
start / 3
A successful migration includes typed values, actual database behavior and explicit cleanup.
Review source, target and evidence
A successful migration includes typed values, actual database behavior and explicit cleanup.
Go
package main
import ("fmt";"context";"time")
func main(){
parent,cancel:=context.WithTimeout(context.Background(),time.Second);defer cancel();child,stop:=context.WithTimeout(parent,2*time.Second);defer stop();a,_:=parent.Deadline();b,_:=child.Deadline();fmt.Println(a.Equal(b))
}

Implement the child-context helper and preserve cancellation/deadlines. Next reconcile real source and target rows and run the complete local application.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Optional database overview. The authored lessons and real local lab teach the exact schema, migration and application-lifetime boundaries used here.