When working directly with the PostgreSQL source code, writing correct logic is only one part of development. Maintaining the official PostgreSQL coding style is equally important. Clean formatting improves readability, makes code reviews easier, and keeps contributions consistent across a large codebase.
PostgreSQL provides an official formatting tool called pg_indent for this purpose. It is used by PostgreSQL developers to automatically align declarations, fix indentation, apply brace style, and keep source files consistent with project standards.
If you are building PostgreSQL from source and modifying files of the PostgreSQL source code, executor files, storage modules, or planner code, learning pg_indent is an essential part of your workflow.
What is pg_indent?
pg_indent is the PostgreSQL source formatter. It is a wrapper script that uses pg_bsd_indent internally along with PostgreSQL-specific rules and typedef definitions.
It helps format:
- .c files
- .h files
- declarations
- code blocks
- switch statements
- indentation
- braces
- alignment of variables
It does not change your logic. It only changes style and layout.
Why Use pg_indent?
In a large project like PostgreSQL, multiple developers work on the same codebase. Without a common formatter, every file would look different.
Using pg_indent provides:
- Consistent coding style
- Cleaner diffs in Git
- Easier code reviews
- Better readability
- PostgreSQL standard formatting
Where is pg_indent located?
Inside the PostgreSQL source tree:
src/tools/pgindent/
Main script:
src/tools/pgindent/pgindent
The formatter binary used internally:
src/tools/pg_bsd_indent/
Step 1: Build pg_bsd_indent
Move into the pg_bsd_indent directory and compile it.
cd ~/cybro_postgres/src/tools/pg_bsd_indent
make
Install it:
sudo make install
Step 2: Verify Installation
Check the version:
./pg_bsd_indent --version
Expected output:
pg_bsd_indent 2.1.2 (based on FreeBSD indent)
This confirms the required formatter is available.
Step 3: Run pg_indent on PostgreSQL Source Files
Move to the PostgreSQL root directory:
cd ~/cybro_postgres
Now format a real PostgreSQL source file:
src/tools/pgindent/pgindent src/backend/postmaster/postmaster.c
You can also use the full absolute path:
/home/cybrosys/cybro_postgres/src/tools/pgindent/pgindent \
/home/cybrosys/cybro_postgres/src/backend/postmaster/postmaster.c
If no error appears, formatting is completed successfully.
Real Formatting Examples
The following examples demonstrate how pg_indent automatically reformats PostgreSQL source code.
1. Variable Alignment
If we change the alignment of these variables in postgres source code like this
Before
int a;
long total;
char *name;
By using pg_bsd_indent formatter, the code looks like this.
After
int a;
long total;
char *name;
What Changed?
- Variables are aligned
- Spacing is standardized
- Pointer declarations follow PostgreSQL style
2. Brace Style
Before
if (x) {
printf("ok");
}After
if (x)
{
printf("ok");
}
What Changed?
- Opening braces moved to a new line
- Indentation corrected
- Code block becomes easier to read
3. Indentation in Blocks
Before
if (x)
printf("yes");
After
if (x)
printf("yes");
What Changed?
- Statement inside the condition is properly indented
4. Switch Formatting
Before
switch(x){
case 1:
break;
}After
switch (x)
{
case 1:
break;
}
What Changed?
- Spacing after switch
- Braces moved to PostgreSQL style
- case indentation corrected
- break aligned properly
Real PostgreSQL Example from postmaster.c
Suppose you accidentally write declarations with inconsistent spacing:
pg_getopt_ctx optctx;
int opt;
int status;
char *userDoption = NULL;
bool listen_addr_saved = false;
char *output_config_variable = NULL;
After running pg_indent:
pg_getopt_ctx optctx;
int opt;
int status;
char *userDoption = NULL;
bool listen_addr_saved = false;
char *output_config_variable = NULL;
This is exactly how PostgreSQL keeps declarations clean and aligned.
Recommended Development Workflow
Whenever you modify PostgreSQL source code:
cd ~/cybro_postgres
make
src/tools/pgindent/pgindent path/to/file.c
make
Example:
src/tools/pgindent/pgindent src/backend/postmaster/postmaster.c
Then check your changes:
git diff
Important Notes
pg_indent Does Not Change Logic
It will not modify:
- conditions
- loops
- algorithms
- queries
- return values
It only changes formatting.
Why pg_indent Works Better Than Raw indent
Using pg_bsd_indent directly can format C code, but pg_indent adds PostgreSQL-specific rules such as:
- typedef awareness
- project formatting standards
- exclusion patterns
- consistent behavior across source files
For PostgreSQL development, always prefer pg_indent.
Best Practice Tip
Create an alias:
alias pgfmt='$HOME/cybro_postgres/src/tools/pgindent/pgindent'
Then use:
pgfmt src/backend/postmaster/postmaster.c
This saves time during daily development.
If you are customizing PostgreSQL, contributing patches, or learning internal development, pg_indent should become part of your regular workflow. It ensures your source files match PostgreSQL standards and keeps your code professional and maintainable.
Formatting may look like a small step, but in serious source code development, consistency matters just as much as correctness.
Use pg_indent after every edit, and your PostgreSQL source code will always stay clean, readable, and ready for review.