Skip to content

PostgreSQL ​

PostgreSQL Data Types ​

πŸ”’ Numeric Types ​

TypeRange/Use CaseStorage
SMALLINT-32,768 to 32,7672 bytes
INT / INTEGER-2.1B to 2.1B4 bytes
BIGINTΒ±9.2 quintillion8 bytes
DECIMAL(p,s)Exact decimal (e.g., DECIMAL(10,2) for currency)Variable
NUMERIC(p,s)Same as DECIMAL (synonym)Variable
REAL6-digit precision (float)4 bytes
DOUBLE PRECISION15-digit precision (double)8 bytes
SERIALAuto-incrementing INT (1 to 2.1B)4 bytes
BIGSERIALAuto-incrementing BIGINT (1 to 9.2 quintillion)8 bytes

πŸ“… Date/Time Types ​

TypeRange/Use CaseStorage
DATE4713 BC to 5874897 AD4 bytes
TIME00:00:00 to 24:00:00 (no timezone)8 bytes
TIMESTAMP4713 BC to 294276 AD (no timezone)8 bytes
TIMESTAMPTZTIMESTAMP with timezone8 bytes
INTERVALTime intervals (e.g., '1 day')16 bytes

πŸ“œ String Types ​

TypeMax Size/Use CaseStorage
CHAR(n)Fixed-length (padded with spaces)1 byte/char
VARCHAR(n)Variable-length (up to 1GB)1 byte/char + overhead
TEXTUnlimited lengthVariable
BYTEABinary data (e.g., images)Variable

πŸŒ€ Other Types ​

TypeUse CaseStorage
BOOLEANTRUE/FALSE/NULL1 byte
JSONJSON data (plain text)Variable
JSONBBinary JSON (indexable, faster query)Variable
UUIDUniversally Unique Identifier16 bytes
ENUMPredefined list (e.g., ENUM('red','blue'))4 bytes
ARRAYStores arrays (e.g., INT[])Variable

Key Notes: ​

  • SERIAL vs IDENTITY: Prefer IDENTITY (SQL-standard) in modern PG:

    sql
    CREATE TABLE users (
        id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
        name TEXT
    );
  • JSONB > JSON: Use JSONB for indexing/querying; JSON for literal storage.

  • TEXT over VARCHAR: TEXT is preferred for unbounded strings (no performance difference).

UUID ​

UUIDv7 in PostgreSQL 17.5: Use Cases & Implementation

πŸ”Ή Key Features of UUIDv7 ​

βœ… Time-Ordered – First 48 bits = Unix timestamp (milliseconds).
βœ… Efficient Indexing – Reduces fragmentation vs. random UUIDv4.
βœ… No MAC Exposure – Unlike UUIDv1, it doesn’t leak hardware info.
βœ… Collision Resistant – Remaining bits are random/sequential.

Example UUIDv7:

018f0a24-1e6b-7f00-9c3d-2f4e5d6c7b8a
^^^^^^^^ (timestamp prefix)

πŸ”Ή How to Use UUIDv7 in PostgreSQL 17.5 ​

1. Enable uuid-ossp Extension ​

sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

2. Generate UUIDv7 ​

sql
SELECT uuid_generate_v7();  -- Time-ordered UUID

3. Use as Default Primary Key ​

sql
CREATE TABLE orders (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
    user_id UUID,
    amount DECIMAL(10,2)
);

πŸ”Ή Performance Considerations ​

  • Indexing: ~16% larger than BIGINT but better than random UUIDv4.
  • Storage: 16 bytes (same as other UUIDs).
  • Benchmark: Inserts are ~2x faster than UUIDv4 in indexed tables.

Handcrafted with πŸ’œ in Bangalore, India