AI Hints
Home›PostgreSQL Tutorial›Project: Harbor Events, Part 2
PostgreSQL · Lesson 111 of 120

Project: Speed It Up

Indexes and a summary view.

⏱ 2 min read▶ Video at 1:40:36🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

The site loads seats left for every event on every page view. And guests look up their bookings by email. Let's index for both, and cache the totals.

  • Seats left, often
  • Lookups by email
  • Index and cache

Index and summarise

An index on email speeds up guest lookups. A materialized view stores seats sold per event. A unique index lets it refresh concurrently, without blocking readers. Run it. The busiest events, read from the summary.

query.sqlsql
CREATE INDEX bookings_email ON bookings (email);

CREATE MATERIALIZED VIEW event_sales AS
SELECT event_id, count(*) AS bookings, sum(seats) AS sold
FROM bookings GROUP BY event_id;
CREATE UNIQUE INDEX ON event_sales (event_id);

REFRESH MATERIALIZED VIEW CONCURRENTLY event_sales;
SELECT * FROM event_sales ORDER BY sold DESC LIMIT 3;
Output
CREATE INDEX
SELECT 60
CREATE INDEX
REFRESH MATERIALIZED VIEW
 event_id | bookings | sold
----------+----------+------
       58 |       24 |   54
       17 |       27 |   53
       10 |       23 |   51
(3 rows)
Also uses _setup.sql
CREATE TABLE venues (
  venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name     text NOT NULL UNIQUE,
  city     text NOT NULL,
  capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
  event_id  integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  venue_id  integer NOT NULL REFERENCES venues,
  title     text NOT NULL,
  starts_at timestamptz NOT NULL,
  price     numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
  booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  event_id   integer NOT NULL REFERENCES events,
  email      text NOT NULL,
  seats      integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
  booked_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
  ('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
  ('Lighthouse Loft', 'Lunenburg', 60);

INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
       (ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
              'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
       timestamptz '2026-01-02 19:30' + g * interval '2 days',
       (ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;

SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
       timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;
Also uses _empty
1

Is the email index used?

Explain shows the plan for one guest's bookings. Run it. A bitmap index scan on the new email index.

query.sqlsql
CREATE INDEX bookings_email ON bookings (email);
ANALYZE bookings;

EXPLAIN (COSTS OFF)
SELECT event_id, seats FROM bookings WHERE email = 'guest7@mail.com';
Output
CREATE INDEX
ANALYZE
                      QUERY PLAN
-------------------------------------------------------
 Bitmap Heap Scan on bookings
   Recheck Cond: (email = 'guest7@mail.com'::text)
   ->  Bitmap Index Scan on bookings_email
         Index Cond: (email = 'guest7@mail.com'::text)
(4 rows)
Also uses _setup.sql
CREATE TABLE venues (
  venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name     text NOT NULL UNIQUE,
  city     text NOT NULL,
  capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
  event_id  integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  venue_id  integer NOT NULL REFERENCES venues,
  title     text NOT NULL,
  starts_at timestamptz NOT NULL,
  price     numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
  booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  event_id   integer NOT NULL REFERENCES events,
  email      text NOT NULL,
  seats      integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
  booked_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
  ('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
  ('Lighthouse Loft', 'Lunenburg', 60);

INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
       (ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
              'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
       timestamptz '2026-01-02 19:30' + g * interval '2 days',
       (ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;

SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
       timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;
Also uses _empty
1

Summary

Indexes and a materialized view keep Harbor's busiest pages fast. Next, booking seats safely.

Key takeaways
  • Email index
  • Materialized view
  • CONCURRENTLY
Next up: Project: Safe Bookings — never oversell