DEV Community

Cover image for Postgresql Simplified
Krati Joshi
Krati Joshi

Posted on

Postgresql Simplified

PostgreSQL Backend Developer Essentials: The Concepts I Learned Beyond CRUD

When working with Node.js backend applications, knowing SQL CRUD operations is only the beginning.

As backend developers, we eventually need to understand transactions, indexes, concurrency, query optimization, JSONB, connection pooling, and how PostgreSQL actually executes our queries.

Today I explored some of the PostgreSQL concepts that matter most for backend development.

๐Ÿ˜ What is PostgreSQL?

PostgreSQL is an open-source object-relational database management system (ORDBMS).

It provides traditional relational database features such as tables, rows, columns, primary keys, foreign keys, and SQL, while also supporting advanced capabilities such as JSONB, arrays, custom types, functions, extensions, and specialized indexes.

A typical Node.js backend flow looks like:

Client
   โ†“
Node.js / Express
   โ†“
Prisma / PostgreSQL driver
   โ†“
PostgreSQL
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”‘ Primary Key vs Foreign Key

A primary key uniquely identifies a row.

id UUID PRIMARY KEY
Enter fullscreen mode Exit fullscreen mode

A foreign key creates a relationship between tables.

user_id UUID REFERENCES users(id)
Enter fullscreen mode Exit fullscreen mode

The foreign key also helps maintain referential integrity.

A simple way to remember:

Primary Key โ†’ identifies
Foreign Key โ†’ relates
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”’ Transactions and ACID

A transaction groups multiple database operations into one logical unit.

For example, a money transfer requires both:

Account A โ†’ -โ‚น1000
Account B โ†’ +โ‚น1000
Enter fullscreen mode Exit fullscreen mode

If one operation fails, we don't want only half of the transaction to be committed.

That's where ACID comes in:

A โ†’ Atomicity
C โ†’ Consistency
I โ†’ Isolation
D โ†’ Durability
Enter fullscreen mode Exit fullscreen mode

Transactions help make database operations reliable.

๐Ÿ”„ MVCC

One PostgreSQL concept I found particularly important is MVCC โ€” Multi-Version Concurrency Control.

PostgreSQL maintains different row versions/snapshots so concurrent transactions can work with consistent views of data.

The goal is to allow reads and writes to happen concurrently with less blocking than a simple locking model would provide.

For a backend developer, the key takeaway is:

MVCC is one of the mechanisms PostgreSQL uses to provide concurrency and transaction isolation.

๐Ÿงฉ JSONB

PostgreSQL supports JSONB for storing semi-structured data.

For example:

CREATE TABLE users (
    id UUID PRIMARY KEY,
    preferences JSONB
);
Enter fullscreen mode Exit fullscreen mode

We could store:

{
  "theme": "dark",
  "language": "en",
  "notifications": true
}
Enter fullscreen mode Exit fullscreen mode

Then query a property:

SELECT preferences->>'theme'
FROM users;
Enter fullscreen mode Exit fullscreen mode

JSONB becomes particularly powerful when combined with appropriate indexing, such as a GIN index.

However, JSONB shouldn't automatically replace relational columns. Frequently queried, constrained, and relational data is often better represented using normal columns.

๐Ÿ“Š Indexes

Indexes can significantly improve query performance by providing a faster access path to relevant rows.

For example:

CREATE INDEX idx_users_email
ON users(email);
Enter fullscreen mode Exit fullscreen mode

But indexes aren't free.

They consume storage and add overhead to INSERT, UPDATE, and DELETE operations because the index also needs to be maintained.

So:

Don't create indexes blindly. Create them based on actual query patterns and execution plans.

๐Ÿ” EXPLAIN and EXPLAIN ANALYZE

When a query becomes slow, we need to understand how PostgreSQL is executing it.

EXPLAIN
SELECT *
FROM users
WHERE email = 'test@example.com';
Enter fullscreen mode Exit fullscreen mode

EXPLAIN shows the planner's estimated execution plan.

For actual execution information:

EXPLAIN ANALYZE
SELECT *
FROM users
WHERE email = 'test@example.com';
Enter fullscreen mode Exit fullscreen mode

