DBIO::PostgreSQL
PostgreSQL driver for DBIO (fork of DBIx::Class::Storage::DBI::Pg).
Supports
- desired-state deployment via test-deploy-and-compare (DBIO::PostgreSQL::Deploy)
- native introspection via pg_catalog (DBIO::PostgreSQL::Introspect)
- native diff (DBIO::PostgreSQL::Diff)
- native DDL generation (DBIO::PostgreSQL::DDL)
- async PostgreSQL via EV::Pg (DBIO::PostgreSQL::Async, no DBI)
Usage
package MyApp::DB;
use base 'DBIO::Schema';
__PACKAGE__->load_components('PostgreSQL');
my $schema = MyApp::DB->connect('dbi:Pg:database=myapp');
DBIO core autodetects dbi:Pg: DSNs and loads this storage automatically.
PostgreSQL Features
The driver supports the full range of PostgreSQL features:
Types
SERIAL,BIGSERIAL- auto-increment via sequencesUUID- generate viagen_random_uuid()oruuid_generate_v4()JSONB- binary JSON with comparison operatorsJSON- text JSONTEXT[],INTEGER[]- PostgreSQL arraysHSTORE- key-value storeTSVECTOR- full-text searchVECTOR- embedding vectors (pgvector extension)ENUM- user-defined enum types (auto-created on deploy)INTERVAL,TIMESTAMPTZ,TIMETZ,ABSTIME- temporal types
Schema Support
- Multiple schemas per database (namespace via
schema_namein connect_info) - Schema-qualified table references
- Public schema is default
Row Level Security (RLS)
row_securityattribute on result sources enables RLS policies- Per-column
security_invokersupport DBIO::PostgreSQL::StoragehandlesSET LOCAL ROLEfor RLS context
Indexes
PRIMARY KEY,UNIQUEconstraints create indexesdbix_indexresult source attribute for expression indexesINCLUDEcolumns for covering indexes (PostgreSQL 11+)CONCURRENTLYindex creation option- Partial indexes via
WHEREclause in index definition NULLS NOT DISTINCTfor unique indexes with NULL handling (PG15+)
Full-Text Search
TSVECTORcolumn type withto_tsvector/to_tsqueryhelperstsvector_columninflator for Result class attribute
Foreign Data Wrappers
postgres_fdwremote table supportoracle_fdw,mysql_fdwexternal tables
PostgreSQL-Specific Features
ON CONFLICT ... DO UPDATE(upsert) viainsert_or_updateRETURNINGclause for all INSERT/UPDATE/DELETEFOR UPDATE SKIP LOCKEDfor non-blocking row locking- Advisory locks via
pg_advisory_lock/pg_try_advisory_lock COPYbulk import/export supportLISTEN/NOTIFYpub/sub via DBIO::PostgreSQL::Async
Introspection (pg_catalog)
pg_class,pg_attribute,pg_indexfor tables/columns/indexespg_type,pg_enumfor type and enum introspectionpg_namespacefor schema listingpg_constraintfor foreign keys and unique checkspg_triggerfor trigger-based auto-increment detection- Sequence detection via
pg_dependand trigger inspection
Deploy
DBIO::PostgreSQL::Deploy orchestrates test-deploy-and-compare:
- Introspect live database via pg_catalog (DBIO::PostgreSQL::Introspect)
- Deploy desired schema to a temporary schema (
_dbio_tmp_<pid>_<time>) - Introspect the temporary schema the same way
- Diff source vs target (DBIO::PostgreSQL::Diff)
- Drop the temporary schema
Install (install_ddl) creates fresh schema. Upgrade diffs live vs. desired.
Testing
Requires a running PostgreSQL instance:
export DBIO_TEST_PG_DSN="dbi:Pg:database=myapp"
export DBIO_TEST_PG_USER=postgres
export DBIO_TEST_PG_PASS=secret
prove -l t/
Offline tests (t/00-load.t, SQLMaker tests) run without a database.
Requirements
- Perl 5.36+
- DBD::Pg
- DBIO core
See Also
DBIO::Introspect::Base, DBIO::Diff::Base, DBIO::Deploy