Postgresql Simplified

작성자

카테고리:

← 피드로
DEV Community · Krati Joshi · 2026-08-11 개발(SW)

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 = '[email protected]';

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 = '[email protected]';

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

원문에서 계속 ↗

추출 본문 · 출처: dev.to · https://dev.to/joshikrati03/postgresql-simplified-m8o

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다