DEV Community

MangeshJadhav
MangeshJadhav

Posted on • Edited on

# PostgreSQL Tutorial: How SQL Query run in PostgreSQL

What Actually Happens When You Run:

SELECT * FROM table_name;

While it might look simple, this SQL query run off a powerful chain of events deep inside the PostgreSQL engine. Let check how it process.

  1. Database Server Interaction As soon as you hit Enter, the query is sent to the PostgreSQL server. PostgreSQL handles multiple databases and interacts with various objects:

Tables
Views
Indexes
Functions

  1. PostgreSQL Server Process Once received, PostgreSQL does this:

Spawns a backend process just for your query
Manages this process individually for efficiency and concurrency
Think of each request as getting its own lane on a data highway

  1. Query Processing Pipeline Your query now goes through several stages of transformation:

Parser:

Tokenizes the SQL query, breaking it into individual meaningful units (tokens).

Creates a parse tree, which is a hierarchical representation of the query's syntax.

Validates syntax & semantics to ensure the query follows SQL rules and refers to valid database objects (tables, columns, etc.).

Analyzer:

Interprets what the query is trying to do, understanding the logical operations (e.g., selection, projection, join).

Builds a query tree, a more semantic representation of the query's intent, detailing the operations to be performed.

Rewriter:

Applies PostgreSQL rules (e.g., from views or IN clauses that can be rewritten) to transform the query if needed.

May return multiple new queries if a single logical query can be executed in different physical ways.

Planner:

Analyzes all possible strategies for running the query, considering various access methods (e.g., sequential scan, index scan) and join orders.

Uses statistics about the data (e.g., number of rows, distribution of values) to estimate the cost of each strategy.

Chooses the cheapest & most efficient plan (the query execution plan) to minimize resource usage and execution time.

Executor:

Carries out the plan generated by the Planner.

Retrieves the actual rows from disk and memory according to the optimized plan.

Returns the results to you in milliseconds, completing the query's lifecycle.

  1. Background Processes (Always Working!) Behind the scenes, these processes support every query:

Checkpointer — Writes dirty pages to disk
WAL Writer — Logs data changes to prevent loss
AutoVacuum — Cleans up dead rows
Archiver — Handles backup processes
Background Writer — Smoothens disk writes
Stats Collector — Gathers performance data

  1. Memory Management PostgreSQL splits memory into:

Shared Memory — Used across all backend processes
Local Memory — Used for individual queries
Efficient memory handling ensures high-speed data access and minimal disk reads.

Final Thoughts
Every time you run:

SELECT * FROM table_name;

you’re actually triggering a symphony of parsing, planning, optimizing, and executing, backed by an army of helper processes.

Top comments (0)