DEV Community

Arnob
Arnob

Posted on

Understanding Database Internal Operation

A database INSERT operation is actually a sequence of low-level internal operations inside the database engine. The SQL query is only the top layer.

Example query:

INSERT INTO users(id,name)
VALUES(1,'Arnob');
Enter fullscreen mode Exit fullscreen mode

Behind this, the database performs many operations.

Basic Operation Flow

Application
    ↓
DB Driver
    ↓
TCP Connection
    ↓
SQL Parser
    ↓
Query Optimizer
    ↓
Transaction Manager
    ↓
Lock Manager
    ↓
Buffer Pool (RAM)
    ↓
Write Ahead Log (WAL)
    ↓
Storage Engine
    ↓
Disk Write
Enter fullscreen mode Exit fullscreen mode

INSERT Operation Flow

Client Request
    ↓
SQL Parsing
    ↓
Authentication & Permission Check
    ↓
Transaction Creation
    ↓
Lock Acquisition
    ↓
Constraint Validation
    ↓
Data Encoding
    ↓
Memory Page Modification
    ↓
WAL/Redo Log Write
    ↓
Index Updates
    ↓
Commit
    ↓
Background Flush to Disk
Enter fullscreen mode Exit fullscreen mode

1. Network Receive Operation

The DB server first receives packets over:

  • TCP
  • Unix socket
  • Shared memory

Example:

Application
   ↓
DB Driver
   ↓
TCP Packet
   ↓
Database Listener Port
Enter fullscreen mode Exit fullscreen mode

Examples:

  • MySQL → 3306
  • PostgreSQL → 5432

2. SQL Parse Operation

The SQL parser tokenizes the query.

Example:

INSERT INTO users VALUES(1,'Arnob');
Enter fullscreen mode Exit fullscreen mode

becomes internal tokens:

INSERT
INTO
users
VALUES
1
'Arnob'
Enter fullscreen mode Exit fullscreen mode

Then converted into an internal syntax tree.

Parse Tree Example

INSERT
 ├── TABLE users
 ├── COLUMN id
 ├── COLUMN name
 └── VALUES
      ├── 1
      └── Arnob
Enter fullscreen mode Exit fullscreen mode

3. Semantic Validation Operation

Database validates:

| Validation       | Example        |
| ---------------- | -------------- |
| Table exists     | users          |
| Columns exist    | id,name        |
| Data types valid | int,string     |
| Syntax valid     | INSERT grammar |
Enter fullscreen mode Exit fullscreen mode

4. Permission Check Operation

Checks user privileges.

Example:

GRANT INSERT ON users TO app;
Enter fullscreen mode Exit fullscreen mode

If unauthorized:

ERROR: permission denied
Enter fullscreen mode Exit fullscreen mode

5. Transaction Initialization

The database creates internal transaction metadata.

Example internal structure:

Transaction ID = 1055
Status = ACTIVE
Timestamp = ...
Enter fullscreen mode Exit fullscreen mode

In PostgreSQL:

  • XID generated
  • Snapshot created

6. Lock Operation

Locks protect consistency.

Possible locks:

| Lock          | Purpose                    |
| ------------- | -------------------------- |
| Row lock      | Prevent conflicting update |
| Page lock     | Protect memory page        |
| Metadata lock | Protect schema             |
Enter fullscreen mode Exit fullscreen mode

Example:

Lock users table page 14
Enter fullscreen mode Exit fullscreen mode

7. Constraint Check Operation

Database validates constraints.

Primary Key Check

PRIMARY KEY(id)
Enter fullscreen mode Exit fullscreen mode

DB searches index:

Does id=1 already exist?
Enter fullscreen mode Exit fullscreen mode

Foreign Key Check

FOREIGN KEY(department_id)
Enter fullscreen mode Exit fullscreen mode

DB verifies referenced row exists.

Unique Constraint Check

UNIQUE(email)
Enter fullscreen mode Exit fullscreen mode

Searches the unique index.

8. Internal Data Encoding

The database converts values into a binary format.

Example:

1 → 0x00000001
Arnob → UTF-8 bytes
Enter fullscreen mode Exit fullscreen mode

Binary Storage Example

01 00 00 00
41 72 6E 6F 62
Enter fullscreen mode Exit fullscreen mode

Databases store binary structures, not SQL text.

9. Buffer Pool Operation

Databases use RAM pages.

Buffer Pool Architecture

captionless imagecaptionless imagecaptionless imagecaptionless image

The DB:

  1. Loads page into RAM
  2. Modifies the memory page
  3. Marks page dirty

Example:

Disk Page → RAM Buffer
Enter fullscreen mode Exit fullscreen mode

then:

Insert row into page slot
Enter fullscreen mode Exit fullscreen mode

10. Row Insert Operation

Database inserts a row physically into the page.

Example page:

+----------------+
| Page Header    |
| Row Directory  |
| Free Space     |
| Row Data       |
+----------------+
Enter fullscreen mode Exit fullscreen mode

The new row is appended into free space.

11. MVCC Metadata Operation

Modern DBs add hidden metadata.

Example in PostgreSQL:

xmin = transaction id
xmax = delete marker
Enter fullscreen mode Exit fullscreen mode

This supports:

  • Concurrent reads
  • Snapshot isolation
  • Rollbacks

12. WAL / Redo Log Operation

Before table data reaches disk:

The database writes a log record. This is called:

Write Ahead Logging (WAL)

WAL Flow

captionless imagecaptionless imagecaptionless image

Example WAL record:

TXN 1055:
INSERT users(id=1,name='Arnob')
Enter fullscreen mode Exit fullscreen mode

Purpose:

  • Crash recovery
  • Durability
  • Replication

13. Index Insert Operation

Indexes must also be updated.

Example:

CREATE INDEX idx_name ON users(name);
Enter fullscreen mode Exit fullscreen mode

Database inserts into:

  • B+ Tree
  • Hash index
  • GiST
  • GIN

Depending on the engine.

B+ Tree Insert

captionless imagecaptionless image

Possible operations:

  • Find leaf node
  • Insert key
  • Split the page if full
  • Rebalance tree

14. Commit Operation

When COMMIT occurs:

Database:

  1. Flushes WAL safely
  2. Mark's transaction COMMITTED
  3. Releases locks

Then the client receives:

INSERT 0 1
Enter fullscreen mode Exit fullscreen mode

15. Background Flush Operation

Actual table pages may remain in RAM.

Background workers later flush them to disk.

Examples:

| DB         | Worker       |
| ---------- | ------------ |
| PostgreSQL | Checkpointer |
| MySQL      | Page Cleaner |
Enter fullscreen mode Exit fullscreen mode

16. Crash Recovery Support

If power fails:

Database reads WAL.

Recovery process:

Read WAL
Replay committed TX
Undo incomplete TX
Enter fullscreen mode Exit fullscreen mode

This guarantees durability.

Actual Low-Level OS Operations

Internally DB may call:

write()
fsync()
mmap()
pread()
pwrite()
Enter fullscreen mode Exit fullscreen mode

on Linux.

These interact with:

  • Filesystem
  • SSD/HDD
  • Kernel page cache

Simplified Internal View

SQL Query
   ↓
Parser
   ↓
Transaction
   ↓
Lock Manager
   ↓
Constraint Check
   ↓
Buffer Pool
   ↓
WAL Log
   ↓
Index Update
   ↓
Commit
   ↓
Disk Flush
Enter fullscreen mode Exit fullscreen mode

Why WAL Exists

Without WAL:

Crash during insert
=
Corrupted database
Enter fullscreen mode Exit fullscreen mode

With WAL:

Replay logs
Restore consistency
Enter fullscreen mode Exit fullscreen mode

Summary of Internal Operations