EXPLAIN ANALYZE actually executes the query and reports actual runtime statistics.

For deeper investigation:

EXPLAIN (ANALYZE, BUFFERS)
SELECT ...
Enter fullscreen mode Exit fullscreen mode

This can help identify CPU and I/O-related bottlenecks.

Some important things to look for include:

  • Sequential Scan
  • Index Scan
  • Actual execution time
  • Estimated vs actual rows
  • Loops
  • Buffer hits
  • Buffer reads

A Sequential Scan isn't automatically bad. If a query needs a large percentage of the table, PostgreSQL may correctly decide that scanning the table is cheaper than using an index.

๐Ÿง  CTEs

CTE stands for Common Table Expression.

It allows us to define a named intermediate query:

WITH active_users AS (
    SELECT *
    FROM users
    WHERE status = 'active'
)
SELECT *
FROM active_users;
Enter fullscreen mode Exit fullscreen mode

CTEs can make complex SQL easier to read and can also be useful for recursive queries and multi-step data processing.

๐Ÿ“ˆ Window Functions

Window functions allow calculations across related rows without collapsing the result into one row per group.

For example:

SELECT
    name,
    salary,
    RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;
Enter fullscreen mode Exit fullscreen mode

Common window functions include:

ROW_NUMBER()
RANK()
DENSE_RANK()
LAG()
LEAD()
SUM() OVER()
AVG() OVER()
Enter fullscreen mode Exit fullscreen mode

This is an important distinction:

GROUP BY
โ†’ reduces/groups rows

Window Function
โ†’ keeps rows + calculates across them
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฑ Other PostgreSQL Features

Some other concepts worth knowing as a backend developer are:

Arrays

PostgreSQL can store arrays directly:

skills TEXT[]
Enter fullscreen mode Exit fullscreen mode

UUID

UUIDs provide globally unique identifiers and can be useful in distributed systems.

Views

A view is a saved query that behaves like a virtual table.

Functions and Procedures

PostgreSQL allows reusable logic to execute inside the database.

Extensions

Extensions add additional functionality to PostgreSQL.

Examples include:

pgcrypto
pg_trgm
PostGIS
citext
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ PostgreSQL in a Node.js Backend

A typical architecture can look like:

Client
   โ†“
Express API
   โ†“
Controller
   โ†“
Service
   โ†“
Prisma
   โ†“
Connection Pool
   โ†“
PostgreSQL
Enter fullscreen mode Exit fullscreen mode

Connection pooling is important because creating a new database connection for every request is expensive.

A connection pool allows the application to reuse a controlled number of database connections.

๐Ÿงช A Real Query Optimization Example

One practical query I work with filters a large consumer table using conditions such as:

WHERE DIV_CODE IN (...)
  AND BILL_CYC_CD = 'SBM'
  AND CON_STATUS IN (...)
  AND SUPPLY_TYPE NOT BETWEEN 50 AND 59
Enter fullscreen mode Exit fullscreen mode

Instead of immediately creating an index, the better approach is:

EXPLAIN
   โ†“
EXPLAIN ANALYZE
   โ†“
EXPLAIN (ANALYZE, BUFFERS)
   โ†“
Inspect execution plan
   โ†“
Check existing indexes
   โ†“
Optimize
   โ†“
Measure again
Enter fullscreen mode Exit fullscreen mode

This taught me an important backend lesson:

Performance optimization should be measurement-driven, not guess-driven.

๐ŸŽฏ Key Takeaways

The PostgreSQL concepts I consider most important for backend interviews are:

ACID
MVCC
Indexes
EXPLAIN ANALYZE
Connection Pooling
JSONB
JOINs
CTEs
Window Functions
Constraints
Enter fullscreen mode Exit fullscreen mode

Knowing CRUD tells us how to interact with a database.

Understanding transactions, concurrency, indexing, query plans, and connection management helps us understand how to build reliable and performant backend systems.

That's the difference I'm aiming for: not just knowing how to write a query, but understanding what PostgreSQL is doing underneath it.

PostgreSQL #NodeJS #BackendDevelopment #Database #SQL #WebDevelopment #Programming

Top comments (0)