How psycopg/psycopg2 works
Psycopg 2 is a Python DB-API 2.0 adapter over PostgreSQL’s libpq. The primary workflow is an application opening a connection, creating a cursor, executing SQL, and receiving database results; the cursor execution path and libpq layer are visible in the sampled source. Separate branches represent documented adaptation, asynchronous use, COPY, notifications, replication, and convenience utilities. Some of those branches are documented capabilities but their implementations were not sampled, so those relationships are shown as unverified.
Main entry points: cursor_type.c Cursor API · connection_type.c Connection API · pqpath.c libpq query path · microprotocols.c Python-to-SQL adaptation
Components 21
Python API
- Connection API
psycopg/connection_type.c - Connection core
psycopg/connection_int.c - Cursor API
psycopg/cursor_type.c
Query Execution
- libpq query path
psycopg/pqpath.c
Types and SQL
- Python-to-SQL adaptation
psycopg/microprotocols.c - Value adapters
psycopg - Result typecasting
psycopg/typecast.c - Date-time casts
psycopg/typecast_datetime.c - Extension API
lib/extensions.py
Database Features
- Replication cursors
psycopg/replication_cursor_type.c - Replication messages
psycopg/replication_message_type.c - Large objects
psycopg/lobject_type.c - Notifications
psycopg/notify_type.c
Convenience Utilities
- Connection pooling
lib/pool.py - SQL composition
lib/sql.py - Extras
lib/extras.py - JSON support
lib/_json.py - Range support
lib/_range.py - Timezone support
lib/tz.py
Other components
- Python application
- PostgreSQL server
Connections 21
- Python application → Connection API: opens connection
- Connection API → Connection core: manages connection
psycopg/connection_type.c - Connection API → Cursor API: creates cursor
psycopg/connection_type.c - Python application → Cursor API: executes SQL
- Cursor API → libpq query path: executes query
psycopg/cursor_type.c - libpq query path → PostgreSQL server: uses libpq
- libpq query path → Result typecasting: uses typecasts
psycopg/pqpath.c - Result typecasting → Date-time casts: initializes casts
psycopg/typecast.c - Cursor API → Python-to-SQL adaptation: adapts parameters
- Python-to-SQL adaptation → Value adapters: selects adapters
- Python application → Extension API: customizes adaptation
- Python application → Replication cursors: streams replication
- Replication cursors → Replication messages: reads messages
- Python application → Large objects: uses large objects
- Python application → Notifications: receives notifications
- Python application → Connection pooling: reuses connections
lib/pool.py - Python application → SQL composition: composes SQL
lib/sql.py - Python application → Extras: uses helpers
lib/extras.py - Python application → JSON support: registers JSON
lib/_json.py - Python application → Range support: uses ranges
lib/_range.py - Python application → Timezone support: uses timezones
lib/tz.py