‹ BackHN Continuity

Thread

We need to stop using Stored Procedures

17 points · 32 comments · snikolaev

  1. saxenaabhi · · focus · HN ↗
    This is very poorly written and is wrong on basic facts.

    > So what does a stored procedure get us? > Absolutely nothing! Well, I mean, headache for one.

    > ... we have to deploy migrations to update our queries, and we have to run diff migration_for_my_sproc migration_for_my_sproc_n to see how things changed

    1) You can version sql functions in your repo alongside your code and deploy sql function alongside your db migrations(even in the same transaction).

    Have one file per sql function and you can also compute checksums to speed it up if like me you have a repo with 600 stored procedures.

    2) With stored procedures you have no need to to db.startTransaction on server when executing multiple statements and wait for db round trips. That's often the biggest reason for preferring stored procedures.

    3) I have seen systems in healthcare/finance where different teams have no access to underlying tables and the db only exposes sql procedures. Every-time a procedure is called it also adds a log entry to an audit table.

    Databases are a great piece of technology! Learning how to use them properly can have huge payoff in terms of business value generation.

    EDIT: not mentioned in this article but people often mention testing difficulties with stored procedures.

    You can have normal vitest tests testing your postgres functions with in-memory pglite.

    1. 999900000999 · · focus · HN ↗
      I agree. The author doesn’t understand advanced database concepts and demands others stop them.

      Stored procedures ensure consistency. You know to add an item using one proc call. Not 20 different SQL calls.

      Just say you hate SQL. I do. For some god forsaken reason I got placed in a SQL heavy role a while back and was out within months.

      I both understand Postgres is the best solution for most DB use cases and I still reach for Firebase for my personal projects.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.