Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

D-Code Postgres

Decoding PostgreSQL internals, one function at a time.

A beginner-friendly journey through PostgreSQL source code, combining real code walkthroughs with hands-on educational implementations.

This project is for people who want to understand how PostgreSQL works internally but do not know where to begin. We will study small parts of the real PostgreSQL codebase, explain them in plain language, and reinforce what we learn by writing small educational implementations.

What we will learn

The project will initially focus on two areas:

  1. initdb — how PostgreSQL creates and prepares a new database cluster.
  2. Query processing — how PostgreSQL takes SQL through parsing, rewriting, planning, and execution.

The goal is not to rewrite PostgreSQL. The goal is to make its architecture and source code easier to approach.

First project: mini-initdb

Our first coding project will be mini-initdb, a small C program inspired by part of PostgreSQL's real initdb.

The first version will:

  • accept a data-directory argument such as -D ./demo-data;
  • validate the destination directory;
  • create a simplified PostgreSQL-like directory structure;
  • write a PG_VERSION file;
  • generate simple configuration files;
  • report each initialization step; and
  • clean up incomplete output when initialization fails.

Important

mini-initdb is an educational program. It will not create a database cluster that can be started by the real PostgreSQL server.

Creating a real cluster also requires PostgreSQL's bootstrap backend, system catalogs, template databases, and other production behavior. We will study those parts separately.

Planned project structure

The repository will grow toward the following structure:

D-Code-Postgres/
├── README.md
├── initdb/
│   ├── README.md
│   ├── real-code-walkthrough.md
│   ├── diagrams/
│   │   └── initdb-flow.md
│   └── mini-initdb/
│       ├── include/
│       │   └── mini_initdb.h
│       ├── src/
│       │   ├── main.c
│       │   ├── directories.c
│       │   ├── config.c
│       │   └── cleanup.c
│       ├── tests/
│       ├── Makefile
│       └── README.md
├── query-processing/
└── glossary/

This tree describes the plan. Some files and directories may not exist yet.

Learning approach

Each topic should follow the same pattern:

  1. Introduce the concept.
  2. Run a small experiment.
  3. Locate the relevant PostgreSQL source files and entry points.
  4. Follow a short call path through the real code.
  5. Implement a smaller version where that helps understanding.
  6. Add exercises and questions for further exploration.

We will also maintain a glossary for PostgreSQL and C terms such as Oid, Datum, Node, Path, Plan, and RelOptInfo.

initdb roadmap

Milestone 1: Directories and version file

  • Parse -D and --pgdata.
  • Validate the target directory.
  • Create the initial directory structure securely.
  • Write PG_VERSION.
  • Add basic tests.

Milestone 2: Configuration files

  • Generate simplified postgresql.conf.
  • Generate simplified pg_hba.conf.
  • Explain how the real initdb creates these files.

Milestone 3: Errors and cleanup

  • Detect partial initialization.
  • Clean up files created by the current run.
  • Preserve pre-existing user files.
  • Test failure cases.

Milestone 4: Read the real PostgreSQL code

  • Build PostgreSQL with debug symbols.
  • Trace the real initdb with GDB or LLDB.
  • Map our functions to PostgreSQL's implementation.
  • Study bootstrap mode and system-catalog creation.

Query-processing roadmap

We will follow a small query through PostgreSQL:

SELECT name
FROM users
WHERE id = 42;

The journey will cover:

SQL text
  → parser
  → analyzed query tree
  → rewriter
  → planner and optimizer
  → plan tree
  → executor
  → result rows

Later exercises will explore sequential scans, index scans, cost estimates, row estimates, and join strategies using EXPLAIN.

Upstream references

Contributing

This is a learning project, and beginner questions are welcome. Explanations, diagrams, experiments, tests, corrections, and links to relevant PostgreSQL code are all valuable contributions.

When adding a lesson, try to include:

  • the concept in plain language;
  • the relevant PostgreSQL source path;
  • commands readers can reproduce;
  • expected output;
  • a small exercise; and
  • the PostgreSQL version used for the walkthrough.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors