News for hackers. Stuff that matters.
Re:Type Constraints in 65 lines of SQL by oliverrice on Friday February 17, 2023 @10:11AM (#34835534)
hi, author here happy to answer any questions

another approach that works great is to use `create type`[1] with an `input_function` and `output_function` to build on top of an existing scalar type. For example, using that method would allow semver to be persisted as a string natively. The only downside to that is you have to be superuser.

[1] https://www.postgresql.org/docs/current/sql-createtype.html

Re:Type Constraints in 65 lines of SQL by iddan on Friday February 17, 2023 @12:41PM (#34837753)
Why are these practices not common with web developers? What’s held them back in recent years using it?
Re:Type Constraints in 65 lines of SQL by weird-eye-issue on Friday February 17, 2023 @12:55PM (#34837968)
Way easier handling this in the application code. If I had a developer join my team then commit this into the codebase rather than just using the ORM that everyone else is familiar with they would be fired UNLESS there was a very good reason for doing so
Re:Type Constraints in 65 lines of SQL by ericHosick on Friday February 17, 2023 @01:16PM (#34838248)
> using the ORM that everyone else is familiar with

A relational database isn't always fronted by the same tech. You have RESTful APIs, GraphQL APIs, Low Code/No Code Solutions (like Retool), reporting tools, ETL systems, etc.

You have company mergers where that one familiar ORM is now five to six unfamiliar ORMs.

Ensuring data integrity is key and easiest way to do that is to push constraints as close to the persistence layer as possible.

Re:Type Constraints in 65 lines of SQL by thor_molecules on Friday February 17, 2023 @12:59PM (#34838023)
So in this case, you've let a new dev commit to a code base without review.

Then, instead of informing them of best practices and giving them time to re-work the code, you'd just fire them?

Interesting strategy.

Re:Type Constraints in 65 lines of SQL by weird-eye-issue on Friday February 17, 2023 @10:08PM (#34843475)
Creating a pull request requires committing code too.
Re:Type Constraints in 65 lines of SQL by oliverrice on Friday February 17, 2023 @01:01PM (#34838064)
Agreed that there should be a data integrity or custom operation/filter/sorting requirement to motivate a user defined type
Re:Type Constraints in 65 lines of SQL by cgh on Friday February 17, 2023 @05:48PM (#34841690)
Easier, slower, more error-prone. Also, you should consider implementing code reviews rather than just firing people who commit code you don't like.
Re:Type Constraints in 65 lines of SQL by doctor_eval on Friday February 17, 2023 @06:06PM (#34841837)
I love working directly in plpgsql, it makes it possible to perform all the database logic in the database, effectively presenting a domain-specific API to the world. This api is then available to any SQL user. It’s like creating a built in CLI for your data.

Putting logic directly in the database is much faster at runtime, doesn’t require redundant entity definitions, is substantially more likely to detect schema problems, and is way easier to test.

And it removes the dependency on a specific language and toolchain. Your logic is accessible by everyone.

Unfortunately, the tooling around managing this stuff is not great. The language is a bit boring; idiomatic upper case is the first thing I get rid of.

But it’s the best tool for the job. Far from being “way easier” in application code, plpgsql reduces LOC, improves performance and removes dependencies.

If someone on my team ignored all of these benefits, well, I’d explain it to them and show them examples until they “got it”.

Way easier than firing them.

Re:Type Constraints in 65 lines of SQL by icedchai on Friday February 17, 2023 @07:37PM (#34842613)
Very few people know plpgsql. Like all stored proc languages (Oracle PL/SQL, etc.) it looks and feels like something out of the 80's. It's annoying to deploy, annoying to debug, annoying to test. Many developers today barely know SQL.
Re:Type Constraints in 65 lines of SQL by doctor_eval on Friday February 17, 2023 @07:53PM (#34842707)
It’s easy to make it feel more modern (select ffs from stop_using_all_caps). But it’s an ideal DSL for data manipulation.

It’s not particularly annoying to debug - there are logging, and even single step debugging tools available - and it’s so much easier to test than writing unit tests to do the same thing.

I agree it’s painful to deploy, but as I’ve discovered this is a tooling problem; it’s not at all intrinsic to the medium.

It’s certainly possible to create tooling to make it easier, and who knows? Maybe I’ll have something to show HN one day.

> Many developers today barely know SQL.

If true - a very big if - it’s an inditement of the industry and the incentives we use, not a reason to avoid something that makes code faster, safer and smaller.

Objections to using these languages are a lot more about willingness to learn and try new (old) things, than they are about the capabilities of the platform.

Re:Type Constraints in 65 lines of SQL by oliverrice on Friday February 17, 2023 @12:58PM (#34838014)
I think there are a few things:

1) Its only been a few years since the trend of testing on sqlite locally and running postgres in production went away in favor of postgres everywhere (probably thanks to docker). That prevented using any feature that wasn't equally supported by both.

2) There's definitely a knowledge gap, and not just among developers. The features are most useful for building rich applications, so even DBAs didn't have much incentive to use them prior to tools like supabase using the database as the data model source-of-truth.

3) Companies are increasingly deciding that data is their competitive advantage and interest in data integrity is growing. Database constraints are unparalleled at that because they can't be sidestepped

Re:Type Constraints in 65 lines of SQL by RedShift1 on Friday February 17, 2023 @01:29PM (#34838436)
I think most webdevs see databases as dumb datastores.
Re:Type Constraints in 65 lines of SQL by dventimi on Friday February 17, 2023 @01:44PM (#34838640)
I blame the Apple II computer.
Re:Type Constraints in 65 lines of SQL by heywhatupboys on Friday February 17, 2023 @01:46PM (#34838656)
no to forget: Turing's seminal paper
Re:Type Constraints in 65 lines of SQL by dventimi on Saturday February 18, 2023 @02:12AM (#34844736)
Perhaps you're joking, but I'm not. The personal computer revolution began in earnest with the Apple II in 1977, joined by the Commodore 64 in 1982. Gen-X boys shoved Gen-X girls aside, shoved the computers into their bedrooms, and 15 years later were at the right place and the right time to shove an Algol-based tech stack onto the center stage of the dot com boom. SQL was a necessary evil held at arms length behind ORMs as fear, uncertainty, and doubt rapidly took root. It's taken decades, but finally, mercifully, the Gen-X priests of procedural programming are losing their grip and we're just starting to emerge from the Dark Ages of all this superstitious nonsense about not putting business logic in the database. It's been a long strange trip, but it's never too late to study the classics. After all, they never really go out of style.
Re:Type Constraints in 65 lines of SQL by btown on Friday February 17, 2023 @03:15PM (#34839905)
IMO the lack of a Terraform-esque ecosystem of tools around declaratively managing database-objects-as-infrastructure, and understanding how a rollout of a change would be planned, has historically been a big issue. If I'm choosing where in the stack to declare some new business logic or type constraint, I can trivially check that into Git if it's running on an application server, and most frameworks' ORMs can handle column-level migrations. And if I make a mistake, I can push a change that declaratively says what my function signature or DDL should be; it's one of the first things taught to new developers. Terraform extended this mentality to infrastructure, and it was gamechanging.

But there are far fewer tutorials and best practices on how to, say, maintain a library of Postgres functions, types, and stored procedures that can be iterated on. I'd venture that most people have no idea how powerful their databases can be.

Re:Type Constraints in 65 lines of SQL by progmetaldev on Friday February 17, 2023 @06:48PM (#34842174)
I think most web developers just do not understand SQL well enough to take advantage of these types of features (and if they do, they still may be sticking closer to "standard" ANSI SQL, or may just not be up to date on newer features). Unfortunately, lots of web developers don't get past their ORM abstraction into what is occurring behind the scenes and how to make use of the full range of their database's features.
Re:Type Constraints in 65 lines of SQL by LunaSea on Saturday February 18, 2023 @04:58AM (#34845599)
The tooling ecosystem for databases is also terrible.

Bad support for testing, for version controlled schemas, language types, etc.

This is the reason for the spread of ORMs and similar abstractions to hide this lack of support.

Re:Type Constraints in 65 lines of SQL by Groxx on Friday February 17, 2023 @04:40PM (#34840943)
* * *
Re:Type Constraints in 65 lines of SQL by oliverrice on Friday February 17, 2023 @07:08PM (#34842372)
sure, if you switch the sql functions for plpgsql functions you can use procedural logic to on each segment and `raise exception` with a custom error message that will get returned to the client
Re:Type Constraints in 65 lines of SQL by mst on Friday February 17, 2023 @05:38PM (#34841576)
Why the {1,255} ? Pg's text type is not varchar, it can get a lot longer than that and still index just fine, and while I can see a restriction on the pre_release stuff -maybe- being useful, compilation/build metadata doesn't seem like something where an enforced maximum length that low is necessarily preferable. (I could see URLs, file paths, repo+path+sha etc. type stuff being useful in metadata and not something the code recording it can really control the length of - it may not do any harm in practice at least for a while, but I don't immediately see what -good- the restriction does, albeit I'm perfectly willing to be told "here's the good thing about it that you didn't think of" ;)
Re:Type Constraints in 65 lines of SQL by oliverrice on Friday February 17, 2023 @08:09PM (#34842790)
The 255 max length is something you could consider if untrusted people are able to insert data (through an API for example). A column in postgres can store up to 1 GB of data so its a possible DOS vector. Probably not an issue for most use-cases, just something that was considered in the application those code samples came from
Re:Type Constraints in 65 lines of SQL by mst on Sunday February 19, 2023 @08:47AM (#34857511)
I would still have gone with a higher length for the meta stuff, given the 2730 character limit on indexing I'd've thought 2048 would probably be fine and much less likely to result in a long path unexpectedly ventilating somebody's foot.

If space abuse is a vector under consideration, given there doesn't seem to be a restriction on the number of entries (if I somehow missed one my apologies) you could easily stuff it with a really big array of 255 character entries so I wonder if 2048 plus some sort of reasonably generous limit on the number of elements might not achieve the goal better and provide a better UX/DX as an added bonus.

(I'm currently working on a design in a somewhat related area so I've been doing a fair bit of thinking about this - I may of course still be completely wrong, but if so I can at least claim I put effort into ending up wrong ;)

Re:Type Constraints in 65 lines of SQL by maxloh on Saturday February 18, 2023 @05:30AM (#34845761)
I once heard that data validation should be implemented in back end server but not datsbase because the latter is more difficult to scare horizontally and usually the bottleneck of transaction performance.

Is that true?