AsyncPG¶
High-performance async PostgreSQL adapter using asyncpg. Supports native pipelines, Arrow export, and Cloud SQL / AlloyDB connectors.
Configuration¶
- class sqlspec.adapters.asyncpg.AsyncpgConfig[source]¶
Bases:
AsyncDatabaseConfig[PoolConnectionProxy,Pool[Record],AsyncpgDriver]Configuration for AsyncPG database connections using TypedDict.
- driver_type¶
alias of
AsyncpgDriver
- connection_type¶
alias of
PoolConnectionProxy
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, observability_config=None, **kwargs)[source]¶
Initialize AsyncPG configuration.
- Parameters:
connection_config¶ – Connection and pool configuration parameters (TypedDict or dict)
connection_instance¶ – Existing pool instance to use
migration_config¶ – Migration configuration
statement_config¶ – Statement configuration override
driver_features¶ – Driver features configuration (TypedDict or dict)
bind_key¶ – Optional unique identifier for this configuration
extension_config¶ – Extension-specific configuration (e.g., Litestar plugin settings)
observability_config¶ – Adapter-level observability overrides for lifecycle hooks and observers
**kwargs¶ – Additional keyword arguments
- async create_connection()[source]¶
Create a single async connection from the pool.
- Return type:
PoolConnectionProxy- Returns:
An AsyncPG connection instance.
- provide_session(*_args, statement_config=None, **_kwargs)[source]¶
Provide an async driver session context manager.
- async provide_pool(*args, **kwargs)[source]¶
Provide async pool instance.
- Return type:
Record- Returns:
The async connection pool.
- get_signature_namespace()[source]¶
Get the signature namespace for AsyncPG types.
This provides all AsyncPG-specific types that Litestar needs to recognize to avoid serialization attempts.
- class sqlspec.adapters.asyncpg.config.AsyncpgPoolConfig[source]¶
Bases:
AsyncpgConnectionConfigTypedDict for AsyncPG pool parameters, inheriting connection parameters.
Driver¶
- class sqlspec.adapters.asyncpg.AsyncpgDriver[source]¶
Bases:
AsyncDriverAdapterBaseAsyncPG PostgreSQL driver for async database operations.
Supports COPY operations, numeric parameter style handling, PostgreSQL exception handling, transaction management, SQL statement compilation and caching, and parameter processing with type coercion.
- __init__(connection, statement_config=None, driver_features=None)[source]¶
Initialize driver adapter with connection and configuration.
- Parameters:
connection¶ (
PoolConnectionProxy) – Database connection instancestatement_config¶ (
StatementConfig|None) – Statement configuration for the driverdriver_features¶ (
dict[str, typing.Any] |None) – Driver-specific features like extensions, secrets, and connection callbacksobservability¶ – Optional runtime handling lifecycle hooks, observers, and spans
- async dispatch_execute(cursor, statement)[source]¶
Execute single SQL statement.
Handles both SELECT queries and non-SELECT operations.
- async dispatch_execute_many(cursor, statement)[source]¶
Execute SQL with multiple parameter sets using AsyncPG’s executemany.
- async dispatch_execute_script(cursor, statement)[source]¶
Execute SQL script with statement splitting and parameter handling.
- async dispatch_special_handling(cursor, statement)[source]¶
Handle PostgreSQL COPY operations and other special cases.
- with_cursor(connection)[source]¶
Create context manager for AsyncPG cursor.
- Return type:
AsyncpgCursor
- handle_database_exceptions()[source]¶
Handle database exceptions with PostgreSQL error codes.
- Return type:
AsyncpgExceptionHandler
- async execute_stack(stack, *, continue_on_error=False)[source]¶
Execute a StatementStack using asyncpg’s rapid batching.
- Return type:
- async select_to_storage(statement, destination, /, *parameters, statement_config=None, partitioner=None, format_hint=None, telemetry=None, **kwargs)[source]¶
Execute a query and persist results to storage once native COPY is available.
- Return type:
- async load_from_arrow(table, source, *, partitioner=None, overwrite=False, telemetry=None)[source]¶
Load Arrow data into a PostgreSQL table via COPY.
- async load_from_storage(table, source, *, file_format, partitioner=None, overwrite=False)[source]¶
Read an artifact from storage and ingest it via COPY.
- Return type:
- property data_dictionary: AsyncpgDataDictionary¶
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
Extension Dialects¶
AsyncPG supports the pgvector and ParadeDB dialects for vector similarity search and full-text search operators. See the Dialects reference for operator details.
Data Dictionary¶
- class sqlspec.adapters.asyncpg.data_dictionary.AsyncpgDataDictionary[source]¶
Bases:
AsyncDataDictionaryBasePostgreSQL-specific async data dictionary.
-
dialect:
ClassVar[str] = 'postgres'¶ Dialect identifier. Must be defined by subclasses as a class attribute.
- async get_version(driver)[source]¶
Get PostgreSQL database version information.
- Parameters:
driver¶ (
AsyncpgDriver) – Async database driver instance.- Return type:
- Returns:
PostgreSQL version information or None if detection fails.
- async get_feature_flag(driver, feature)[source]¶
Check if PostgreSQL database supports a specific feature.
- Parameters:
driver¶ (
AsyncpgDriver) – Async database driver instance.
- Return type:
- Returns:
True if feature is supported, False otherwise.
- async get_optimal_type(driver, type_category)[source]¶
Get optimal PostgreSQL type for a category.
- Parameters:
driver¶ (
AsyncpgDriver) – Async database driver instance.
- Return type:
- Returns:
PostgreSQL-specific type name.
- async get_tables(driver, schema=None)[source]¶
Get tables sorted by topological dependency order using Recursive CTE.
- Return type:
- async get_columns(driver, table=None, schema=None)[source]¶
Get column information for a table or schema.
- Return type:
- async get_indexes(driver, table=None, schema=None)[source]¶
Get index metadata for a table or schema.
- Return type:
-
dialect: