An open-source relational database (widely used in backend)
Powerful & prod-ready
-- Flexible searching (General JSONB searching)
-- Indexes all keys and values inside JSONB
CREATE INDEX idx_users_profile
ON users USING GIN(profile);
-- Hot field (creates an expression B-tree index)
-- Indexes only extracted TEXT
CREATE INDEX idx_users_country
ON users ((profile->>'country'));
Philosophy: Correct first, fast second (Very strict about data integrity)
Use MVCC (reads don’t block writes, writes create new versions)
Client (App / pgAdmin)
↓
PostgreSQL Server
├── Postmaster (main process)
├── Backend process (per connection)
├── Shared Buffers (cache)
├── WAL (Write Ahead Log)
└── Background workers
| JSON | JSONB |
|---|---|
| Stored as text | Stored as binary |
| Slow to query | Fast |
| No index | Can be indexed |
| Rarely used | Use this always |
Example create & insert:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT,
profile JSONB
);
INSERT INTO users (email, profile)
VALUES (
'[email protected]',
'{"age":22,"country":"MY","skills":["Java","Spring"]}'
);
Example select:
// Get the value
SELECT profile->>'country' FROM users;
// -> returns JSON
// ->> returns TEXT
// JSON containment
SELECT *
FROM users
WHERE profile @> '{"country":"MY"}';
// Use in filter
SELECT *
FROM users
WHERE profile->>'country' = 'MY';
// Array contains
SELECT *
FROM users
WHERE profile->'skills' ? 'Java';
Before data changes: