# magicstack/asyncpg architecture

> Architecture diagram of the GitHub repository magicstack/asyncpg, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/magicstack/asyncpg
- Repository: https://github.com/magicstack/asyncpg

## Overview

##### Main Components

a. Core Components:
- Protocol Layer (protocol/)
- Connection Management (connection.py, pool.py)
- Query Execution Pipeline
- Type System & Codecs (protocol/codecs/)
- Authentication System (protocol/scram.pyx)

b. Supporting Components:
- Exception Handling System (exceptions/)
- Connection Pool Management
- Transaction Management (transaction.py)
- Cursor Implementation (cursor.py)

**User Application Code**

**Connection Pool → Connection → Transaction Management**

**Query Execution → Prepared Statements → Cursor Management**

**PostgreSQL Protocol Implementation → Authentication → Type System**

**AsyncIO Transport**

**PostgreSQL Server**

##### Key Relationships to Highlight

- Flow of queries from Connection through Protocol layer
- Demonstrate how the Type System interfaces with both Protocol and Public API layers
- Relationship between Prepared Statements and Query Execution

##### Technical Details

- Connection pooling configuration options
- Prepared statement lifecycle
- Type conversion pipeline
- Authentication methods supported
- Error handling flow

##### The asyncio integration points

Remember to:
- Use consistent symbols throughout
- Version compatibility notes
- Error handling paths

This system design should reflect asyncpg's focus on performance and its direct implementation of the PostgreSQL protocol while showing how it integrates with Python's asyncio framework.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and classes
    classDef api fill:#2196F3,stroke:#1565C0,color:white
    classDef core fill:#4CAF50,stroke:#2E7D32,color:white
    classDef protocol fill:#FFC107,stroke:#FFA000,color:black
    classDef external fill:#9E9E9E,stroke:#616161,color:white
    classDef security fill:#F44336,stroke:#C62828,color:white
    
    %% Layer 1 - Application Interface
    UserApp[User Application Code]:::external

    %% Layer 2 - Public API
    subgraph API
        ConnPool[Connection Pool]:::api
        Conn[Connection]:::api
        TransMgmt[Transaction Management]:::api
    end

    %% Layer 3 - Core Processing
    subgraph Core
        QueryExec[Query Execution]:::core
        PrepStmt[Prepared Statements]:::core
        CursorMgmt[Cursor Management]:::core
        ExceptionHandler[Exception Handler]:::core
    end

    %% Layer 4 - Protocol Implementation
    subgraph Protocol
        PgProtocol[PostgreSQL Protocol]:::protocol
        Auth[Authentication]:::security
        TypeSystem[Type System]:::protocol
        Codecs[Codecs]:::protocol
        RecordMgmt[Record Management]:::protocol
        ProtSettings[Protocol Settings]:::protocol
    end

    %% Layer 5 & 6 - Network & Database
    AsyncIO[AsyncIO Transport]:::external
    PostgreSQL[PostgreSQL Server]:::external

    %% Relationships
    UserApp --> ConnPool
    UserApp --> Conn
    ConnPool --> Conn
    Conn --> TransMgmt
    Conn --> QueryExec
    Conn --> PrepStmt
    QueryExec --> CursorMgmt
    QueryExec --> PgProtocol
    PrepStmt --> PgProtocol
    PgProtocol --> TypeSystem
    PgProtocol --> Auth
    TypeSystem --> Codecs
    PgProtocol --> RecordMgmt
    PgProtocol --> ProtSettings
    PgProtocol --> AsyncIO
    AsyncIO --> PostgreSQL

    %% Click events for component mapping
    click PgProtocol "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/protocol/protocol.pyx"
    click Codecs "https://github.com/MagicStack/asyncpg/tree/master/asyncpg/protocol/codecs/"
    click Auth "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/protocol/scram.pyx"
    click Conn "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/connection.py"
    click ConnPool "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/pool.py"
    click TransMgmt "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/transaction.py"
    click ExceptionHandler "https://github.com/MagicStack/asyncpg/tree/master/asyncpg/exceptions/"
    click CursorMgmt "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/cursor.py"
    click PrepStmt "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/prepared_stmt.py"
    click TypeSystem "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/protocol/pgtypes.pxi"
    click ProtSettings "https://github.com/MagicStack/asyncpg/blob/master/asyncpg/protocol/settings.pyx"
    click RecordMgmt "https://github.com/MagicStack/asyncpg/tree/master/asyncpg/protocol/record/"

    %% Legend
    subgraph Legend
        API_L[Public API]:::api
        Core_L[Core Processing]:::core
        Protocol_L[Protocol Layer]:::protocol
        Security_L[Security]:::security
        External_L[External Systems]:::external
    end
```
