Prompt
I'm building something with Django and PostgreSQL, what should I watch out for?
Latest observation
Django + PostgreSQL is a solid combo. The main things to watch out for are usually less about “can it work?” and more about performance, schema design, and how Django’s abstractions map to Postgres behavior.
Key things to watch out for
1) N+1 queries
Very common in Django.
- Use
select_related()for foreign keys / one-to-one. - Use
prefetch_related()for many-to-many / reverse relations. - Watch templates and serializers too; they can hide repeated queries.
2) Missing indexes
PostgreSQL is fast, but only if your queries are indexed appropriately.
- Add indexes on fields used in
WHERE,JOIN,ORDER BY. - In Django, use
db_index=TrueorMeta.indexes. - For composite lookups, create multi-column indexes, not just single-field ones.
- Don’t over-index: inserts/updates get slower.
3) Migrations on large tables
Some Django migrations can lock tables or take a long time.
- Adding a column with a default can be expensive depending on PG/Django versions and how it’s done.
- Adding indexes can block writes unless you use concurrent index creation.
- For big tables, consider:
RunSQLwithCREATE INDEX CONCURRENTLY- phased migrations
- backfilling data in batches
4) Transaction and locking behavior
PostgreSQL is strong on concurrency, but locks still matter.
- Long transactions can block vacuum and other writes.
- Be careful with
select_for_update()—it’s useful, but can create contention. - Avoid doing external API calls inside DB transactions if possible.
5) Data types matter
Use PostgreSQL-native types wisely.
JSONFieldis great, but don’t use it for everything.ArrayFieldcan be useful, but normalized relational data is often better.- Use
UUIDFieldif you want non-sequential identifiers, but know it can affect index locality. - Use
DateTimeFieldwith timezone support consistently.
6) ORM-generated SQL can be surprising
Django’s ORM is good, but not always optimal.
- Check generated SQL for complex queries.
- Use
.explain()to inspect query plans. - Be careful with chained filters, annotations, subqueries, and aggregations.
- Sometimes a custom SQL query is simpler and faster.
7) Full-text search and case-insensitive search
PostgreSQL supports these well, but you need to design for them.
icontainson large tables can be slow.- Consider:
- trigram indexes (
pg_trgm) - full-text search
- functional indexes like
Lower(field)
- trigram indexes (
8) Boolean/default/null semantics
Django and PostgreSQL have good support, but consistency matters.
- Be intentional about
null=Truevs empty string. - For booleans, keep nullability clear unless three-state logic is truly needed.
- Use proper defaults and constraints.
9) Constraints are your friend
Let the database enforce integrity.
- Use
UniqueConstraint,CheckConstraint, foreign keys. - Don’t rely only on application logic.
- This reduces race conditions and inconsistent data.
10) Connection management
PostgreSQL connections can be a bottleneck.
- Use PgBouncer if you expect many app connections.
- Make sure
CONN_MAX_AGEis set appropriately. - Watch out for connection leaks in workers / async jobs.
11) Vacuum / bloat / maintenance
PostgreSQL needs routine maintenance, especially under heavy update/delete workloads.
- Monitor table and index bloat.
- Autovacuum usually helps, but high-churn tables may need tuning.
- Avoid unnecessary updates to large rows.
12) Query performance at scale
A query that’s fine with 10k rows may struggle at 10M.
- Use pagination carefully.
- Prefer keyset pagination over offset pagination for large datasets.
- Be cautious with wide
ORDER BY+OFFSET. - Measure, don’t guess.
Practical best practices
- Turn on query logging in development.
- Use Django Debug Toolbar locally.
- Run
EXPLAIN ANALYZEon slow queries. - Add tests for model constraints and important query behavior.
- Review migrations before applying them to production.
- Keep an eye on slow endpoints and query counts.
A good rule of thumb
If you’re unsure, ask:
- Is this query indexed?
- Is Django generating the SQL I expect?
- Could this migration lock a big table?
- Can the database enforce this rule better than the app?
- Will this still perform with 100x more data?
If you want, I can give you:
- a Django/Postgres performance checklist,
- common anti-patterns,
- or a production setup guide for this stack.