Prompt

I'm building something with Django and PostgreSQL, what should I watch out for?

Technology / Databases2 observationsLast seen Oct 13, 2026

Latest observation

Oct 13, 2026OpenAI APIWeb search: off

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=True or Meta.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:
    • RunSQL with CREATE 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.

  • JSONField is great, but don’t use it for everything.
  • ArrayField can be useful, but normalized relational data is often better.
  • Use UUIDField if you want non-sequential identifiers, but know it can affect index locality.
  • Use DateTimeField with 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.

  • icontains on large tables can be slow.
  • Consider:
    • trigram indexes (pg_trgm)
    • full-text search
    • functional indexes like Lower(field)

8) Boolean/default/null semantics

Django and PostgreSQL have good support, but consistency matters.

  • Be intentional about null=True vs 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_AGE is 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 ANALYZE on 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:

  1. Is this query indexed?
  2. Is Django generating the SQL I expect?
  3. Could this migration lock a big table?
  4. Can the database enforce this rule better than the app?
  5. 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.

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.