class PG::Pool

Connections shared by many tasks, on any worker. An idle connection is kept as a PG::Lease (its socket and its session's state, Shareable); a task that needs one takes a lease and makes a PG::Connection of it in its own task, and gives it back when done.

PG::Pool.open("postgres://app@localhost/shop", size: 8) do |pool|
  Task.scope do |s|
    8.times { |i| s.spawn { pool.query("SELECT $1::int", [i]) } }
  end
  pool.transaction { |c| c.exec("UPDATE stock SET n = n - 1") }
end

The rules:

src/pool.qz:29

Class methods

def self.new(url: String, size: Int = …, checkout_timeout: Duration? = …) -> PG::Pool

A pool of up to size connections to the server url names (see PG.connect); close it with #close. .open closes it for you.

src/pool.qz:43

def self.new(config: PG::Config, size: Int = …, checkout_timeout: Duration? = …) -> PG::Pool

As .new(url), with a PG::Config.

src/pool.qz:51

def self.open[R](url: String, size: Int = …, checkout_timeout: Duration? = …, &: (PG::Pool) -> R) -> R

Runs the block with a new pool (see .new), and closes the pool when the block ends. Returns the block's value.

src/pool.qz:59

Methods

def with[R](&: (PG::Connection) -> R) -> R

Runs the block with a connection of the pool, in the calling task, and returns its value (see the class's rules). The connection is the pool's: a block that calls its PG::Connection#release takes it out of the pool.

src/pool.qz:72

def exec(sql: String) -> PG::Result

PG::Connection#exec on a connection of the pool.

src/pool.qz:82

def query(sql: String, params: Array[Object?] = …) -> PG::Result

PG::Connection#query on a connection of the pool.

src/pool.qz:85

def transaction[R](&: (PG::Connection) -> R) -> R

PG::Connection#transaction on a connection of the pool.

src/pool.qz:88

def size -> Int

How many connections are open, idle or in use.

src/pool.qz:93

def closed? -> Bool

Whether #close was called.

src/pool.qz:96

def close -> Void

Closes the idle connections now, and the others when they are given back; later calls raise PG::ConnectionError. Closing again does nothing.

src/pool.qz:101