# Buggy code
def __init__(
    self,
    protocol,
    transport,
    loop,
    addr: (str, int) or str,
    config: connect_utils._ClientConfiguration,
    params: connect_utils._ConnectionParameters,
):
    self._protocol = protocol
    self._transport = transport
    self._loop = loop
    self._types_stmt = None
    self._type_by_name_stmt = None
    self._top_xact = None
    self._uid = 0
    self._aborted = False
    # Incremented very time the connection is released back to a pool.
    # Used to catch invalid references to connection-related resources
    # post-release (e.g. explicit prepared statements).
    self._pool_release_ctr = 0

    self._addr = addr
    self._config = config
    self._params = params

    self._stmt_cache = _StatementCache(
        loop=loop,
        max_size=config.statement_cache_size,
        on_remove=self._maybe_gc_stmt,
        max_lifetime=config.max_cached_statement_lifetime,
    )

    self._stmts_to_close = set()

    self._listeners = {}
    self._log_listeners = set()

    settings = self._protocol.get_settings()
    ver_string = settings.server_version
    self._server_version = serverversion.split_server_version_string(ver_string)

    self._server_caps = _detect_server_capabilities(self._server_version, settings)

    self._intro_query = introspection.INTRO_LOOKUP_TYPES

    self._reset_query = None
    self._proxy = None

    # Used to serialize operations that might involve anonymous
    # statements.  Specifically, we want to make the following
    # operation atomic:
    #    ("prepare an anonymous statement", "use the statement")
    #
    # Used for `con.fetchval()`, `con.fetch()`, `con.fetchrow()`,
    # `con.execute()`, and `con.executemany()`.
    self._stmt_exclusive_section = _Atomic()


# Traceback (InvalidSQLStatementNameError)
In [1]: import os, asyncpg, asyncio

In [2]: loop = asyncio.get_event_loop()

In [3]: conn = loop.run_until_complete(asyncpg.connect(
...:     host = 'pgaas.mail.yandex.net',
...:     port = 12000,
...:     user = 'statinfra_api',
...:     database = 'statinfra_api_beta',
...:     password=os.environ['DB_PASSWORD'],
...:     ssl=True,
...:     statement_cache_size=0
...: ))

In [4]: loop.run_until_complete(conn.fetch('SELECT * FROM tasks'))
---------------------------------------------------------------------------
InvalidSQLStatementNameError              Traceback (most recent call last)
<ipython-input-5-8a44237acb87> in <module>()
----> 1 loop.run_until_complete(conn.fetch('SELECT * FROM tasks'))

/usr/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
465             raise RuntimeError('Event loop stopped before Future completed.')
466
--> 467         return future.result()
468
469     def stop(self):

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/connection.py in fetch(self, query, timeout, *args)
339         """
340         self._check_open()
--> 341         return await self._execute(query, args, 0, timeout)
342
343     async def fetchval(self, query, *args, column=0, timeout=None):

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/connection.py in _execute(self, query, args, limit, timeout, return_status)
1186         timeout = self._protocol._get_timeout(timeout)
1187         with self._stmt_exclusive_section:
-> 1188             return await self._do_execute(query, executor, timeout)
1189
1190     async def _executemany(self, query, args, timeout):

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/connection.py in _do_execute(self, query, executor, timeout, retry)
1197     async def _do_execute(self, query, executor, timeout, retry=True):
1198         if timeout is None:
-> 1199             stmt = await self._get_statement(query, None)
1200         else:
1201             before = time.monotonic()

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/connection.py in _get_statement(self, query, timeout, named)
290                 self._types_stmt = await self.prepare(self._intro_query)
291
--> 292             types = await self._types_stmt.fetch(list(ready))
293             self._protocol.get_settings().register_data_types(types)
294

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/prepared_stmt.py in fetch(self, timeout, *args)
155         :return: A list of :class:`Record` instances.
156         """
--> 157         data = await self.__bind_execute(args, 0, timeout)
158         return data
159

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/prepared_stmt.py in __bind_execute(self, args, limit, timeout)
194         protocol = self._connection._protocol
195         data, status, _ = await protocol.bind_execute(
--> 196             self._state, args, '', limit, True, timeout)
197         self._last_status = status
198         return data

~/.virtualenvs/statbox-abt-backend/lib/python3.6/site-packages/asyncpg/protocol/protocol.pyx in bind_execute (asyncpg/protocol/protocol.c:66799)()

InvalidSQLStatementNameError: prepared statement "__asyncpg_stmt_1__" does not exist

# Fixed code
def __init__(
    self,
    protocol,
    transport,
    loop,
    addr: (str, int) or str,
    config: connect_utils._ClientConfiguration,
    params: connect_utils._ConnectionParameters,
):
    self._protocol = protocol
    self._transport = transport
    self._loop = loop
    self._top_xact = None
    self._uid = 0
    self._aborted = False
    # Incremented very time the connection is released back to a pool.
    # Used to catch invalid references to connection-related resources
    # post-release (e.g. explicit prepared statements).
    self._pool_release_ctr = 0

    self._addr = addr
    self._config = config
    self._params = params

    self._stmt_cache = _StatementCache(
        loop=loop,
        max_size=config.statement_cache_size,
        on_remove=self._maybe_gc_stmt,
        max_lifetime=config.max_cached_statement_lifetime,
    )

    self._stmts_to_close = set()

    self._listeners = {}
    self._log_listeners = set()

    settings = self._protocol.get_settings()
    ver_string = settings.server_version
    self._server_version = serverversion.split_server_version_string(ver_string)

    self._server_caps = _detect_server_capabilities(self._server_version, settings)

    self._intro_query = introspection.INTRO_LOOKUP_TYPES

    self._reset_query = None
    self._proxy = None

    # Used to serialize operations that might involve anonymous
    # statements.  Specifically, we want to make the following
    # operation atomic:
    #    ("prepare an anonymous statement", "use the statement")
    #
    # Used for `con.fetchval()`, `con.fetch()`, `con.fetchrow()`,
    # `con.execute()`, and `con.executemany()`.
    self._stmt_exclusive_section = _Atomic()