Your database works, until millions of rows show up
With a hundred rows every query is fast, which is why the development database never warns you. How to fill it with millions of records shaped like the real ones and use them to decide indexes, foreign keys, sorting and composite indexes before the system grows.
PostgreSQL, SQL, EXPLAIN ANALYZE, generate_series