# ossc-db/pg_bulkload architecture

> Architecture diagram of the GitHub repository ossc-db/pg_bulkload, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ossc-db/pg_bulkload
- Repository: https://github.com/ossc-db/pg_bulkload

## Overview

##### Main Components

- Command-Line Interface / Engine:
– Highlight the entry point (e.g., the main routine in bin/pg_bulkload.c and its Windows counterpart in pg_bulkload_win32.c).
– Indicate how the tool is invoked (by passing a control file that specifies load operations).

- Parsing Modules:
– Show separate modules handling different input formats. For example, modules for CSV (parser_csv.c), binary (parser_binary.c), function-based input (parser_function.c), and tuple parsing (parser_tuple.c) are clearly present.
– Indicate that these modules are responsible for validating and transforming the input data as specified by the control file.

- Data Writer / Loader Components:
– Include components from the lib directory that perform the actual loading into the destination PostgreSQL tables.
– Note the different writer implementations (e.g., writer_binary.c, writer_buffered.c, writer_direct.c, writer_parallel.c) that manage different loading methods.

- PostgreSQL Interface Layer:
– Identify how SQL scripts and SQL interface files in bin/sql (like load_csv-v1.sql, load_bin-v1.sql, etc.) are used to interact with the PostgreSQL database.
– Include the control file logic that bypasses PostgreSQL shared buffers, ensuring very high-speed data ingestion.

- Utility and Support Libraries:
– Show supporting code in the lib (e.g., pg_btree.c, nbtsort modules) and include directories which provide essential utilities such as logging (logger.h/.c), string utilities, and other helper functions.
– Note the “pgut” utilities present both in bin/pgut and lib/pgut that support common operations.

- Testing and Packaging Components:
– Include an element for the regression tests (i.e., bin/expected with expected outputs, and GitHub workflows in .github/workflows) to indicate how system integrity is maintained.
– Represent the SPECS directory that contains package specification files for different PostgreSQL versions.
– Optionally mention the MSVC and util directories, which provide alternative build configurations and extra utilities (e.g., the pg_timestamp converter).

##### Relationships and Data Flow

- How the Command-Line Engine receives a control file as input from the user and then triggers the parsing phase.
- Direct the output from the Parsing Modules to the Data Writer components that then interface with PostgreSQL using various SQL scripts and low-level routines.
- Interactions for logging and error handling shown via the Logger component. The Logger is used throughout to capture operation details.

##### Architectural Patterns and Design Principles

- Modularity: separate components for parsing, data writing, and logging provide clear boundaries between concerns.
- Pipeline nature of the system: data moves from input (control file) → transformation (parsing/ETL) → loading (database interface).
- That the use of multiple writer modules (direct, buffered, parallel) suggests a strategy for optimizing different load scenarios.
- That platform-specific adaptations exist (e.g., MSVC projects for Windows build).

##### Relevant Technologies and Frameworks

