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.
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.
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.
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.