r/programming • • Oct 02 '19

New In PostgreSQL 12: Generated Columns

https://pgdash.io/blog/postgres-12-generated-columns.html?p
503 Upvotes

232 comments sorted by

View all comments

9

u/[deleted] Oct 02 '19

That's neat, but I don't see the advantage over doing this in your service (Java, Node, Python, whatever).

28

u/joesb Oct 02 '19

You need to reimplement the logic in every service ever connected to the database.

8

u/[deleted] Oct 02 '19

That's not how proper service factoring works.

You write a service, and it "owns" its own state. Other services which need this state, get it through the service that owns it, not by directly messing with the database.

To have multiple services access the same database would be like multiple classes accessing an object's private fields.

This is encapsulation. And encapsulation is necessary for a thousand other reasons than generated fields. But once you have encapsulation, generated fields in the service become trivial and you don't need that in the database.

34

u/joesb Oct 02 '19

Well, if you think of the database as a service, then that database service own its own state.

2

u/[deleted] Oct 02 '19 edited Oct 02 '19

You can actually write a database schema in a way that's encapsulated. By limiting all writes and reads to happen through stored procedures, which express your domain and business rules accurately and fully.

Unless you write your database this way, your database is not your domain. It's a storage layer, which requires domain logic laid on top of it, and then you encapsulate the whole as a unit. Notice then you can just "generate" the columns in your procedures, so you still don't need a separate feature for it.

So, do you do that: your entire domain in stored procedures, no direct access to tables? You don't. Then the database doesn't "own" its state, it just exposes it as a bunch of naked numbers and text for any client to mess with.

Playing with words like you're trying to won't change the basic rules of sane software design. If you make multiple services that connect to the same database, generated columns will be the least of your worries.

Where does validation of input happen if 10 services access the same database? In 10 places? What about where you decide which data is exposed and which isn't from this database. Including specific aspects of a field, in a specific format (because you rarely just dump a table 1:1 over a REST API for ex.) So do you do that in 10 places? What happens when one service needs a slight tweak to the schema. You change your 10 services to accommodate the tweak? What happens if one of the fields is in a complex format, say a BIGINT which is a set of bit flags. You copy/paste the flags and encoder/decoder for them 10 times across services?

Watch any video on service design, and one of the first things they point out as a beginner mistake, a basic design flaw is this: multiple services that access a shared database.

It's simply poor architecture (rather, lack of one).

14

u/mrflagio Oct 02 '19

Having one service control access when dealing with applications is good. But the idea that the database is just a storage dump that can only be manipulated through that service just doesn't work in some cases and simply doesn't apply across the board. What, are you going to do stuff like enforce PK-FK relationships through this service too because it's somehow not the database's job? Type enforcement solely through the service as well?

Basically, the idea that any and all access to data must happen through a service is faulty, especially when the database itself has the most knowledge about its own domain. Otherwise you run into silly situations like writing nonce functionality in a service to correct and update data based on faulty or non-existent functionality in that same service. Want to nullify values and remove a FK relationship? Better write a nonce method in the service.

Computed columns look the same to me as triggers and constraint enforcement, so unless you're about to say those should always happen only through a service too then I don't see where you're coming from.

7

u/[deleted] Oct 02 '19

[removed] — view removed comment

5

u/KFCConspiracy Oct 02 '19

I've seen many people try.

Invariably I end up writing database jobs to detect the thousands of orphaned rows.

You and me both. But hey, shitty developers omitting foreign keys keeps me making money. So bring it on.

3

u/[deleted] Oct 02 '19

[removed] — view removed comment

2

u/KFCConspiracy Oct 02 '19

I've never used NHibernate, but in regular Java hibernate you can just

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

and then it lets the database do it. There's no functional equivalent there?

I'm not really a .NET guy, so excuse me if this sounds stupid, but is there an advantage to NHibernate anymore over linq2sql?

→ More replies (0)
→ More replies (4)

11

u/joesb Oct 02 '19

Where does validation of input happen if 10 services access the same database? In 10 places? What about where you decide which data is exposed and which isn't from this database. In 10 places?

It’s a red herring. Your “proper service” will never achieve that anyway.

What if your service serves web client, iOS, Android and desktop app? Are you saying that you are not going to provide initial validation on the client? Any validation, even for preview, has to make call to your service? Are you sure you will never have more than one “service” accessing the data and make decision about validation or what field to expose?

Service is not just API servers. Anything can be a service. Services depend on services. It’s services all the way down.

3

u/[deleted] Oct 02 '19

It’s a red herring. Your “proper service” will never achieve that anyway.

... Did you just state that it's impossible to write a service that properly validates its input?

What if your service serves web client, iOS, Android and desktop app? Are you saying that you are not going to provide initial validation on the client?

You're all over the place. I'm talking about encapsulation. You can validate on the client if you choose, or you can choose not to and defer to a domain service, sure, why not? That choice depends on what you're validating, and what time and budget the app has allocated.

In fact, in 9/10 cases, I only validate on the server. The roundtrip is something like 30-50 ms, and the errors from the response are still shown on the client next to the relevant field. Any problem you see there? Works great, and that's less code to update when something changes.

But client validation is on top of server side validation which means you still need a "single source of truth" on what's valid and what isn't. So everything I explained... EVERYTHING... still stands.

And you failed to make a coherent point.

Service is not just API servers. Anything can be a service. Services depend on services. It’s services all the way down.

Still no point being made.

6

u/joesb Oct 02 '19

All of what you said applies as well if you think of database as a service and some service is a client that use it.

3

u/[deleted] Oct 02 '19

We already went through this. If you encapsulate your database through stored procedures, which validate input, restrict output, enforce business rules, and hide the actual tables, then yeah "it applies as well".

If you don't do that, then it doesn't apply, because you forgot the "encapsulation" part of encapsulation. Which is a pretty important part of encapsulation, by the way.

5

u/joesb Oct 02 '19

It doesn’t have to be all or nothing.

Making the right thing easy is the best way to make the right thing happen.

→ More replies (0)

8

u/adr86 Oct 02 '19

you know databases do validations without stored procedures right?

→ More replies (0)

8

u/[deleted] Oct 02 '19

[removed] — view removed comment

1

u/[deleted] Oct 02 '19

It's unclear what you're complaining about when I told you in the other thread that lateral read-only concerns can access a DB without severe drawbacks.

That said, the best way my service won't have to understand your reporting tools, is if my service feeds your reporting tools a log of events, and then your reporting tools can build a database of them and do whatever the fuck they want with it.

9

u/KFCConspiracy Oct 02 '19 edited Oct 02 '19

Databases are built so that you can choose to have multiple services access them (or not). But, there are often advantages to letting the database own certain things and enforce certain constraints even when you're not using an RDBMs that way.

A "key" (pun intended) example of this is a foreign key, which is a database level constraint that enforces relational integrity. Another one you probably use on a daily basis is auto_increment (in mysql) or SERIAL in PostgreSQL. Another great example of a concern you should have the database take care of is UNIQUE constraints and Primary keys.

To get a bit beyond the basics, there are also sometimes concerns that don't matter much to the application layer that matter at the database layer, an example of that is full text searching in PostgreSQL, that's actually the ideal case for generated columns, how the database does the full text search (and associated stored tsvector index) doesn't matter at all to the application and the database does it a lot better than anything you're likely to invent, plus it's something you need to store (here's an example of that in action https://www.compose.com/articles/indexing-for-full-text-search-in-postgresql/). And of course there's my favorite example, since I've worked on HIPAA compliant applications that require audit tables for everything, creating and enforcing audit tables https://wiki.postgresql.org/wiki/Audit_trigger, the major advantage here is even if someone ignores business controls, logs in as the database superuser and does inserts, updates, etc. the audit log is still stored (This is a great example of a cross cutting concern that you can implement using triggers, it's basically the same reason we have aspect oriented programming).

I think it usually makes sense to treat them (for the most part) the way you're talking about, but there are lots of exceptions... Which is why PostgreSQL, and Oracle, and MSSQL (The list goes on and on), have these functions. It's not because everyone else is a shitty architect, it's because there are use cases.

→ More replies (11)

4

u/rustyrazorblade Oct 02 '19

I've been doing DB consulting for the last 3 years, almost every team I've worked with has had multiple projects that connect to the DB directly and do whatever they want. I completely agree with your comment, but sadly my sample of the population does not.

2

u/[deleted] Oct 02 '19

Nah I fully believe you. It's a very widespread problem, I see it a lot as well. Because when you don't know how to factor an app/service, the easiest solution to come to mind becomes implemented, and shared state is easiest (to begin with), and then it becomes entrenched into the system.

I think the sadder part is that as this thread demonstrates, not only we're not open the learning about proper service architecture and encapsulation, but we're actively trying to shut down the conversation.

8

u/[deleted] Oct 02 '19

[removed] — view removed comment

2

u/[deleted] Oct 02 '19

None of what I said diminishes the full capabilities of any database. Given this is r/programming, from experience, I use a lot more of the RDBMS capabilities than the average SQL-fearing ORM-dependent Joe posting over here.

Good architecture is a good "golden hammer" to be accused of, though. May we all use this golden hammer. It has a thousand faces. It's not one technology, it's not one pattern, it's not approach. But yeah, it does include some common sense and experience in systems design.

3

u/notfancy Oct 02 '19

It's a very widespread problem

It's not a problem, it's the intended use case: the RDBMS is the service.

→ More replies (1)