Skip to content

KeyError in cursor.execute with pgbouncer, works with psycopg2 #303

@j-bennet

Description

@j-bennet

An error happens when trying to run a command on pgbouncer database (virtual database).

Traceback (most recent call last):
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/errors.py", line 310, in _class_for_state
    return lookup(sqlstate)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/errors.py", line 293, in lookup
    return _sqlcodes[sqlstate.upper()]
KeyError: ''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/irinatruong/src/pgcli/t2.py", line 8, in <module>
    cur.execute("show help;")
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/cursor.py", line 551, in execute
    self._conn.wait(
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/connection.py", line 776, in wait
    return waiting.wait(gen, self.pgconn.socket, timeout=timeout)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/waiting.py", line 62, in wait_selector
    s = gen.send(ready)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/cursor.py", line 200, in _execute_gen
    self._check_results(results)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/cursor.py", line 405, in _check_results
    self._raise_for_result(res)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/cursor.py", line 412, in _raise_for_result
    raise e.error_from_result(result, encoding=self._encoding)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/errors.py", line 300, in error_from_result
    cls = _class_for_state(state.decode("ascii"))
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/errors.py", line 312, in _class_for_state
    return get_base_exception(sqlstate)
  File "/Users/irinatruong/.pyenv/versions/pgcli/lib/python3.9/site-packages/psycopg/errors.py", line 318, in get_base_exception
    or _base_exc_map.get(sqlstate[0])
IndexError: string index out of range

Code:

# t2.py
import psycopg


if __name__ == "__main__":
    conn = psycopg.connect("user=postgres port=6432 password=postgres dbname=pgbouncer")
    conn.autocommit = True
    cur = conn.cursor()
    cur.execute("show help;")
    for n in conn.notifies():
        print(n)
    print(cur.statusmessage)
    cur.close()
    conn.close()

Same code, but with psycopg2 works:

# t1.py

import psycopg2


if __name__ == "__main__":
    conn = psycopg2.connect("user=postgres port=6432 password=postgres dbname=pgbouncer")
    conn.autocommit = True
    cur = conn.cursor()
    cur.execute("show help;")
    for n in conn.notices:
        print(n)
    print(cur.statusmessage)
    cur.close()
    conn.close()

Output:

> python t1.py
NOTICE:  Console usage
DETAIL:
	SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION
	SHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM
	SHOW DNS_HOSTS|DNS_ZONES
	SHOW STATS|STATS_TOTALS|STATS_AVERAGES|TOTALS
	SET key = arg
	RELOAD
	PAUSE [<db>]
	RESUME [<db>]
	DISABLE <db>
	ENABLE <db>
	RECONNECT [<db>]
	KILL <db>
	SUSPEND
	SHUTDOWN


SHOW

Steps to reproduce

  1. Install pgbouncer (brew install pgbouncer or get one here).
  2. Put this in pgbouncer.ini:
# pgbouncer.ini

 [databases]
 test = host=localhost port=5432 dbname=test

 [pgbouncer]
 listen_port = 6432
 listen_addr = localhost
 auth_type = md5
 auth_file = userlist.txt
 logfile = pgbouncer.log
 pidfile = pgbouncer.pid
 admin_users = postgres
  1. Put this in userlist.txt:
"postgres" "postgres"
  1. Start pgbouncer.
  2. Run the code snippet t2.py.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions