Skip to content

Commit 325e22b

Browse files
committed
Add some documentation
1 parent 3f28c9d commit 325e22b

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ The following type maps are prefilled with type mappings from the PG::BasicTypeR
175175
* PG::BasicTypeMapBasedOnResult - a PG::TypeMapByOid prefilled with encoders for common PostgreSQL column types
176176
* PG::BasicTypeMapForQueries - a PG::TypeMapByClass prefilled with encoders for common Ruby value classes
177177

178+
Several type maps can be chained by setting PG::TypeMap::DefaultTypeMappable#default_type_map .
178179

179180
## Thread support
180181

ext/pg_type_map_by_oid.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ pg_tmbo_coders( VALUE self )
315315
* The type map will do Hash lookups for each result value, if the number of rows
316316
* is below or equal +number+.
317317
*
318+
* Default is 10.
319+
*
318320
*/
319321
static VALUE
320322
pg_tmbo_max_rows_for_online_lookup_set( VALUE self, VALUE value )

lib/pg/connection.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,16 @@ def cancel
673673
alias async_cancel cancel
674674

675675
module Pollable
676-
# Track the progress of the connection, waiting for the socket to become readable/writable before polling it
676+
# Track the progress of the connection, waiting for the socket to become readable/writable before polling it.
677+
#
678+
# Connecting to multiple hosts is done like so:
679+
# - All hosts are passed to PG::Connection.connect_start
680+
# - As soon as the host is tried to connect the related host is removed from the hosts list
681+
# - When the polling status changes to `PG::PGRES_POLLING_OK` the connection is returned and ready to use.
682+
# - When the polling status changes to `PG::PGRES_POLLING_FAILED` connecting is aborted and a PG::ConnectionBad is raised with details to all connection attepts.
683+
# - When a timeout occurs, connecting is restarted with the remaining hosts.
684+
#
685+
# The downside is that this connects only once to hosts which are listed twice when they timeout.
677686
private def polling_loop(poll_meth)
678687
connect_timeout = conninfo_hash[:connect_timeout]
679688
if (timeo = connect_timeout.to_i) && timeo > 0
@@ -799,7 +808,7 @@ class << self
799808
# PG::Connection.new(connection_string) -> conn
800809
# PG::Connection.new(host, port, options, tty, dbname, user, password) -> conn
801810
#
802-
# Create a connection to the specified server.
811+
# === Create a connection to the specified server.
803812
#
804813
# +connection_hash+ must be a ruby Hash with connection parameters.
805814
# See the {list of valid parameters}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS] in the PostgreSQL documentation.
@@ -823,7 +832,13 @@ class << self
823832
# [+password+]
824833
# login password
825834
#
826-
# Examples:
835+
#
836+
# If the Ruby default internal encoding is set (i.e., <code>Encoding.default_internal != nil</code>), the
837+
# connection will have its +client_encoding+ set accordingly.
838+
#
839+
# Raises a PG::Error if the connection fails.
840+
#
841+
# === Examples:
827842
#
828843
# # Connect using all defaults
829844
# PG::Connection.new
@@ -840,10 +855,16 @@ class << self
840855
# # As an URI
841856
# PG::Connection.new( "postgresql://user:pass@pgsql.example.com:5432/testdb?sslmode=require" )
842857
#
843-
# If the Ruby default internal encoding is set (i.e., <code>Encoding.default_internal != nil</code>), the
844-
# connection will have its +client_encoding+ set accordingly.
858+
# === Specifying Multiple Hosts
859+
#
860+
# It is possible to specify multiple hosts to connect to, so that they are tried in the given order or optionally in random order.
861+
# In the Keyword/Value format, the host, hostaddr, and port options accept comma-separated lists of values.
862+
# The {details to libpq}[https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS] describe how it works, but there are two small differences how ruby-pg handles multiple hosts:
863+
# - All hosts are resolved before the first connection is tried.
864+
# This means that when +load_balance_hosts+ is set to +random+, then all resolved addresses are tried randomly in one level.
865+
# When a host resolves to more than one address, it is therefore tried more often than a host that has only one address.
866+
# - When a timeout occurs due to the value of +connect_timeout+, then the given +host+, +hostaddr+ and +port+ combination is not tried a second time, even if it is specified several times.
845867
#
846-
# Raises a PG::Error if the connection fails.
847868
def new(*args)
848869
conn = connect_to_hosts(*args)
849870

0 commit comments

Comments
 (0)