Keep the useful bits, human.
Beginner field notes
- A connection pool manages channels an application can borrow to talk to a database. It shares reusable connections rather than creating an unlimited supply.
- A maximum is a cap, not a promise that every slot is already open. The cited pg.Pool starts empty and creates connections lazily.
- When pg.Pool is full and every client is borrowed, the next acquisition waits for a returned client. An idle client at the cap can still be borrowed.
See a worked example
Track two reusable connections and two different waiting exits
Sample only. Nothing here runs on your computer.
OFFLINE Lantern Archive pool. Capacity 2; healthy pre-opened C1,C2 start idle. Each read request needs exactly one exclusive connection; choose the lowest-numbered idle connection. No connection opens, closes, fails or is evicted during the trace; no retries or other writers.
Active waiters are FIFO. A healthy released connection goes immediately to the oldest active waiter. A waiter expires 5 logical units after arrival; expiry is processed before a same-time action, with no active expiry/release tie in this trace. A supplied cancellation removes only a waiting request. Acquisition expiry never cancels running work.
t0: A arrives and takes C1.
t1: B arrives and takes C2.
t2: C arrives and waits.
t3: D arrives and waits.
t4: D is canceled while waiting.
t5: A’s read explicitly completes successfully; A releases C1.
t6: E arrives and waits.
t7: F arrives and waits.
t8: B’s read explicitly completes successfully; B releases C2.
t12: F’s waiting deadline is explicitly reached.
t13: C’s read explicitly completes successfully; C releases C1.
t14: E’s read explicitly completes successfully; E releases C2.
All times are invented integer bookkeeping values, not measured durations or production timeout settings.t0: C1=A; C2 idle; queue empty.
t1: C1=A,C2=B; no idle connection.
t2: queue C, deadline t7.
t3: queue C,D. Both connections remain borrowed.
t4: queue C; canceled D never checks out.
t5: A releases C1; oldest waiter C immediately receives C1, before its waiting deadline.
t6: queue E, deadline t11.
t7: queue E,F; F deadline t12. C is already running, so its former waiting deadline does not cancel it.
t8: B releases C2; E receives C2 before F. Queue F.
t12: F expires without checkout; C1=C,C2=E still running.
t13: C1 becomes idle after C’s given successful completion/release.
t14: C2 becomes idle after E’s given successful completion/release. Both idle; queue empty.
Given completed reads: A,B,C,E; canceled D; wait-expired F. Maximum borrowed: 2; maximum waiting: 2; C waited 3 units and E waited 2; new connections created during this trace: 0.Track assignments and waiting identities separately from the explicitly supplied read outcomes. C reuses C1 and E reuses C2. A request that leaves the waiting queue never became a running query in this model. The five-unit rule constrains acquisition waiting, not the duration of an assigned read, and it grants no cancellation behavior in a real driver.
Visual
The reviewed description and complete diagram source are available here.
More of this diagram is available inside its frame. Focus the frame and use the arrow keys to pan.
Read the complete diagram source
This is the source for the reviewed diagram, not a command to run.
sequenceDiagram
participant A as Request A
participant P as Example pool: C1 and C2
participant C as Waiting request C
participant E as Waiting request E
A->>P: t0: check out C1
Note over P: t1: B checks out C2#59; both busy
C->>P: t2: wait for a connection
A->>P: t5: given successful read completion#59; release C1
P-->>C: Hand off the same C1
E->>P: t6: wait for a connection
Note over P: t8: B completes successfully and releases C2
P-->>E: Hand off C2 to oldest active waiter Sources and full explanation
Notes sources · Beginner
AI source review · 2026-10-17
Reviewed explanation
A database connection pool keeps track of connections an application can borrow, use and return. Reusing a healthy connection can avoid repeating connection setup for each request. It does not reuse the earlier query result, make every operation fast or make connections unlimited. In node-postgres, the checked-out client is the object through which an application uses its borrowed PostgreSQL connection.
Separate the pool’s total, idle and borrowed connections from the requests waiting to acquire one. A maximum is a cap: a library may create connections lazily and retire idle or broken ones. Reaching the cap is not itself exhaustion if an idle connection can still be checked out. When the cap is reached and all usable connections are borrowed, another acquisition must wait or fail according to the applicable policy. A cap on one pool also does not bound every other pool or process using the database.
A holder must finish using its connection and perform any required cleanup before returning it safely. Query errors do not remove the need to release or discard the client appropriately. Some libraries handle checkout and release for a single-query convenience operation, while a transaction may need several statements on the same connection. Forgetting to return clients can exhaust a pool; depending on timeout configuration, later acquisitions can fail or keep waiting. This is not a recommendation to increase the maximum as a substitute for finding a leak.
The cited pg.Pool documents FIFO acquisition waiting when full and busy. Our paper model also chooses FIFO, plus explicitly accepted cancellation and a five-logical-unit acquisition limit. Those cancellation rules are not a claim that pg.Pool provides a particular cancel method. The timeout applies only while waiting for checkout; it is not a query deadline and never cancels a holder’s database work. Every time and completion below is supplied, with no retry, connection failure, eviction, multiplexing or race hidden between rows.
The example starts with two healthy connections already open. C receives C1 after A’s explicitly successful read and release; E receives C2 after B’s. D withdraws while waiting and F reaches its waiting deadline without a checkout. C and E can hold their connections beyond five units because that acquisition deadline stopped applying when they were assigned. No real database, query, policy, credential or clock is involved, and no later release or query success should be guessed from this finite trace.
- node-postgres: pg.Pool API
Maintainer documentation describes lazy creation, bounded pool size, idle versus checked-out clients, FIFO waiting when full and busy, return versus destruction, and exhaustion when clients are not released. Its library/configuration-specific behavior is not a universal pool contract. The example’s cancellation and five-logical-unit deadline are explicitly fictional.
- node-postgres: Pooling
Explains reusable database clients, connection-setup work, checkout/use/return and safe release after both success and error. Single-query convenience and transaction scope differ. No fixed connection latency, default capacity, universal pipelining rule or performance guarantee is adopted.
AI source comparison recorded 2026-10-17. Human learner validation is pending.
Quick quiz · one question
Keep pool capacity separate from waiting
Sample scenario. Pre-opened C1/C2, capacity 2; otherwise the same explicit FIFO/deadline rules. Completing the read and returning its healthy lease are separately supplied in each success-and-release event.
- Given connections
C1, C2 already open and healthy, with one exclusive borrower each. Lowest-numbered idle selection and FIFO waiting. No new connection, SQL, cached result, failure, retry or hidden work.- Waiting rule
5 invented logical units from arrival, only while waiting. Expiry is processed before same-time actions, which run in listed order. Granted reads have no checkout deadline. Cancellation removes only a waiting request. No real clock or timer runs.- Logical time 0
A arrives for checkout.- Logical time 1
B arrives for checkout.- Logical time 2
C arrives for checkout.- Logical time 3
D arrives for checkout.- Logical time 4
D is canceled while waiting.- Logical time 5
A has an explicitly supplied successful read, then returns its healthy connection. Release alone is not evidence of success.- Logical time 6
E arrives for checkout.- Logical time 7
F arrives for checkout.- Logical time 8
B has an explicitly supplied successful read, then returns its healthy connection. Release alone is not evidence of success.- Logical time 12
Only advance the stated logical observation.- Logical time 13
C has an explicitly supplied successful read, then returns its healthy connection. Release alone is not evidence of success.- Logical time 14
E has an explicitly supplied successful read, then returns its healthy connection. Release alone is not evidence of success.
With two healthy existing connections, trace checkout, reuse, cancellation and timeout. Did any checkout create a new connection, and how long did C and E wait?
Why this answer?
Answers stay on this device.
Sources and exercise scope
- node-postgres: pg.Pool API
Maintainer documentation describes lazy creation, bounded pool size, idle versus checked-out clients, FIFO waiting when full and busy, return versus destruction, and exhaustion when clients are not released. Its library/configuration-specific behavior is not a universal pool contract. The example’s cancellation and five-logical-unit deadline are explicitly fictional.
- node-postgres: Pooling
Explains reusable database clients, connection-setup work, checkout/use/return and safe release after both success and error. Single-query convenience and transaction scope differ. No fixed connection latency, default capacity, universal pipelining rule or performance guarantee is adopted.
Original synthetic exercise. AI comparison against the reviewed lesson recorded on 2026-10-17; the references above state their scope. Human learner validation has not been run. A result here records this attempt; it does not establish mastery.
Save or adjust this lesson
Make this lesson yours
Save it for later, or tell your robot what would help.
This correction stays on this browser. Browse all concepts if the topic is missing.
Familiarity is your own assessment. To use it in your coding tool, download your updated learning profile.
Choices are saved on this browser. They do not sync to your account or coding tool.