# duckdb/pg_duckdb architecture

> Architecture diagram of the GitHub repository duckdb/pg_duckdb, 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/duckdb/pg_duckdb
- Repository: https://github.com/duckdb/pg_duckdb

## Overview

##### Main Components

a. Core Components:
- PostgreSQL Database Server
- DuckDB Engine Integration
- Query Execution Layer
- Storage Layer
- Extension Management System
- Transaction Management System

b. External Integrations:
- Object Storage (S3, Azure, GCS, R2)
- MotherDuck Integration
- File Format Support (Parquet, CSV, JSON, Iceberg, Delta)

**┌─ Client Layer ─────────────────────────────────────┐**
- PostgreSQL Clients (psql, applications)            │
- ┬───────────────────────────┘
- ↓
**┌─ Interface Layer ─────────────────────────────────┐**
- PostgreSQL Protocol                             │
- Extension API Interface                         │
- ┬───────────────────────────┘
- ↓
**┌─ Core Processing Layer ───────────────────────────┐**
- ┌─────────────────┐        ┌──────────────────┐  │
- PostgreSQL     │ ←──→   │     DuckDB       │  │
- Query Engine   │        │   Query Engine   │  │
- ┘        └──────────────────┘  │
- ┬───────────────────────────┘
- ↓
**┌─ Storage Layer ──────────────────────────────────┐**
- ┌─────────────┐ ┌──────────┐ ┌───────────────┐  │
- PostgreSQL │ │  DuckDB  │ │  Object       │  │
- Tables     │ │  Tables  │ │  Storage      │  │
- ┘ └──────────┘ └───────────────┘  │
- ┘

##### Key Interactions to Highlight

- Query Router: Show how queries are directed to either PostgreSQL or DuckDB engine based on:
- Query type
- Table access method
- Force execution setting
- Data source type

- Data Flow Paths:
- PostgreSQL ↔ DuckDB data exchange
- External storage access paths
- MotherDuck integration path

##### Important Subsystems to Detail

- Catalog Management (from /include/pgduckdb/catalog/)
- Transaction Management
- Query Planning and Execution
- Type System Integration
- Extension Management
- Background Workers

##### Technical Details

- Support for various data types
- Transaction boundary handling
- Security and authentication flows
- Extension loading mechanism
- Configuration settings

##### Specific Implementation Notes

- Catalog structure (pgduckdb_catalog.hpp)
- Transaction management (pgduckdb_transaction.hpp)
- Table access methods (pgduckdb_table_am.hpp)
- Query planning (pgduckdb_planner.hpp)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph CL[Client Layer]
        PGC[PostgreSQL Clients]:::client
    end

    %% Interface Layer
    subgraph IL[Interface Layer]
        PGP[PostgreSQL Protocol]:::protocol
        EAI[Extension API Interface]:::api
    end

    %% Core Processing Layer
    subgraph CPL[Core Processing Layer]
        subgraph QE[Query Engine]
            PGQE[PostgreSQL Query Engine]:::postgres
            DQE[DuckDB Query Engine]:::duckdb
            QR[Query Router]:::router
            PE["Query Planner/Executor"]:::executor
            BW[Background Worker]:::worker
        end
        
        subgraph CM[Catalog Management]
            CAT[Catalog System]:::catalog
            TM[Transaction Manager]:::transaction
            TS[Type System]:::types
        end
    end

    %% Storage Layer
    subgraph SL[Storage Layer]
        PGT[PostgreSQL Tables]:::postgres
        DT[DuckDB Tables]:::duckdb
        
        subgraph OS[Object Storage]
            S3[S3]:::storage
            AZ[Azure]:::storage
            GCS[GCS]:::storage
            R2[R2]:::storage
        end
    end

    %% External Integration
    MD[MotherDuck]:::external

    %% Connections
    PGC --> PGP
    PGP --> EAI
    EAI --> QR
    QR --> PGQE & DQE
    PGQE <--> PE
    DQE <--> PE
    PE --> CAT
    CAT <--> TM
    CAT <--> TS
    BW --> QE
    PGQE --> PGT
    DQE --> DT
    DQE --> OS
    DQE <--> MD

    %% Click Events
    click DQE "https://github.com/duckdb/pg_duckdb/blob/main/include/pgduckdb/pgduckdb_duckdb.hpp"
    click TM "https://github.com/duckdb/pg_duckdb/blob/main/include/pgduckdb/catalog/pgduckdb_transaction_manager.hpp"
    click PE "https://github.com/duckdb/pg_duckdb/blob/main/include/pgduckdb/pgduckdb_planner.hpp"
    click CAT "https://github.com/duckdb/pg_duckdb/blob/main/include/pgduckdb/catalog/pgduckdb_catalog.hpp"
    click TS "https://github.com/duckdb/pg_duckdb/blob/main/include/pgduckdb/pgduckdb_types.hpp"
    click BW "https://github.com/duckdb/pg_duckdb/blob/main/include/pgduckdb/pgduckdb_background_worker.hpp"
    click OS "https://github.com/duckdb/pg_duckdb/tree/main/third_party/cached_httpfs/"

    %% Styles
    classDef postgres fill:#4169E1,color:white
    classDef duckdb fill:#32CD32,color:white
    classDef storage fill:#FFA500,color:white
    classDef external fill:#800080,color:white
    classDef client fill:#A9A9A9,color:white
    classDef protocol fill:#A9A9A9,color:white
    classDef api fill:#A9A9A9,color:white
    classDef router fill:#FFD700,color:black
    classDef executor fill:#20B2AA,color:white
    classDef catalog fill:#CD853F,color:white
    classDef transaction fill:#CD853F,color:white
    classDef types fill:#CD853F,color:white
    classDef worker fill:#20B2AA,color:white

    %% Legend
    subgraph Legend
        L1[PostgreSQL Components]:::postgres
        L2[DuckDB Components]:::duckdb
        L3[Storage Systems]:::storage
        L4[External Services]:::external
        L5[Infrastructure]:::client
    end
```
