Hacker News new | past | comments | ask | show | jobs | submit login
PostgreSQL adds support for OUT parameters in Procedures (migops.com)
97 points by avi_vallarapu on Oct 15, 2021 | hide | past | favorite | 24 comments



You could already use RECORD to return multiple values from a proc, so this really is only useful for oracle compatibility.


Also for the MySQL community. May companies have hundreds of stored procedures. Rewriting them to not use OUT params just to even try out Postgres can be a big deal. So I welcome this change.


Looks like a lot of potential to build an API right in the database. Just wish the syntax was a bit more modern. Would be nice to have an easy way to just make it a rest server.


I've used Postgraphile to do this. GraphQL not REST, same idea though, autogenerated API from stored procedures, it's pretty neat.

Pros:

  - Can query DB multiple times, conditionally, without making multiple trips to DB, since your code for a certain procedure is all in the DB.
  - Procedures are accessible using any DB client.
Cons:

  - Version control of these procedures is not as nice as normal code. Graphile Starter has some tools for snapshotting the DB schema that help, but the DX is still not great.
  - Scaling your DB is more costly than scaling compute, so from a cost/scaling perspective this might not be the best idea.


I'd be nervous about the testability/verifiability of it. I like treating the DB as infrastructure, and I get nervous when the infrastructure gets too smart.

Maybe I'm just stuck in the olden days and haven't yet embraced the brave new world where no one can run a full local instance because it depends on queues and storage backends and whatever else supplied by a cloud vendor. But even in my little world, I feel like I experience this with overly-smart Jenkins pipelines that can't really be executed except in production or an expensive-to-maintain clone of production.


I think the sweet spot for Database smartness is ensuring data integrity, at close to domain level.

Having views instead of tables (hiding the actual implementation details), having procedures to interface with underlying tables and having procedures to ensure data integrity for incoming changes is a great use for procedures.

Too often, I see databases used as simply storage boxes, when they are capable of being much more.


I think most people have seen the app server crash and burn while the data is nice and safe in the DB. Seems sketchy to merge them. But some some usage it would be simpler.


You might be interested in PostgREST ( https://postgrest.org/en/v8.0/)

> PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API. The structural constraints and permissions in the database determine the API endpoints and operations.


Anyone aware of a quality tool like this for SQL Server?

I've been looking for something like this for a long time?


I haven’t used it but Hasura[0] supports MS SQL [0] https://hasura.io/docs/latest/graphql/core/databases/ms-sql-...


If multiple applications with limited functionality are interfacing with the db, then adding certain logic with procedures can be a good solution for consistency.

Procedures can be triggered by inserts/updates/deletes. This means logic can be replayed. There might be a table "IncomingActions" that triggers a set of procedures. Procedures might sanitize values on updates, update various specific materialized views.

Thinking about it generally, consider the db as storing system state, and a set of actions that updates the state. So the db is a sort of state machine, and the triggers/procedures define the reachable state space. For example, there might be FK constraints that forbid clients to insert into the table "Orders". Instead, clients could insert into "OrderRequests" which triggers a set of procedures that ensures the entry in "Orders" is legal. In this way, an ORM app could be allowed to create Orders without needing to call procedures, or knowing anything about what makes "Orders" legal. We also get a log in "OrderRequests".

Error handling is ugly though.


Postgraphile turns your database into a GraphQL API: https://www.graphile.org/postgraphile/


It's so awesome even the testimonials have radical names:

    Chad F

    Sam L

    Max D


After being thrown into a codebase written with Oracle PL/SQL procedures outputted directly into Coldfusion tags, this idea gives me PTSD.


Worth noting that Postgres supports many more languages other than PL/SQL, the plv8 extensions allows JavaScript to be used for example.


I originally tried this with a project I am working on, moving as much of the logic as possible into the DB. But abandoned the effort quickly in part because the logic I need to implement was simply too complex and was better expressed & validated in a client side language.

plpgsql performance is a serious issue for anything non trivial.

debugging is slow and challenging.

General rules I apply with procs

* try to keep procs to SQL, not plpgsql. More performant usually and sometimes pg will inline these. But sometimes I've found forcing it as plpgsql is good too, as it effectively 'fences' the query planner just like CTEs used to. As supportive as I am with pg's philosophy on querying hinting, sometimes you need to press your thumb on the scales a bit.

* procs for very complex and subtle queries where performance is not such a concern but readabiltiy of client code or dependent queries is useful. Like a client side function, something whose inputs and outputs are easy to reason about and you can easily build client side regression tests to validate.

* consider using plpgsql proc instead of recursive CTE for things where CTE recursion is needed. I've found for some types of jobs, plpgsql outperforms a recursive CTE. I think bc query planning for recursive CTES is very difficult.


In a previous job many moons ago I was tasked with maintaining an application that used an Oracle DB with stored procedures written in Java.

The server backend was naturally in Java as well so as soon as possible I ripped it out of the db and ran it on the backend.

The code ran twice as fast.

It might have been partly due to the JVM in the db being 1.3 whereas the server had 1.4 but still, nobody bothered to measure and just assumed the code in the db running faster because it was closer to the db.


There is the PostgREST project that allows you to just have a REST API to your database.


PostgREST as others have mentioned is excellent. Check out https://supabase.io for their hosted/managed option with a few more features (and open source tools working in concert).


API right in the database … nice to have an easy way to just make it a rest server

And then forms, and then reports. Hail the return of FoxPro!


but stored procedures are evil


an articulate and well-argued position


they run in daemons


No doubt in some aspects. Business logic may be great most of the times when it is out of the database. However, the theory cannot be applied to numerous legacy applications and also while migrating them all to PostgreSQL.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: