PsqlPy#
Async PostgreSQL adapter using psqlpy, a Rust-backed PostgreSQL driver with native connection pooling.
Configuration#
- class sqlspec.adapters.psqlpy.PsqlpyConfig[source]#
Bases:
AsyncDatabaseConfig[Any,ConnectionPool,PsqlpyDriver]Configuration for Psqlpy asynchronous database connections.
- driver_type#
alias of
PsqlpyDriver
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, **kwargs)[source]#
Initialize Psqlpy configuration.
Extracts the 'on_connection_create' hook from driver_features before storing them. Initializes a set to track initialized connection IDs because psqlpy connections do not support weak references.
- Parameters:
connection_config¶ -- Connection and pool configuration parameters.
connection_instance¶ -- Existing connection pool instance to use.
migration_config¶ -- Migration configuration.
statement_config¶ -- SQL statement configuration.
driver_features¶ -- Driver feature configuration (TypedDict or dict).
bind_key¶ -- Optional unique identifier for this configuration.
extension_config¶ -- Extension-specific configuration.
**kwargs¶ -- Additional keyword arguments.
- async create_connection()[source]#
Create a single async connection (not from pool).
- Return type:
- Returns:
A psqlpy 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:
ConnectionPool- Returns:
The async connection pool.
Extension Dialects#
PsqlPy supports the pgvector and ParadeDB dialects for vector similarity search and full-text search operators. See the Dialects reference for operator details.
driver_features={"enable_pgvector": True} enables extension detection and
promotes the runtime dialect to pgvector when the PostgreSQL vector
extension is installed. It does not register automatic psqlpy vector type
handlers; pass psqlpy.extra_types.PgVector values or use explicit SQL casts
for vector parameters.
Driver#
- class sqlspec.adapters.psqlpy.PsqlpyDriver[source]#
Bases:
AsyncDriverAdapterBasePostgreSQL driver implementation using psqlpy.
Provides parameter style conversion, type coercion, error handling, and transaction management.
- __init__(connection, statement_config=None, driver_features=None)[source]#
Initialize driver adapter with connection and configuration.
- Parameters:
- async dispatch_execute_script(cursor, statement)[source]#
Execute SQL script with statement splitting.
- async set_migration_session_schema(schema)[source]#
Set the PostgreSQL search path for migration SQL.
- Return type:
- async set_migration_non_transactional_schema(schema)[source]#
Set the PostgreSQL search path for non-transactional migration SQL.
- Return type:
- async reset_migration_session_schema()[source]#
Reset the PostgreSQL search path after non-transactional migration SQL.
- Return type:
- dispatch_select_stream(statement, chunk_size)[source]#
Return a native psqlpy row stream backed by a server-side cursor in a transaction.
- handle_database_exceptions()[source]#
Handle database-specific exceptions.
- Return type:
PsqlpyExceptionHandler- Returns:
Exception handler context manager
- async select_to_storage(statement, destination, /, *parameters, statement_config=None, partitioner=None, format_hint=None, telemetry=None, **kwargs)[source]#
Execute a query and stream Arrow results to a storage backend.
- Return type:
- async load_from_arrow(table, source, *, partitioner=None, overwrite=False, telemetry=None)[source]#
Load Arrow-formatted data into PostgreSQL via psqlpy binary COPY.
- Return type:
- async load_from_storage(table, source, *, file_format, partitioner=None, overwrite=False)[source]#
Load staged artifacts from storage using the storage bridge pipeline.
- Return type:
- prepare_driver_parameters(parameters, statement_config, is_many=False, prepared_statement=None)[source]#
Prepare parameters with cast-aware type coercion for psqlpy.
- Parameters:
- Return type:
- Returns:
Parameters with cast-aware type coercion applied
- property data_dictionary: PsqlpyDataDictionary#
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
Data Dictionary#
- class sqlspec.adapters.psqlpy.data_dictionary.PsqlpyDataDictionary[source]#
Bases:
AsyncDataDictionaryBasePostgreSQL-specific async data dictionary via psqlpy.
- dialect: ClassVar[str] = 'postgres'#
Dialect identifier. Must be defined by subclasses as a class attribute.
- async get_metadata_capabilities(driver, domains=None)[source]#
Get PostgreSQL replacement data-dictionary capability profile.
- Return type:
MetadataCapabilityProfile
- async get_system_metadata_capabilities(driver, domains=None)[source]#
Get PostgreSQL opt-in system metadata capability disclosures.
- async get_objects(driver, schema=None)[source]#
Get database object metadata.
- Return type:
MetadataResult
- async get_table_details(driver, table, schema=None)[source]#
Get rich table metadata.
- Return type:
MetadataResult
- async get_constraints(driver, table=None, schema=None)[source]#
Get constraint metadata.
- Return type:
MetadataResult
- async get_privileges(driver, object_name=None, schema=None)[source]#
Get privilege metadata.
- Return type:
MetadataResult
- async get_dependencies(driver, object_name=None, schema=None)[source]#
Get dependency metadata.
- Return type:
MetadataResult
- async get_ddl(driver, object_name, schema=None, *, object_type='table', include_dependencies=True, prefer_native=True, redact=True)[source]#
Get object DDL where PostgreSQL exposes native definition helpers.
- Return type:
DDLResult
- async get_system_metadata(driver, request=None, **kwargs)[source]#
Get opt-in PostgreSQL system metadata.
- Return type:
SystemMetadataResult
- async get_version(driver)[source]#
Get PostgreSQL database version information.
Performs an inline cache check first to avoid a cross-module method call that causes mypyc segfaults. If not cached, fetches from the database.
- Return type:
VersionInfo|None
- async get_feature_flag(driver, feature)[source]#
Check if PostgreSQL database supports a specific feature.
- Return type:
- async get_optimal_type(driver, type_category)[source]#
Get optimal PostgreSQL type for a category.
- Return type:
- async get_tables(driver, schema=None)[source]#
Get tables sorted by topological dependency order using Recursive CTE.
- Return type:
list[TableMetadata]
- async get_columns(driver, table=None, schema=None)[source]#
Get column information for a table or schema.
- Return type:
list[ColumnMetadata]