Exploring PL/pgSQL: Strings, arrays, recursion, and parsing JSON
notes.eatonphil.com
notes.eatonphil.com
More recently I learned that you can also write python functions 'natively' (ie no need to install any modules) in postgres, which honestly seems like a much better idea all round than learning PL/pgSQL, even if you don't know any python already.
Can anyone comment on if there are any feature differences between the two, or why that might be wrong?
I wrote a lot of Oracle PL/SQL at my last job* and I will say if you're not familiar with that kind of database language I would highly recommend Python over it. First think about whether you really want to have business logic in your database and then choose what you know best.
*: It was messy and I am glad that that job is over.
Another consideration is portability. Perhaps you're migrating to or from another database that has its own Procedural Language. You're probably going to have an easier time dealing with PL -> PL when migrating stored procedures.
There are times when it's just easier and more intuitive to glue a bunch of SQL together using a PL, than whipping up some python/js/etc...
Also keep in mind that depending on the DB, you may get a scripting language like python or js, but you can't use any third party libraries.
PL/pgSQL has a fairly low complexity in my experience compared to many other languages... More often it's the SQL/database conceptual part that I see people struggling with most. PL/pgSQL in some ways makes that a little more front and center... But if you're writing functions and procedures you're really not going to escape having to come to terms with the database. May not be the issue you were experiencing, but expect that to be more common.
(https://www.postgresql.org/docs/14/runtime-config-resource.h...)
Bunch of SQL scripts with "CREATE OR REPLACE" and cross your fingers? Make a schema that a script can wholly own, then blow the whole thing away and reinitialize in a transaction?
It seems like with DBT we're getting to declarative table construction, but I'm looking forward to declarative maintenance of other data warehouse aspects.
Ah that makes sense. I guess you could hack around that by having a script reading all your declarations that are checked into code and removing any that exist in the db that aren't in that list, or something.
I've used this a lot with SQL Server and used the Redgate tools for that and that worked very well. For PostgreSQL, it seems there's a free tool from devart: dbForge Schema Compare [0]. I've never used it and have no idea how well it works...
[0]: https://www.devart.com/dbforge/postgresql/schemacompare/down...