Frequently Asked Questions
Does asyncpg support DB-API?
No. DB-API is a synchronous API, while asyncpg is basedaround an asynchronous I/O model. Thus, full drop-in compatibilitywith DB-API is not possible and we decided to design asyncpg APIin a way that is better aligned with PostgreSQL architecture andterminology. We will release a synchronous DB-API-compatible versionof asyncpg at some point in the future.
Can I use asyncpg with SQLAlchemy ORM?
Yes. SQLAlchemy version 1.4 and later supports the asyncpg dialect natively.Please refer to its documentation for details. Older SQLAlchemy versionsmay be used in tandem with a third-party adapter such asasyncpgsa ordatabases.
Can I use dot-notation withasyncpg.Record? It looks cleaner.
We decided against makingasyncpg.Record a named tuplebecause we want to keep theRecord method namespace separatefrom the column namespace. That said, you can provide a customRecordclass that implements dot-notation via therecord_class argument toconnect() or any of the Record-returningmethods.
classMyRecord(asyncpg.Record):def__getattr__(self,name):returnself[name]
Why can’t I use acursor outside of a transaction?
Cursors created by a call toConnection.cursor() orPreparedStatement.cursor()cannot be used outside of a transaction. Any such attempt will result inInterfaceError.To create a cursor usable outside of a transaction, use theDECLARE...CURSORWITHHOLD SQL statement directly.
Why am I getting prepared statement errors?
If you are getting intermittentpreparedstatement"__asyncpg_stmt_xx__"doesnotexist orpreparedstatement“__asyncpg_stmt_xx__”alreadyexists errors, you are most likely not connecting to thePostgreSQL server directly, but viapgbouncer. pgbouncer, whenin the"transaction" or"statement" pooling mode, does not supportprepared statements. You have several options:
if you are using pgbouncer only to reduce the cost of new connections(as opposed to using pgbouncer for connection pooling froma large number of clients in the interest of better scalability),switch to theconnection poolfunctionality provided by asyncpg, it is a much better option for thispurpose;
disable automatic use of prepared statements by passing
statement_cache_size=0toasyncpg.connect()andasyncpg.create_pool()(and, obviously, avoid the use ofConnection.prepare());switch pgbouncer’s
pool_modetosession.
Why do I getPostgresSyntaxError when usingexpressionIN$1?
expressionIN$1 is not a valid PostgreSQL syntax. To checka value against a sequence useexpression=any($1::mytype[]),wheremytype is the array element type.