0
0
Supabasecloud~10 mins

Connection pooling with PgBouncer in Supabase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Connection pooling with PgBouncer
Client sends DB request
PgBouncer receives request
Check for free DB connection
Use free connection
Send request to DB
DB processes request
Return result to client
Connection returned to pool
PgBouncer manages a pool of database connections. It reuses free connections for new client requests or queues requests if none are free, improving efficiency.
Execution Sample
Supabase
Client -> PgBouncer -> DB
PgBouncer pools connections
Reuse or queue requests
This flow shows how PgBouncer handles client requests by reusing or queuing connections to the database.
Process Table
StepClient RequestPgBouncer ActionDB Connection StateResult Sent to Client
1Request 1Assign free connection1 connection in useNo
2Request 2Assign free connection2 connections in useNo
3Request 3No free connection, queue request2 connections in use, 1 queuedNo
4DB finishes Request 1Return connection to pool1 connection in use, 1 freeSend result for Request 1
5Process queued Request 3Assign freed connection2 connections in useNo
6DB finishes Request 2Return connection to pool1 connection in use, 1 freeSend result for Request 2
7DB finishes Request 3Return connection to pool0 connections in use, 2 freeSend result for Request 3
💡 All requests processed, connections returned to pool
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
DB Connections In Use01221210
Queued Requests00011000
Key Moments - 3 Insights
Why does PgBouncer queue a request instead of opening a new connection?
PgBouncer limits the number of active DB connections to avoid overload. When all connections are busy (see Step 3 in execution_table), it queues requests to wait for a free connection.
What happens when a DB connection finishes processing a request?
The connection is returned to the pool (Step 4), making it available for queued or new requests, improving resource reuse.
How does connection pooling improve performance?
By reusing existing connections instead of opening new ones for each request, PgBouncer reduces overhead and speeds up request handling (visible in Steps 1, 2, 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many connections are in use after Step 2?
A2
B1
C0
D3
💡 Hint
Check the 'DB Connection State' column for Step 2 in execution_table
At which step does PgBouncer queue a request because no connections are free?
AStep 1
BStep 3
CStep 5
DStep 7
💡 Hint
Look for 'queue request' in the 'PgBouncer Action' column in execution_table
If PgBouncer had 3 connections instead of 2, what would happen at Step 3?
ARequest 2 would be queued
BRequest 3 would still be queued
CRequest 3 would be assigned a free connection immediately
DNo requests would be processed
💡 Hint
Refer to how connection limits affect queuing in execution_table and variable_tracker
Concept Snapshot
Connection pooling with PgBouncer:
- PgBouncer keeps a set number of DB connections open.
- Client requests reuse these connections if free.
- If none free, requests queue until a connection frees.
- This reduces DB load and speeds up handling.
- Pool size limits max concurrent DB connections.
Full Transcript
Connection pooling with PgBouncer works by managing a fixed number of database connections. When a client sends a request, PgBouncer checks if a free connection is available. If yes, it assigns the connection to the request and sends it to the database. If no connections are free, PgBouncer queues the request until a connection becomes available. When the database finishes processing a request, the connection returns to the pool for reuse. This process reduces the overhead of opening new connections and improves performance by reusing existing ones. The execution table shows how connections are assigned, queued, and freed step-by-step. Key moments include understanding why requests queue and how connections return to the pool. The visual quiz tests understanding of connection usage and queuing behavior. Overall, PgBouncer helps manage database load efficiently by pooling connections.