-
The Postgres middleware acquires a connection from default pool for each (authorized) http request and attaches the connection to the request context conn, err := defaultPool.Acquire(r.Context())
// ... ...
ctx = context.WithValue(ctx, pgo.PgConnCtxKey, conn)
ctx = context.WithValue(ctx, pgo.PgRoleCtxKey, pgRole) In the request context, the middleware also sets Postgres role using which queries on Postgres are performed. The role is derived from authorization methods:
What are the performance considerations of attaching pgxpool.Conn in EACH request context? Then the http handlers obtain connection with Line 49 in d4c7870 This may not be optimal, because setRoleQuery := fmt.Sprintf("SET ROLE %s;", role)
setReqClaimsQuery := fmt.Sprintf("SET request.oidc.claims TO '%s';", escapedClaimsJSON)
combinedQuery := setRoleQuery + setReqClaimsQuery
_, execErr := conn.Exec(context.Background(), combinedQuery) Can we batch this Finally, pgxutil helpers in pgo by default releases the connection with defer conn.Release() it can be prevented by supplying the optional How'd we optimise connection acquisition and release? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
From @jackc himself (via email), quoted verbatim
|
Beta Was this translation helpful? Give feedback.
-
The context operations are very cheap. The only thing that I see could be a little more expensive than desired is that the connection is held for the entire duration of the request. If your handlers were doing other non-database things that could be undesirable.
Yes. It would be possible. But the interface may not be as easy to use. And I'm not sure off the top of my head what the state of the connection would be if the query bundled with the |
Beta Was this translation helpful? Give feedback.
The context operations are very cheap. The only thing that I see could be a little more expensive than desired is that the connection is held for the entire duration of the request. If your handlers were doing other non-database things that could be undesirable.
Yes. It would be possible. But the interface may not be as easy to use. And I'm not sure off the top of my head what the state of the connection would be if the query bundled with the
set
queries had an error. You'd want to be very sure that those …