| Step | Internal Operation     |
| ---- | ---------------------- |
| 1    | Receive network packet |
| 2    | Parse SQL              |
| 3    | Validate schema        |
| 4    | Check permissions      |
| 5    | Start transaction      |
| 6    | Acquire locks          |
| 7    | Validate constraints   |
| 8    | Encode binary row      |
| 9    | Modify RAM page        |
| 10   | Write WAL              |
| 11   | Update indexes         |
| 12   | Commit transaction     |
| 13   | Flush to disk later    |
Enter fullscreen mode Exit fullscreen mode

DATABASE INSERT ARCHITECTURE

                    DATABASE INSERT ARCHITECTURE
 ┌────────────────────────────────────────────────────────────┐
 │                     CLIENT APPLICATION                     │
 │  PHP / Node.js / Java / Python / Go / ORM / API Server    │
 └───────────────────────┬────────────────────────────────────┘
                         │ SQL Query
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                    DATABASE DRIVER                         │
 │        PDO / JDBC / psycopg2 / mysql2 / libpq             │
 └───────────────────────┬────────────────────────────────────┘
                         │ TCP / Socket Connection
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                    DATABASE SERVER                         │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                  CONNECTION MANAGER                        │
 │   Authentication / Session / Permission Validation         │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                      SQL PARSER                            │
 │   Parse SQL → Generate Internal Parse Tree                 │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                 QUERY PLANNER / OPTIMIZER                  │
 │    Determine insert strategy / indexes / execution plan    │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                  TRANSACTION MANAGER                       │
 │      Create TX ID / MVCC Snapshot / ACID Handling          │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                     LOCK MANAGER                           │
 │         Row Lock / Page Lock / Metadata Lock               │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                  CONSTRAINT VALIDATOR                      │
 │   PK / FK / UNIQUE / CHECK / NOT NULL Validation           │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                   STORAGE ENGINE                           │
 │     InnoDB / Heap Storage / WiredTiger / etc               │
 └───────────────────────┬────────────────────────────────────┘
                         │
         ┌───────────────┴────────────────┐
         │                                │
         ▼                                ▼
 ┌──────────────────────┐      ┌────────────────────────────┐
 │    BUFFER POOL       │      │    WAL / REDO LOG          │
 │   Memory Pages       │      │  Write-Ahead Logging       │
 │   Dirty Pages        │      │  Crash Recovery Logs       │
 └──────────┬───────────┘      └──────────────┬─────────────┘
            │                                  │
            └──────────────┬───────────────────┘
                           │
                           ▼
 ┌────────────────────────────────────────────────────────────┐
 │                    INDEX MANAGER                           │
 │         Update B+Tree / Hash / GIN Indexes                │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                      COMMIT PHASE                          │
 │        Flush WAL / Mark TX Committed / Release Locks       │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │               BACKGROUND FLUSH PROCESS                     │
 │      Checkpointer / Page Cleaner / Flush Thread            │
 └───────────────────────┬────────────────────────────────────┘
                         │
                         ▼
 ┌────────────────────────────────────────────────────────────┐
 │                     PHYSICAL STORAGE                       │
 │          SSD / HDD / NVMe / Distributed Storage            │
 │                                                            │
 │   Data Files     WAL Files      Index Files                │
 │   .ibd           pg_wal         B+Tree Pages               │
 └────────────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

When an INSERT query runs, the database engine does much more than simply saving data to disk. First, the query is parsed and validated, then a transaction is created to maintain ACID consistency.

The database converts the row into binary format and writes it into a memory buffer (Buffer Pool/Shared Buffer). Before saving the actual data page, it writes a WAL (Write-Ahead Log) or Redo Log for crash recovery and durability.

If indexes exist, the engine also updates B+Tree index structures. After the transaction commits, background processes eventually flush the modified pages from memory to physical storage.

This architecture helps databases like PostgreSQL and MySQL provide fast, reliable, and crash-safe data insertion.

Top comments (0)