Combining an immutable movement journal with a materialized balance projection for sub-millisecond reads
A pure movement ledger gives complete auditability, but scanning millions of historical journal rows on every catalog page view would bottleneck PostgreSQL CPU. To build inventory management software that delivers both strict accounting integrity and sub-millisecond storefront reads, store immutable records in `inventory_movements` while updating a materialized `stock_balances` table within the exact same database transaction.
In the schema below, PostgreSQL computes `available_qty` automatically as a stored generated column (`on_hand_qty - reserved_qty`) and enforces `CHECK (on_hand_qty >= 0)` and `CHECK (reserved_qty <= on_hand_qty)`. Even if fifty checkout workers race for the last two units of a popular SKU simultaneously, the database engine guarantees zero negative balances.
During checkout reservation, the application executes an atomic `UPDATE stock_balances SET reserved_qty = reserved_qty + $qty WHERE sku_id = $1 AND location_id = $2 AND (on_hand_qty - reserved_qty) >= $qty RETURNING available_qty` alongside inserting the corresponding `RESERVE` movement row—eliminating distributed lock complexity while preserving linearizable correctness.
db/schema/inventory_ledger.sql (sql)
-- Double-Entry Inventory Ledger + Real-Time Materialized Balance
CREATE TABLE warehouses (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
code TEXT UNIQUE NOT NULL, -- e.g. 'US-EAST-01'
name TEXT NOT NULL,
timezone TEXT NOT NULL DEFAULT 'UTC'
);
CREATE TABLE storage_locations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
warehouse_id UUID NOT NULL REFERENCES warehouses(id),
location_code TEXT NOT NULL, -- e.g. 'RECEIVING', 'A-04-B2', 'SHIPPED_VIRTUAL'
location_type TEXT NOT NULL CHECK (
location_type IN ('SUPPLIER', 'RECEIVING', 'BIN', 'DAMAGED', 'CUSTOMER_SHIPPED')
),
is_sellable BOOLEAN NOT NULL DEFAULT false,
UNIQUE (warehouse_id, location_code)
);
CREATE TABLE stock_balances (
sku_id UUID NOT NULL,
location_id UUID NOT NULL REFERENCES storage_locations(id),
lot_id UUID NOT NULL,
on_hand_qty NUMERIC(14, 4) NOT NULL DEFAULT 0,
reserved_qty NUMERIC(14, 4) NOT NULL DEFAULT 0,
-- Generated column ensures available stock stays mathematically consistent
available_qty NUMERIC(14, 4) GENERATED ALWAYS AS (on_hand_qty - reserved_qty) STORED,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (sku_id, location_id, lot_id),
CONSTRAINT chk_non_negative_on_hand CHECK (on_hand_qty >= 0),
CONSTRAINT chk_valid_reservation CHECK (reserved_qty >= 0 AND reserved_qty <= on_hand_qty)
);
CREATE TABLE inventory_movements (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
idempotency_key TEXT UNIQUE NOT NULL,
movement_type TEXT NOT NULL CHECK (
movement_type IN ('RECEIPT', 'PUTAWAY', 'TRANSFER', 'RESERVE', 'RELEASE', 'SHIP', 'ADJUSTMENT')
),
sku_id UUID NOT NULL,
lot_id UUID NOT NULL,
from_location_id UUID NOT NULL REFERENCES storage_locations(id),
to_location_id UUID NOT NULL REFERENCES storage_locations(id),
quantity NUMERIC(14, 4) NOT NULL CHECK (quantity > 0),
reference_type TEXT NOT NULL, -- 'PURCHASE_ORDER', 'SALES_ORDER', 'CYCLE_COUNT'
reference_id UUID NOT NULL,
performed_by UUID NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_movements_sku_time ON inventory_movements (sku_id, created_at DESC);