Skip to content
Back to Resources
Technology ReferenceAdvanced60 min•
Chris MaskChris Mask
•Updated

Database Architecture Patterns for Marketplaces

PostgreSQL schema patterns for marketplaces and directories: users with multiple roles, flexible listing attributes, money in integer cents, reviews, messaging and the indexes each one needs.

Who Is This For?

This guide is specifically designed for:

Startup Stage:

MVP & Launch

Building your minimum viable product and preparing for market launch.

Best For Role:

Developers

Technical implementation guides and code examples for developers.

Expected Impact:

Strategic

Medium-term initiatives that build competitive advantages.

Platform: Platform Agnostic
Reading Level: Advanced

What You'll Learn

  • Design flexible user schemas supporting multiple roles
  • Implement JSONB-based listing attributes for category flexibility
  • Structure transaction tables for accurate financial tracking
  • Build review systems with denormalized ratings
  • Create efficient messaging schemas with conversation threading
  • Apply proper indexing strategies for query optimization
  • Use database triggers for maintaining data consistency
  • Keep deleted records and an audit trail for money, bookings and moderation

Prerequisites

  • •Working knowledge of SQL and relational databases
  • •Experience with PostgreSQL or similar RDBMS
  • •Understanding of database indexing concepts
  • •Familiarity with JSONB data types

The database schema is the part of a marketplace that is most expensive to change later. A listing table that only fits one category, prices stored as floats, or separate buyer and seller accounts all work at launch and then force migrations, data clean-up and awkward workarounds once the business adds a second listing type or lets providers also buy.

These are the PostgreSQL patterns we use when we design and build custom marketplaces and directories. They are written for the engineer who will implement them, but the decisions behind each one are worth a founder's attention because they decide what the product can do later without a rebuild.

The short answer: use one users table with roles, a listings table with typed core columns plus a JSONB field for category-specific attributes, money stored as integer cents, a transactions table that records every fee and payout, and reviews tied to completed transactions. Index every foreign key and the columns you filter on, and add caching, replicas or sharding only when measurements show you need them.

The download is a short starter pack (a minimal data model SQL sketch, an API outline, and architecture, search, payments and monitoring checklists). It is a planning aid, not the full schema in this guide.

For a directory project, see how we scope custom directory development around listings, search, and lead enquiries before choosing the data model.

Core Marketplace Schema

Every marketplace requires these five foundational entities. Implement them correctly and scaling becomes incremental rather than requiring complete refactoring.

1. Users Table (Role Flexibility)

Avoid creating separate tables for buyers and sellers. Use a single user table with role flags to support users who are both buyers and sellers.

CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  email VARCHAR(255) UNIQUE NOT NULL,
  email_verified_at TIMESTAMPTZ,
  password_hash VARCHAR(255), -- bcrypt, 10+ rounds

  -- Profile
  first_name VARCHAR(100),
  last_name VARCHAR(100),
  avatar_url TEXT,
  bio TEXT,

  -- Roles (a member may be both buyer AND seller)
  is_seller BOOLEAN DEFAULT false,
  is_admin BOOLEAN DEFAULT false,

  -- Seller-specific fields (null if not seller)
  seller_verified_at TIMESTAMPTZ,
  stripe_connect_id VARCHAR(255),
  seller_rating DECIMAL(3,2),
  total_sales_count INTEGER DEFAULT 0,

  -- Trust & safety
  phone_number VARCHAR(20),
  phone_verified_at TIMESTAMPTZ,
  kyc_verified_at TIMESTAMPTZ,
  account_status VARCHAR(20) DEFAULT 'active', -- active, suspended, banned

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  last_login_at TIMESTAMPTZ
);

-- Indexes for common queries
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_seller ON users(is_seller) WHERE is_seller = true;
CREATE INDEX idx_users_status ON users(account_status);
CREATE INDEX idx_users_stripe ON users(stripe_connect_id) WHERE stripe_connect_id IS NOT NULL;

-- Trigger for updated_at
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER users_updated_at
  BEFORE UPDATE ON users
  FOR EACH ROW
  EXECUTE FUNCTION update_updated_at();

Design rationale:

  1. Single user table: Eliminates complex joins when users act as both buyers and sellers
  2. Nullable seller fields: Only populated when is_seller = true, avoiding wasted space
  3. Trust signals: Email, phone and KYC verification timestamps let you show verified badges and gate payouts
  4. Cached ratings: Denormalized seller_rating enables fast display without aggregation queries

