Creates a pooled PostgreSQL core. Requires the psycopg2 library.

  :pool_size: must be a 2-tuple in the form (min_connections, max_connections).

def get_pooled_pg_core(connection_string, pool_size=None, *, cursor_factory=None,
                       edit_connection=None, threaded=True):
  """Creates a pooled PostgreSQL core. Requires the psycopg2 library.

  :pool_size: must be a 2-tuple in the form (min_connections, max_connections).
  """
  from psycopg2.extras import NamedTupleCursor
  from psycopg2.pool import ThreadedConnectionPool as TPool, SimpleConnectionPool as SPool
  if not pool_size:
    pool_size = (5, 10)
  if threaded:
    pool = TPool(pool_size[0], pool_size[1], connection_string)
  else:
    pool = SPool(pool_size[0], pool_size[1], connection_string)

  def opener():
    """Gets a PostgreSQL connection from the scope-captured connection pool."""
    cn = pool.getconn()
    cn.cursor_factory = cursor_factory or NamedTupleCursor
    if edit_connection:
      edit_connection(cn)
    return cn

  def closer(connection):
    """Returns a connection to the scope-captured connection pool."""
    pool.putconn(connection)

  return InjectedDataAccessCore(
    opener,
    closer,
    ("%({0})s", "%s", "{0}::{1}"),
    empty_params=None,
    supports_timezones=True,
    supports_returning_syntax=True,
    get_autocommit=get_pg_autocommit,
    set_autocommit=set_pg_autocommit)