- The use of Makefiles and specific spec files for multiple PostgreSQL versions (pg13, pg14, … pg17) should be noted. This illustrates the project’s support for a range of environments.
- CI/CD pipelines (GitHub Workflows for build, test, release) can be added as a separate block to show continuous integration support, even if not directly a runtime component.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Control and Entry Points
    CF["Control File Input"]:::control

    subgraph "Command Line Engines"
        CLI1["Command Line Engine (pg_bulkload.c)"]:::engine
        CLI2["Command Line Engine (pg_bulkload_win32.c)"]:::engine
    end

    %% Engine Output Virtual Node
    E_OUT["Engine Output (Parsed Data Trigger)"]:::engine

    %% Parsing Modules
    subgraph "Parsing Modules"
        P1["CSV Parser (parser_csv.c)"]:::parser
        P2["Binary Parser (parser_binary.c)"]:::parser
        P3["Function-Parser (parser_function.c)"]:::parser
        P4["Tuple Parser (parser_tuple.c)"]:::parser
    end

    %% Virtual node for aggregated parsed output
    PD["Parsed Data"]:::parser

    %% Data Writers / Loader Components
    subgraph "Data Writers"
        D1["Binary Writer (writer_binary.c)"]:::writer
        D2["Buffered Writer (writer_buffered.c)"]:::writer
        D3["Direct Writer (writer_direct.c)"]:::writer
        D4["Parallel Writer (writer_parallel.c)"]:::writer
    end

    %% PostgreSQL Interface Layer
    PG["SQL Interface (bin/sql scripts)"]:::interface

    %% Utility and Support Libraries
    subgraph "Utilities & Logging"
        L1["Logger Header (logger.h)"]:::utility
        L2["Logger Implementation (logger.c)"]:::utility
        BT1["pg_btree Header (pg_btree.h)"]:::utility
        BT2["pg_btree Implementation (pg_btree.c)"]:::utility
        SU1["String Utils Header (pg_strutil.h)"]:::utility
        SU2["String Utils Implementation (pg_strutil.c)"]:::utility
        PGUT1["pgut Utilities (bin/pgut)"]:::utility
        PGUT2["pgut Libraries (lib/pgut)"]:::utility
    end

    %% Testing and Packaging Components
    subgraph "Testing & Packaging"
        TEST["Regression Tests (bin/expected)"]:::testing
        SPECS["Specification Files (SPECS)"]:::testing
        CI["CI/CD Pipelines (.github/workflows)"]:::testing
        MSVC["Platform-Specific Build (msvc)"]:::testing
    end

    %% Data Flow Connections
    CF -->|"controlfile"| CLI1
    CF -->|"controlfile"| CLI2

    CLI1 -->|"executes"| E_OUT
    CLI2 -->|"executes"| E_OUT

    E_OUT -->|"dispatch"| P1
    E_OUT -->|"dispatch"| P2
    E_OUT -->|"dispatch"| P3
    E_OUT -->|"dispatch"| P4

    P1 -->|"produces"| PD
    P2 -->|"produces"| PD
    P3 -->|"produces"| PD
    P4 -->|"produces"| PD

    PD -->|"feeds"| D1
    PD -->|"feeds"| D2
    PD -->|"feeds"| D3
    PD -->|"feeds"| D4

    D1 -->|"loads"| PG
    D2 -->|"loads"| PG
    D3 -->|"loads"| PG
    D4 -->|"loads"| PG

    %% Logging feedback (dotted lines)
    E_OUT ---|"logs"| L1
    PD ---|"logs"| L1
    D1 ---|"logs"| L1
    D2 ---|"logs"| L1
    D3 ---|"logs"| L1
    D4 ---|"logs"| L1
    PG ---|"logs"| L1

    %% Testing and CI/CD feedback
    PG -->|"verify"| TEST
    TEST -->|"triggers"| CI
    CI -->|"packages"| SPECS

    %% Platform-specific build linkage
    MSVC -->|"builds"| CLI1
    MSVC -->|"builds"| CLI2

    %% Click Events
    click CLI1 "https://github.com/ossc-db/pg_bulkload/blob/master/bin/pg_bulkload.c"
    click CLI2 "https://github.com/ossc-db/pg_bulkload/blob/master/bin/pg_bulkload_win32.c"
    click P1 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/parser_csv.c"
    click P2 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/parser_binary.c"
    click P3 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/parser_function.c"
    click P4 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/parser_tuple.c"
    click D1 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/writer_binary.c"
    click D2 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/writer_buffered.c"
    click D3 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/writer_direct.c"
    click D4 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/writer_parallel.c"
    click PG "https://github.com/ossc-db/pg_bulkload/tree/master/bin/sql"
    click L1 "https://github.com/ossc-db/pg_bulkload/blob/master/include/logger.h"
    click L2 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/logger.c"
    click BT1 "https://github.com/ossc-db/pg_bulkload/blob/master/include/pg_btree.h"
    click BT2 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/pg_btree.c"
    click SU1 "https://github.com/ossc-db/pg_bulkload/blob/master/include/pg_strutil.h"
    click SU2 "https://github.com/ossc-db/pg_bulkload/blob/master/lib/pg_strutil.c"
    click PGUT1 "https://github.com/ossc-db/pg_bulkload/tree/master/bin/pgut"
    click PGUT2 "https://github.com/ossc-db/pg_bulkload/tree/master/lib/pgut"
    click TEST "https://github.com/ossc-db/pg_bulkload/tree/master/bin/expected"
    click SPECS "https://github.com/ossc-db/pg_bulkload/tree/master/SPECS"
    click CI "https://github.com/ossc-db/pg_bulkload/tree/master/.github/workflows"
    click MSVC "https://github.com/ossc-db/pg_bulkload/tree/master/msvc"

    %% Styles
    classDef engine fill:#f9e79f,stroke:#d35400,stroke-width:2px;
    classDef parser fill:#aed6f1,stroke:#2471a3,stroke-width:2px;
    classDef writer fill:#abebc6,stroke:#229954,stroke-width:2px;
    classDef interface fill:#f5cba7,stroke:#cb6a08,stroke-width:2px;
    classDef utility fill:#f5b7b1,stroke:#c0392b,stroke-width:2px;
    classDef testing fill:#d2b4de,stroke:#884ea0,stroke-width:2px;
    classDef control fill:#d5f5e3,stroke:#28b463,stroke-width:2px;
```