2. Listings Table (Category Flexibility)

Use JSONB for category-specific attributes to avoid rigid column structures that break when adding new listing types.

CREATE TABLE categories (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name VARCHAR(100) NOT NULL,
  slug VARCHAR(100) UNIQUE NOT NULL,
  parent_id UUID REFERENCES categories(id),
  icon VARCHAR(50),
  sort_order INTEGER DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE listings (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  seller_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  category_id UUID NOT NULL REFERENCES categories(id),

  -- Core fields
  title VARCHAR(255) NOT NULL,
  slug VARCHAR(255) UNIQUE NOT NULL,
  description TEXT NOT NULL,

  -- Pricing
  price_cents INTEGER, -- null for "Contact for price"
  currency VARCHAR(3) DEFAULT 'USD',
  pricing_type VARCHAR(20) DEFAULT 'fixed', -- fixed, negotiable, hourly, project

  -- Flexible attributes (JSONB for category-specific fields)
  attributes JSONB DEFAULT '{}',

  -- Media
  images JSONB DEFAULT '[]', -- ["url1", "url2", ...]
  videos JSONB DEFAULT '[]',
  primary_image_url TEXT,

  -- Location (if relevant)
  location_country VARCHAR(2),
  location_city VARCHAR(100),
  location_lat DECIMAL(10,8),
  location_lng DECIMAL(11,8),

  -- Status & visibility
  status VARCHAR(20) DEFAULT 'draft', -- draft, active, sold, archived
  visibility VARCHAR(20) DEFAULT 'public', -- public, unlisted, private
  featured BOOLEAN DEFAULT false,
  featured_until TIMESTAMPTZ,

  -- SEO
  meta_title VARCHAR(60),
  meta_description VARCHAR(160),

  -- Analytics (denormalized for performance)
  view_count INTEGER DEFAULT 0,
  favorite_count INTEGER DEFAULT 0,
  inquiry_count INTEGER DEFAULT 0,

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  published_at TIMESTAMPTZ,
  sold_at TIMESTAMPTZ
);

-- Critical indexes for performance
CREATE INDEX idx_listings_seller ON listings(seller_id);
CREATE INDEX idx_listings_category ON listings(category_id);
CREATE INDEX idx_listings_status ON listings(status) WHERE status = 'active';
CREATE INDEX idx_listings_created ON listings(created_at DESC);
CREATE INDEX idx_listings_featured ON listings(featured, featured_until) WHERE featured = true;

-- JSONB index for attribute filtering
CREATE INDEX idx_listings_attributes ON listings USING gin(attributes);

-- Geospatial index for location-based search
-- (requires the cube and earthdistance extensions; use PostGIS for heavier geo work)
CREATE INDEX idx_listings_location ON listings USING gist(
  ll_to_earth(location_lat, location_lng)
) WHERE location_lat IS NOT NULL;

-- Full-text search (before you need Elasticsearch)
ALTER TABLE listings ADD COLUMN search_vector tsvector;

CREATE INDEX idx_listings_search ON listings USING gin(search_vector);

CREATE TRIGGER listings_search_update
  BEFORE INSERT OR UPDATE ON listings
  FOR EACH ROW
  EXECUTE FUNCTION tsvector_update_trigger(
    search_vector, 'pg_catalog.english', title, description
  );

JSONB implementation examples:

// Furniture listing with specific attributes
await db.listing.create({
  data: {
    title: "Mid-Century Modern Desk",
    categoryId: furnitureCategoryId,
    priceCents: 45000,
    attributes: {
      condition: "Excellent",
      material: "Walnut",
      dimensions: {
        width: 60,
        depth: 30,
        height: 29,
        unit: "inches",
      },
      brand: "Herman Miller",
      year: 1965,
      shippingIncluded: false,
    },
  },
});

// Service listing with different attribute structure
await db.listing.create({
  data: {
    title: "Logo Design Service",
    categoryId: servicesCategory,
    priceCents: 150000,
    pricingType: "project",
    attributes: {
      deliveryTime: "5 days",
      revisions: 3,
      formats: ["PNG", "SVG", "AI", "PDF"],
      commercialUse: true,
      portfolioUrl: "https://...",
      expertise: ["SaaS", "E-commerce", "Tech Startups"],
    },
  },
});

// Query by JSONB attributes
const modernFurniture = await db.listing.findMany({
  where: {
    categoryId: furnitureCategoryId,
    attributes: {
      path: ["material"],
      equals: "Walnut",
    },
  },
});

// Range queries on JSONB fields
const affordableDesks = await db.$queryRaw`
  SELECT * FROM listings
  WHERE category_id = ${furnitureCategoryId}
    AND (attributes->>'dimensions')::jsonb->>'width' <= '72'
    AND price_cents < 100000
  ORDER BY created_at DESC
`;

3. Transactions Table (Financial Tracking)

Track every financial transaction with cent-precision to avoid float rounding errors.

CREATE TABLE transactions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Relationships
  listing_id UUID REFERENCES listings(id),
  buyer_id UUID NOT NULL REFERENCES users(id),
  seller_id UUID NOT NULL REFERENCES users(id),

  -- Amounts (store in cents to avoid float rounding)
  subtotal_cents INTEGER NOT NULL,
  platform_fee_cents INTEGER NOT NULL,
  processing_fee_cents INTEGER DEFAULT 0,
  total_cents INTEGER NOT NULL,
  seller_payout_cents INTEGER NOT NULL,
  currency VARCHAR(3) DEFAULT 'USD',

  -- Payment details
  payment_provider VARCHAR(20) DEFAULT 'stripe', -- stripe, paypal
  payment_intent_id VARCHAR(255),
  charge_id VARCHAR(255),
  transfer_id VARCHAR(255), -- payout to seller

  -- Status tracking
  status VARCHAR(20) DEFAULT 'pending',
  -- pending → processing → completed → refunded/disputed/cancelled

  -- Escrow (if applicable)
  escrow_release_date TIMESTAMPTZ,
  escrow_released_at TIMESTAMPTZ,

  -- Metadata
  buyer_note TEXT,
  seller_note TEXT,
  metadata JSONB DEFAULT '{}',

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  completed_at TIMESTAMPTZ,
  cancelled_at TIMESTAMPTZ
);

