Skip navigation
Big moment for Postgres!
0:00 / 0:25

Big moment for Postgres!

Avi Chawla

@_avichawla78K followers

Visit on X
2,174 likesDownloadOriginal post

187,280 views

Big moment for Postgres! AI coding tools have been surprisingly bad at writing Postgres code. Not because the models are dumb, but because of how they learned SQL in the first place. LLMs are trained on the internet, which is full of outdated Stack Overflow answers and quick-fix tutorials. So when you ask an AI to generate a schema, it gives you something that technically runs but misses decades of Postgres evolution, like: - No GENERATED ALWAYS AS IDENTITY (added in PG10) - No expression or partial indexes - No NULLS NOT DISTINCT (PG15) - Missing CHECK constraints and proper foreign keys - Generic naming that tells you nothing But this is actually a solvable problem. You can teach AI tools to write better Postgres by giving them access to the right documentation at inference time. This exact solution is actually implemented in the newly released pg-aiguide by Tiger Data - Creators of TimescaleDB, which is an open-source MCP server that provides coding tools access to 35 years of Postgres expertise. In a gist, the MCP server enables: - Semantic search over the official PostgreSQL manual (version-aware, so it knows PG14 vs PG17 differences) - Curated skills with opinionated best practices for schema design, indexing, and constraints. I ran an experiment with Claude Code to see how well this works, and worked with the team to put this together. Prompt: "Generate a schema for an e-commerce site twice, one with the MCP server disabled, one with it enabled. Finally, run an assessment to compare the generated schemas." The run with the MCP server led to: - 420% more indexes (including partial and expression indexes) - 235% more constraints - 60% more tables (proper normalization) - 11 automation functions and triggers - Modern PG17 patterns throughout The MCP-assisted schema had proper data integrity, performance optimizations baked in, and followed naming conventions that actually make sense in production. pg-aiguide works with Claude Code, Cursor, VS Code, and any MCP-compatible tool. It's free and fully open source. I have shared the repo in the replies!

36 Comments

Comments from the original post on X (Twitter)

  • Avi Chawla's profile picture

    @_avichawla

    GitHub repo: (don't forget to star it ⭐ )

    83 likes

  • Avi Chawla's profile picture

    @_avichawla

    If you found it insightful, reshare it with your network. Find me → @_avichawla Every day, I share tutorials and insights on DS, ML, LLMs, and RAGs.

    4 likes

  • Akshay 🚀's profile picture

    @akshay_pachaar

    One thing worth adding here: the version-aware search is underrated. PG14 and PG17 handle things like JSON operators and identity columns differently. So getting the right docs for your specific version prevents subtle bugs that only show up in production.

    13 likes

  • Badbrothers's profile picture

    @Badbrothers__

    Can we make this a skill to not fill up my context 😭

    11 likes

  • Juha Palomäki's profile picture

    @jpalomaki

    "420% more indexes" - I don't think is a very good metric. More interesting would be to know if it can figure out really good indexes. Like ones where single index serves multiple queries and if it can setup the queries so that they benefit from existing indexes.

    8 likes

  • EJ Campbell's profile picture

    @ejc3

    Try Opus 4.5 without the MCP and see how it does when you specify what version of Postgres to use. Your hypothesis about how these models are trained is incorrect. It’s not just reading stack overflow posts. They get literal experts in the field to evaluate the model’s output to fine tune it.

    5 likes

  • Somi's profile picture

    @somi_ai

    This pattern extends beyond Postgres. We see the same issue with any rapidly evolving API or framework. Models trained on 2022 docs suggest deprecated patterns for 2024 libraries. MCP servers as domain-specific knowledge injectors is the right architectural fix. Beats fine-tuning because you can version the knowledge separately from the model.

    3 likes

  • Gilberts Ahumada's profile picture

    @gilbertsahumada

    does it work with ORMs? like drizzle or prisma

    2 likes

  • Rapid's profile picture

    @0xRapid

    Timescaledb was here for a long time. One of the best tools in the market atm

    2 likes

  • Everlier's profile picture

    @Everlier

    Good old SQL Chat in Harbor ( worked reasonably well for everything I trrown at it. For more compicated scenarios - coding agents were usually able to debug and fix performance issues granted connection creds (don't worry - never in production)

    1 like

  • Ivan ⚡️ | 職人コーディング's profile picture

    @Ithora

    As more developers skips stackoverflow and look for answers directly on AI searches this will get worser for all tech not just postgres.

    1 like

  • Devendra Haldankar's profile picture

    @HaldankarDeven

    @ai_with_gopi this can be replicated for any other framework or tool. For eg. Sqlalchemy, typescript

    1 like

  • Himanshu Kumar's profile picture

    @codewithimanshu

    Postgres code generation quality shows significant performance variations across models.

  • Muhammad Faraz's profile picture

    @alternatefaraz

    Will check must needed

  • Muhammad Adrees | AI automation engineer's profile picture

    @Itsadreeskhan

    Finally AI that respects Postgres history, not just Stack Overflow copy paste.

  • Eduardo Borges's profile picture

    @duborges

    pg17 isnt modern, pg18 is!

  • Deluxebear's profile picture

    @deluxebear

    great

  • Moksh M. Sharma's profile picture

    @mokshmsharma

    This goes beyond simple RAG for raw documentation; it's about injecting expert judgment and evolutionary context directly into the generation process.

  • Esteban Puerta's profile picture

    @Esteban_Puerta9

    Hell yeah this is a very useful one

  • Conny Brunnkvist's profile picture

    @connyb

    I though the trend last week was that we were deprecating MCP:s in favor of “Skills”