What is PostgreSQL? Why is it different from other RDBMS?

PostgreSQL is an open-source, relational database used to store and query structured data using SQL. It is designed for correctness, reliability, and advanced querying, not just basic data storage.

Why is it different from other RDBMS?

  • Strict and standards-compliant: enforces data integrity instead of silently allowing bad data
  • Advanced data types: native support for JSONB, ARRAY, UUID, ENUM, etc.
  • Strong concurrency: uses MVCC, so reads and writes don’t block each other
  • Powerful queries: supports CTEs, window functions, recursive queries
  • Highly extensible: custom types, functions, and extensions (e.g., PostGIS)

Data Types

typeuse caseexamplebonus points
SMALLINTsmall numbersagesaves space
INTEGERcounts, idsuser_countfast, common default
BIGINTlarge idsevent_idsafe for scale
NUMERIC / DECIMALmoney, precisionpriceexact arithmetic
DOUBLE PRECISIONmeasurementsdistancefast, approximate
typeuse caseexamplebonus points
TEXTgeneral stringsnamepreferred default
VARCHAR(n)length-limited textemailsame perf as text
CHAR(n)fixed-widthcountry_codespace-padded, avoid
typeuse caseexamplebonus points
BOOLEANflagsis_activesupports t/f/1/0
typeuse caseexamplebonus points
DATEcalendar datedobno timezone
TIMEtime onlyopening_timerarely used
TIMESTAMPdate + timecreated_atno timezone
TIMESTAMPTZdate + timecreated_attimezone-safe, prefer
typeuse caseexamplebonus points
JSONraw jsonpayloadtext storage
JSONBstructured datametadataindexable, fast
typeuse caseexamplebonus points
TEXT[]small listsrolesavoid joins
INT[]numeric listsscoressimple containment
typeuse caseexamplebonus points
hstorekey-value dataattributeslegacy, use jsonb
typeuse caseexamplebonus points
ENUMfixed statesstatusenforces valid values
typeuse caseexamplebonus points
UUIDunique idsidsafe for distributed systems
typeuse caseexamplebonus points
POINTcoordinateslocationsimple geometry
LINEpathsrouteniche usage
BOXareasregionbounding boxes
CIRCLEradius datacoveragebasic spatial logic

PostgreSQL Schema, Tables, and Constraints (Primary Key, Foreign Key, Unique, Check, NOT NULL, DEFAULT)

Normalization (1NF, 2NF, 3NF, BCNF, 4NF, 5NF) and Denormalization

nfwhat goes wrong (bad design)bad table columnsgood table(s) after fix
1nfmultiple values in one cellusers(id, name, phones)user_phones(user_id, phone)
2nfpartial dependency on composite keyenrollments(student_id, course_id, student_name, course_name)students(student_id, student_name)
courses(course_id, course_name)
enrollments(student_id, course_id)
3nfindirect (transitive) dependencyusers(user_id, city_id, city_name)users(user_id, city_id)
cities(city_id, city_name)
bcnfnon-key column determines anotherclasses(student, subject, teacher)subjects(subject, teacher)
enrollments(student, subject)
4nfmixing multiple independent facts in one tableuser_profile(user, skill, hobby)user_skills(user, skill)
user_hobbies(user, hobby)
5nfa fact that can be fully derived by joining other tablesspr(supplier, product, region)supplier_product(supplier, product)
supplier_region(supplier, region)
product_region(product, region)
denormalizationintentional duplication for speednormalized joins across tablesorders(order_id, user_id, city_name)

Views, Materialized Views, and Temporary Tables

View

  • Virtual table (stores SQL, not data)
  • Always shows latest data
  • Used for abstraction, reuse, security
  • Does not improve performance

Materialized View

  • Stores result of a query
  • Fast reads, but data can be stale
  • Must be refreshed manually
  • Used for analytics and heavy queries

Temporary Table

  • Real table, exists only for a session
  • Automatically dropped
  • Used for intermediate / scratch data
featureviewmaterialized viewtemp table
stores datanoyesyes
always freshyesnoyes
performancenormalfastfast
needs refreshnoyesno
scopeglobalglobalsession

Functions, Stored Procedures and Triggers

Function

  • a database object that returns a value
  • used for reusable logic or calculations
  • can be called inside sql queries
  • calculate tax, format strings, validate input
  • select calculate_tax(1000);

Stored Procedure

  • a database object that performs actions
  • does not return a value
  • used for multi-step or transactional workflows
  • transfer money between accounts
  • run batch updates
  • call transfer_money(1, 2, 500);

Trigger

  • automatically executes when a table event occurs
  • fires on insert, update, or delete
  • always calls a trigger function
  • auto-update updated_at timestamp
  • insert audit logs on data changes
  • enforce complex rules not possible with constraints