# psycopg/psycopg2 architecture

> Architecture diagram of the GitHub repository psycopg/psycopg2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/psycopg/psycopg2
- Repository: https://github.com/psycopg/psycopg2

## Overview

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: [`psycopg/cursor_type.c`](https://github.com/psycopg/psycopg2/blob/master/psycopg/cursor_type.c) (Cursor API), [`psycopg/connection_type.c`](https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_type.c) (Connection API), [`psycopg/pqpath.c`](https://github.com/psycopg/psycopg2/blob/master/psycopg/pqpath.c) (libpq query path), [`psycopg/microprotocols.c`](https://github.com/psycopg/psycopg2/blob/master/psycopg/microprotocols.c) (Python-to-SQL adaptation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Python API"]
  node_connection["Connection API<br/>[connection_type.c]"]
  node_connection_core["Connection core<br/>[connection_int.c]"]
  node_cursor["Cursor API<br/>[cursor_type.c]"]
end

subgraph group_execution["Query Execution"]
  node_query_path["libpq query path<br/>[pqpath.c]"]
end

subgraph group_types["Types and SQL"]
  node_adaptation["Python-to-SQL adaptation<br/>[microprotocols.c]"]
  node_adapters["Value adapters"]
  node_typecasting["Result typecasting<br/>[typecast.c]"]
  node_datetime_casts["Date-time casts"]
  node_extensions["Extension API<br/>[extensions.py]"]
end

subgraph group_features["Database Features"]
  node_replication["Replication cursors"]
  node_replication_messages["Replication messages"]
  node_large_objects["Large objects<br/>[lobject_type.c]"]
  node_notifications["Notifications<br/>[notify_type.c]"]
end

subgraph group_utilities["Convenience Utilities"]
  node_pool["Connection pooling<br/>[pool.py]"]
  node_sql_helpers["SQL composition<br/>[sql.py]"]
  node_extras["Extras<br/>[extras.py]"]
  node_json_types["JSON support<br/>[_json.py]"]
  node_range_types["Range support<br/>[_range.py]"]
  node_timezone["Timezone support<br/>[tz.py]"]
end

node_application(("Python application"))
node_postgres[("PostgreSQL server")]

node_application -->|"opens connection"| node_connection
node_connection -->|"manages connection"| node_connection_core
node_connection -->|"creates cursor"| node_cursor
node_application -.->|"executes SQL"| node_cursor
node_cursor -->|"executes query"| node_query_path
node_query_path -->|"uses libpq"| node_postgres
node_query_path -->|"uses typecasts"| node_typecasting
node_typecasting -->|"initializes casts"| node_datetime_casts
node_cursor -.->|"adapts parameters"| node_adaptation
node_adaptation -.->|"selects adapters"| node_adapters
node_application -.->|"customizes adaptation"| node_extensions
node_application -.->|"streams replication"| node_replication
node_replication -.->|"reads messages"| node_replication_messages
node_application -.->|"uses large objects"| node_large_objects
node_application -.->|"receives notifications"| node_notifications
node_application -.->|"reuses connections"| node_pool
node_application -.->|"composes SQL"| node_sql_helpers
node_application -.->|"uses helpers"| node_extras
node_application -.->|"registers JSON"| node_json_types
node_application -.->|"uses ranges"| node_range_types
node_application -.->|"uses timezones"| node_timezone

click node_connection "https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_type.c"
click node_connection_core "https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_int.c"
click node_cursor "https://github.com/psycopg/psycopg2/blob/master/psycopg/cursor_type.c"
click node_query_path "https://github.com/psycopg/psycopg2/blob/master/psycopg/pqpath.c"
click node_adaptation "https://github.com/psycopg/psycopg2/blob/master/psycopg/microprotocols.c"
click node_adapters "https://github.com/psycopg/psycopg2/tree/master/psycopg"
click node_typecasting "https://github.com/psycopg/psycopg2/blob/master/psycopg/typecast.c"
click node_datetime_casts "https://github.com/psycopg/psycopg2/blob/master/psycopg/typecast_datetime.c"
click node_extensions "https://github.com/psycopg/psycopg2/blob/master/lib/extensions.py"
click node_replication "https://github.com/psycopg/psycopg2/blob/master/psycopg/replication_cursor_type.c"
click node_replication_messages "https://github.com/psycopg/psycopg2/blob/master/psycopg/replication_message_type.c"
click node_large_objects "https://github.com/psycopg/psycopg2/blob/master/psycopg/lobject_type.c"
click node_notifications "https://github.com/psycopg/psycopg2/blob/master/psycopg/notify_type.c"
click node_pool "https://github.com/psycopg/psycopg2/blob/master/lib/pool.py"
click node_sql_helpers "https://github.com/psycopg/psycopg2/blob/master/lib/sql.py"
click node_extras "https://github.com/psycopg/psycopg2/blob/master/lib/extras.py"
click node_json_types "https://github.com/psycopg/psycopg2/blob/master/lib/_json.py"
click node_range_types "https://github.com/psycopg/psycopg2/blob/master/lib/_range.py"
click node_timezone "https://github.com/psycopg/psycopg2/blob/master/lib/tz.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_connection,node_connection_core,node_cursor toneBlue
class node_query_path,node_postgres toneAmber
class node_adaptation,node_adapters,node_typecasting,node_datetime_casts,node_extensions toneMint
class node_replication,node_replication_messages,node_large_objects,node_notifications toneRose
class node_pool,node_sql_helpers,node_extras,node_json_types,node_range_types,node_timezone,node_application toneIndigo
```

## Components

### Python API

- [Connection API](https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_type.c): `psycopg/connection_type.c`
- [Connection core](https://github.com/psycopg/psycopg2/blob/master/psycopg/connection_int.c): `psycopg/connection_int.c`
- [Cursor API](https://github.com/psycopg/psycopg2/blob/master/psycopg/cursor_type.c): `psycopg/cursor_type.c`

### Query Execution

- [libpq query path](https://github.com/psycopg/psycopg2/blob/master/psycopg/pqpath.c): `psycopg/pqpath.c`

### Types and SQL

- [Python-to-SQL adaptation](https://github.com/psycopg/psycopg2/blob/master/psycopg/microprotocols.c): `psycopg/microprotocols.c`
- [Value adapters](https://github.com/psycopg/psycopg2/tree/master/psycopg): `psycopg`
- [Result typecasting](https://github.com/psycopg/psycopg2/blob/master/psycopg/typecast.c): `psycopg/typecast.c`
- [Date-time casts](https://github.com/psycopg/psycopg2/blob/master/psycopg/typecast_datetime.c): `psycopg/typecast_datetime.c`
- [Extension API](https://github.com/psycopg/psycopg2/blob/master/lib/extensions.py): `lib/extensions.py`

### Database Features

- [Replication cursors](https://github.com/psycopg/psycopg2/blob/master/psycopg/replication_cursor_type.c): `psycopg/replication_cursor_type.c`
- [Replication messages](https://github.com/psycopg/psycopg2/blob/master/psycopg/replication_message_type.c): `psycopg/replication_message_type.c`
- [Large objects](https://github.com/psycopg/psycopg2/blob/master/psycopg/lobject_type.c): `psycopg/lobject_type.c`
- [Notifications](https://github.com/psycopg/psycopg2/blob/master/psycopg/notify_type.c): `psycopg/notify_type.c`

### Convenience Utilities

- [Connection pooling](https://github.com/psycopg/psycopg2/blob/master/lib/pool.py): `lib/pool.py`
- [SQL composition](https://github.com/psycopg/psycopg2/blob/master/lib/sql.py): `lib/sql.py`
- [Extras](https://github.com/psycopg/psycopg2/blob/master/lib/extras.py): `lib/extras.py`
- [JSON support](https://github.com/psycopg/psycopg2/blob/master/lib/_json.py): `lib/_json.py`
- [Range support](https://github.com/psycopg/psycopg2/blob/master/lib/_range.py): `lib/_range.py`
- [Timezone support](https://github.com/psycopg/psycopg2/blob/master/lib/tz.py): `lib/tz.py`

### Other components

- **Python application**
- **PostgreSQL server**

## Connections

- Python application → Connection API: opens connection
- Connection API → Connection core: manages connection (evidence: `psycopg/connection_type.c`)
- Connection API → Cursor API: creates cursor (evidence: `psycopg/connection_type.c`)
- Python application → Cursor API: executes SQL
- Cursor API → libpq query path: executes query (evidence: `psycopg/cursor_type.c`)
- libpq query path → PostgreSQL server: uses libpq
- libpq query path → Result typecasting: uses typecasts (evidence: `psycopg/pqpath.c`)
- Result typecasting → Date-time casts: initializes casts (evidence: `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 (evidence: `lib/pool.py`)
- Python application → SQL composition: composes SQL (evidence: `lib/sql.py`)
- Python application → Extras: uses helpers (evidence: `lib/extras.py`)
- Python application → JSON support: registers JSON (evidence: `lib/_json.py`)
- Python application → Range support: uses ranges (evidence: `lib/_range.py`)
- Python application → Timezone support: uses timezones (evidence: `lib/tz.py`)