CREATE INDEX idx_transactions_buyer ON transactions(buyer_id);
CREATE INDEX idx_transactions_seller ON transactions(seller_id);
CREATE INDEX idx_transactions_listing ON transactions(listing_id);
CREATE INDEX idx_transactions_status ON transactions(status);
CREATE INDEX idx_transactions_created ON transactions(created_at DESC);
CREATE INDEX idx_transactions_payment_intent ON transactions(payment_intent_id);

Critical: Always use integer cents, never floats:

// ✅ Correct
const priceCents = 4599; // $45.99

// ❌ Wrong (float rounding errors will cost you money)
const price = 45.99;

4. Reviews Table (Trust System)

Implement reviews with bidirectional capability and automatic rating aggregation.

CREATE TABLE reviews (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Relationships
  transaction_id UUID NOT NULL REFERENCES transactions(id),
  listing_id UUID REFERENCES listings(id),
  reviewer_id UUID NOT NULL REFERENCES users(id),
  reviewee_id UUID NOT NULL REFERENCES users(id),
  reviewer_type VARCHAR(10) NOT NULL, -- buyer or seller

  -- Review content
  rating INTEGER NOT NULL CHECK (rating BETWEEN 1 AND 5),
  title VARCHAR(100),
  comment TEXT,

  -- Detailed ratings (optional)
  rating_communication INTEGER CHECK (rating_communication BETWEEN 1 AND 5),
  rating_quality INTEGER CHECK (rating_quality BETWEEN 1 AND 5),
  rating_delivery INTEGER CHECK (rating_delivery BETWEEN 1 AND 5),

  -- Response
  response TEXT,
  responded_at TIMESTAMPTZ,

  -- Moderation
  is_verified BOOLEAN DEFAULT false, -- verified purchase
  is_featured BOOLEAN DEFAULT false,
  status VARCHAR(20) DEFAULT 'active', -- active, flagged, removed

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_reviews_transaction ON reviews(transaction_id);
CREATE INDEX idx_reviews_listing ON reviews(listing_id);
CREATE INDEX idx_reviews_reviewee ON reviews(reviewee_id);
CREATE INDEX idx_reviews_rating ON reviews(rating);

-- Prevent duplicate reviews
CREATE UNIQUE INDEX idx_reviews_unique ON reviews(transaction_id, reviewer_id);

-- Trigger to update seller rating when review is added/updated
CREATE OR REPLACE FUNCTION update_seller_rating()
RETURNS TRIGGER AS $$
BEGIN
  UPDATE users
  SET seller_rating = (
    SELECT AVG(rating)::DECIMAL(3,2)
    FROM reviews
    WHERE reviewee_id = NEW.reviewee_id
      AND status = 'active'
  )
  WHERE id = NEW.reviewee_id
    AND is_seller = true;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER reviews_update_rating
  AFTER INSERT OR UPDATE ON reviews
  FOR EACH ROW
  EXECUTE FUNCTION update_seller_rating();

5. Messaging Tables (Conversation System)

Implement threaded messaging with conversation grouping.

CREATE TABLE conversations (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  listing_id UUID REFERENCES listings(id),
  buyer_id UUID NOT NULL REFERENCES users(id),
  seller_id UUID NOT NULL REFERENCES users(id),

  -- Status
  status VARCHAR(20) DEFAULT 'active', -- active, archived, spam

  -- Metadata
  subject VARCHAR(255),
  last_message_at TIMESTAMPTZ,

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE messages (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
  sender_id UUID NOT NULL REFERENCES users(id),

  -- Content
  message TEXT NOT NULL,
  attachments JSONB DEFAULT '[]',

  -- Status
  read_at TIMESTAMPTZ,

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Indexes
CREATE INDEX idx_conversations_buyer ON conversations(buyer_id, last_message_at DESC);
CREATE INDEX idx_conversations_seller ON conversations(seller_id, last_message_at DESC);
CREATE INDEX idx_messages_conversation ON messages(conversation_id, created_at);

-- Prevent duplicate conversations
CREATE UNIQUE INDEX idx_conversations_unique ON conversations(listing_id, buyer_id, seller_id);

Advanced Patterns

Favorites/Wishlists

CREATE TABLE favorites (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  listing_id UUID NOT NULL REFERENCES listings(id) ON DELETE CASCADE,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE UNIQUE INDEX idx_favorites_unique ON favorites(user_id, listing_id);
CREATE INDEX idx_favorites_user ON favorites(user_id, created_at DESC);

-- Trigger to update listing favorite_count
CREATE OR REPLACE FUNCTION update_favorite_count()
RETURNS TRIGGER AS $$
BEGIN
  IF TG_OP = 'INSERT' THEN
    UPDATE listings SET favorite_count = favorite_count + 1 WHERE id = NEW.listing_id;
  ELSIF TG_OP = 'DELETE' THEN
    UPDATE listings SET favorite_count = favorite_count - 1 WHERE id = OLD.listing_id;
  END IF;
  RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER favorites_update_count
  AFTER INSERT OR DELETE ON favorites
  FOR EACH ROW
  EXECUTE FUNCTION update_favorite_count();

Notifications System

CREATE TABLE notifications (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,

  -- Content
  type VARCHAR(50) NOT NULL, -- new_message, review_received, listing_sold, etc.
  title VARCHAR(255) NOT NULL,
  message TEXT,

  -- Links
  link_url TEXT,
  link_text VARCHAR(100),

  -- Status
  read_at TIMESTAMPTZ,

  -- Metadata
  metadata JSONB DEFAULT '{}',

  -- Timestamps
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_notifications_user ON notifications(user_id, created_at DESC);
CREATE INDEX idx_notifications_unread ON notifications(user_id, read_at) WHERE read_at IS NULL;

Analytics Events

CREATE TABLE events (
  id BIGSERIAL PRIMARY KEY,
  user_id UUID REFERENCES users(id),
  session_id UUID,

  -- Event
  event_name VARCHAR(100) NOT NULL,
  event_category VARCHAR(50),

  -- Context
  page_url TEXT,
  referrer_url TEXT,
  user_agent TEXT,
  ip_address INET,

  -- Properties
  properties JSONB DEFAULT '{}',

  -- Timestamp
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_events_user ON events(user_id, created_at DESC);
CREATE INDEX idx_events_name ON events(event_name, created_at DESC);
CREATE INDEX idx_events_created ON events(created_at DESC);

-- To partition by month, declare the table with PARTITION BY RANGE (created_at)
-- and include created_at in the primary key, then create one partition per month:
CREATE TABLE events_2026_11 PARTITION OF events
  FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');

Scaling Strategies by User Count

The user bands below are rough markers. What matters is the symptom (slow reads, connection limits, very large tables), and our scaling guide covers when to add each step. Vendor prices change often, so budget from your providers' current pricing pages.

Phase 1: 0-10K Users (Single Database)

Setup:

  • Single PostgreSQL instance
  • No read replicas needed
  • Basic indexes only

Goal: keep every common query on an index; check slow-query logs weekly

Phase 2: 10K-100K Users (Add Caching)

Setup:

  • Add Redis for session storage and hot data
  • Implement query result caching
  • Add connection pooling (PgBouncer)
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL);

// Cache listing details (1 hour)
async function getListingWithCache(id: string) {
  const cacheKey = `listing:${id}`;

  // Try cache first
  const cached = await redis.get(cacheKey);
  if (cached) return JSON.parse(cached);

  // Cache miss, query database
  const listing = await db.listing.findUnique({
    where: { id },
    include: {
      seller: true,
      category: true,
      reviews: {
        take: 5,
        orderBy: { createdAt: "desc" },
      },
    },
  });

  // Cache for 1 hour
  await redis.setex(cacheKey, 3600, JSON.stringify(listing));

  return listing;
}

// Invalidate cache on update
async function updateListing(id: string, data: any) {
  await db.listing.update({ where: { id }, data });
  await redis.del(`listing:${id}`);
}

Result: repeated reads come from memory instead of the database. Remember to invalidate on every write path, not just the main edit form.

Phase 3: 100K-500K Users (Read Replicas)

Setup:

  • Primary database for writes
  • 1-2 read replicas for queries
  • Route read queries to replicas
import { PrismaClient } from "@prisma/client";

// Write to primary
const dbPrimary = new PrismaClient({
  datasources: { db: { url: process.env.DATABASE_URL } },
});

// Read from replica
const dbReplica = new PrismaClient({
  datasources: { db: { url: process.env.DATABASE_REPLICA_URL } },
});

// Read queries use replica
async function getListings(filters: any) {
  return await dbReplica.listing.findMany({
    where: filters,
    include: { seller: true },
  });
}

// Writes use primary
async function createListing(data: any) {
  return await dbPrimary.listing.create({ data });
}

Result: browsing load moves off the primary. Replicas lag slightly, so read anything a user has just written (a booking, a balance) from the primary.

Phase 4: 500K-1M+ Users (Sharding)

Setup:

  • Shard by user_id or geography only when a single primary can no longer handle writes
  • Separate hot data (active listings) from cold data (archived)
  • Split out a service only when one domain needs to scale on its own
-- Shard 1: Users A-M
CREATE DATABASE marketplace_shard_1;

-- Shard 2: Users N-Z
CREATE DATABASE marketplace_shard_2;

-- Router logic
function getShardForUser(userId: string) {
  const firstChar = userId.charAt(0).toLowerCase()
  return firstChar <= 'm' ? 'shard_1' : 'shard_2'
}

Trade-off: sharding makes cross-shard queries, reporting and migrations much harder, so most marketplaces exhaust partitioning, replicas and caching first.

Common Mistakes and Solutions

Mistake 1: No Indexes on Foreign Keys

Problem: PostgreSQL does not index foreign keys automatically, so lookups and joins on that key scan the whole table.

-- ❌ Bad: No index
CREATE TABLE listings (
  seller_id UUID REFERENCES users(id)
);

-- Query scans entire table
SELECT * FROM listings WHERE seller_id = '...';

-- ✅ Good: Indexed foreign key
CREATE INDEX idx_listings_seller ON listings(seller_id);

Impact: the slowdown is invisible on a small test database and grows with every row, so it tends to surface right after launch traction.

Mistake 2: Mixing Money Representations

Problem: PostgreSQL NUMERIC/DECIMAL is exact, but application code often turns it into a floating-point number, and fee calculations then drift by a cent. Mixing decimals in the database with cents in your payment provider's API makes it worse.

-- ❌ Risky: exact in the database, but easily read into a float in code
price DECIMAL(10,2)

-- ✅ Simpler: integer cents end to end, matching Stripe's API
price_cents BIGINT -- use BIGINT for totals and running balances

Mistake 3: Not Using Partial Indexes

Problem: Indexes on mostly-null columns waste space.

-- ❌ Bad: Indexes all rows
CREATE INDEX idx_listings_featured ON listings(featured);

-- ✅ Good: Only indexes featured listings
CREATE INDEX idx_listings_featured ON listings(featured, featured_until)
WHERE featured = true;

Impact: when only a small share of rows match the condition, the index stays small and cheap to maintain. Check the gain with EXPLAIN ANALYZE on your own data.

Mistake 4: N+1 Query Problems

Problem: Loading related data in loops.

// ❌ Bad: N+1 queries (1 + 100)
const listings = await db.listing.findMany({ take: 100 });
for (const listing of listings) {
  listing.seller = await db.user.findUnique({
    where: { id: listing.sellerId },
  });
}

// ✅ Good: Single query with join
const listings = await db.listing.findMany({
  take: 100,
  include: { seller: true },
});

Mistake 5: Not Using Transactions for Multi-Step Operations

Problem: Data inconsistency when operations fail midway.

// ❌ Bad: No transaction
await db.listing.update({
  where: { id },
  data: { status: "sold" },
});
await db.transaction.create({ data: transactionData });
// If this fails, listing is marked sold but no transaction exists!

// ✅ Good: Atomic transaction
await db.$transaction(async (tx) => {
  await tx.listing.update({
    where: { id },
    data: { status: "sold" },
  });
  await tx.transaction.create({ data: transactionData });
});

Soft Deletes and Audit Trails

Marketplaces need to answer questions long after the fact: who changed this payout, why was this listing removed, what did the booking look like before the dispute. Hard-deleting rows makes those questions unanswerable, and financial records often have legal retention requirements. Two patterns cover most of it.

Soft deletes

Mark records as deleted instead of removing them, and index only the live rows so everyday queries stay fast:

ALTER TABLE listings ADD COLUMN deleted_at TIMESTAMPTZ;

-- Everyday queries only touch live rows
CREATE INDEX idx_listings_live
ON listings(status, created_at DESC)
WHERE deleted_at IS NULL;

SELECT * FROM listings
WHERE deleted_at IS NULL AND status = 'active'
ORDER BY created_at DESC;
  • Filter deleted rows in one place: a database view or an ORM-level default scope, so no screen forgets the condition.
  • Give admins a way to view and restore deleted records.
  • Personal data still has to be erasable on request, so anonymise the personal fields of a soft-deleted user rather than keeping them forever. Confirm retention rules with your lawyer.

Audit trail

Record every change to the tables that carry money or trust decisions (orders, transactions, payouts, listings under moderation):

CREATE TABLE audit_logs (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  table_name VARCHAR(100) NOT NULL,
  record_id UUID NOT NULL,
  action VARCHAR(20) NOT NULL CHECK (action IN ('INSERT', 'UPDATE', 'DELETE')),
  old_data JSONB,
  new_data JSONB,
  changed_by UUID REFERENCES users(id),
  changed_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_audit_table_record ON audit_logs(table_name, record_id);
CREATE INDEX idx_audit_user ON audit_logs(changed_by, changed_at DESC);

CREATE FUNCTION audit_trigger() RETURNS TRIGGER AS $$
BEGIN
  IF TG_OP = 'DELETE' THEN
    INSERT INTO audit_logs (table_name, record_id, action, old_data)
    VALUES (TG_TABLE_NAME, OLD.id, 'DELETE', to_jsonb(OLD));
    RETURN OLD;
  ELSIF TG_OP = 'UPDATE' THEN
    INSERT INTO audit_logs (table_name, record_id, action, old_data, new_data)
    VALUES (TG_TABLE_NAME, NEW.id, 'UPDATE', to_jsonb(OLD), to_jsonb(NEW));
  ELSE
    INSERT INTO audit_logs (table_name, record_id, action, new_data)
    VALUES (TG_TABLE_NAME, NEW.id, 'INSERT', to_jsonb(NEW));
  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER audit_transactions
  AFTER INSERT OR UPDATE OR DELETE ON transactions
  FOR EACH ROW EXECUTE FUNCTION audit_trigger();

A trigger cannot see which signed-in user made the change, so set changed_by from the application (for example with a session variable read inside the trigger) or write audit rows from the application layer for user actions.

Reading Query Plans and Covering Indexes

Before adding an index, check what the database actually does. EXPLAIN ANALYZE runs the query and shows the plan:

EXPLAIN ANALYZE
SELECT l.id, l.title, l.price_cents, u.display_name
FROM listings l
JOIN users u ON l.seller_id = u.id
WHERE l.status = 'active'
  AND l.price_cents BETWEEN 1000 AND 10000
ORDER BY l.created_at DESC
LIMIT 20;
What you see in the planWhat it usually meansWhat to try
Seq Scan on a large tableNo usable indexAdd an index matching the filter
Estimated rows far from actual rowsOutdated statisticsRun ANALYZE on the table
Many rows scanned, few returnedIndex does not match the filterComposite or partial index
Nested Loop over two large tablesExpensive joinIndex the join key, or denormalize
Sort step on every requestOrder not covered by an indexAdd the sort column to the index

For list views that read the same few columns on every request, a covering index lets PostgreSQL answer from the index alone:

CREATE INDEX idx_listings_seller_cards
ON listings(seller_id, status, created_at DESC)
INCLUDE (title, price_cents, primary_image_url);

The trade-off is a bigger index and slower writes, so reserve it for high-traffic, read-heavy queries and confirm the gain with EXPLAIN ANALYZE on your own data.

Monitoring & Optimization

Query Performance Monitoring

-- Enable query logging
ALTER DATABASE marketplace SET log_min_duration_statement = 1000; -- Log queries >1s

-- Find slow queries
SELECT
  query,
  calls,
  total_exec_time,
  mean_exec_time,
  max_exec_time
FROM pg_stat_statements -- requires the pg_stat_statements extension
ORDER BY mean_exec_time DESC
LIMIT 20;

-- Find missing indexes
SELECT
  schemaname,
  tablename,
  attname,
  n_distinct,
  correlation
FROM pg_stats
WHERE schemaname = 'public'
  AND n_distinct > 100
ORDER BY n_distinct DESC;

Database Size Monitoring

-- Table sizes
SELECT
  schemaname,
  tablename,
  pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size,
  pg_total_relation_size(schemaname||'.'||tablename) AS size_bytes
FROM pg_tables
WHERE schemaname = 'public'
ORDER BY size_bytes DESC;

-- Index usage
SELECT
  schemaname,
  tablename,
  indexname,
  idx_scan,
  idx_tup_read,
  idx_tup_fetch,
  pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY idx_scan ASC;

Implementation Roadmap

Timing follows growth, not the calendar. A typical order:

Before the build: design the schema with flexibility (JSONB) and indexes from day one Launch: single database, basic caching Traction: add Redis and read replicas when measurements show the need Scale: partition large tables; shard or split out services only when one workload limits the rest

Next Steps

  1. Schema audit: Review your current schema against these patterns
  2. Index analysis: Identify missing indexes on foreign keys and frequently queried columns
  3. JSONB migration: Convert rigid category-specific columns to JSONB attributes
  4. Caching strategy: Implement Redis caching for hot data paths
  5. Monitoring setup: Enable query logging and performance tracking

Frequently Asked Questions

Should a marketplace use PostgreSQL or a NoSQL database?

For most marketplaces and directories, PostgreSQL. Transactions, payouts and reviews are relational and need atomic updates, and JSONB covers the flexible, category-specific attributes that usually push teams toward NoSQL.

Should buyers and sellers be separate tables?

Usually not. One users table with role flags (or a roles table) lets the same person buy and sell, and keeps login, messaging and reviews in one place. Put seller-only data such as payout account IDs in nullable columns or a separate seller profile table.

When should listing attributes go in JSONB instead of columns?

Keep anything you filter, sort or join on across all categories as a real column (price, status, category, location). Use JSONB for attributes that only some categories have, and add a GIN index or a generated column when you start filtering on a JSONB field often.

Should we delete records or soft delete them?

Soft delete anything tied to money, bookings, reviews or moderation, and keep an audit trail for those tables, so you can resolve disputes and reconcile payouts later. Personal data still needs to be erasable, so anonymise those fields when a user asks to be removed.

Can we change the schema after launch?

Yes, with migrations, but the cost grows with data volume and with every feature built on the old shape. Changes to how money, roles and listings are modelled are the expensive ones, which is why we settle them during scoping.

Get the Data Model Right Before You Build

Schema decisions are part of the scoping work for every custom marketplace and custom directory we build. If your platform is live and the data model is holding you back, a platform audit reviews the schema, indexes and slow queries and tells you what to change first.

Ask us to review your marketplace data model →

How much should your build actually cost?

Get a personalized investment estimate based on your platform type, scope, and timeline.

Open the Investment Calculator
#database-design#postgresql#schema-design#scalability#sql#indexing
Found this helpful? Share it
Share:

About the Author

Chris Mask

Chris Mask

Founder & Marketplace Strategist

Chris Mask is Directorism’s founder and marketplace strategist. He helps founders define operating models, scope platforms, and choose the right build path.