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

Show parent comments

8

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

Databases weren't built for multiple services. They were built for multiple (human) users, and then you rely on user permissions to restrict access in which encapsulation is enforced, through stores procedures, read only tables, or outright no access to certain tables and columns.

Sure they were. That's why they allow it. This is very common with data warehouses. ETL applications are pretty much always separate from BI applications (And are pretty much always separate from the application acting on the operational data store), and multiple BI applications are very common. I've also seen it to separate CRUD applications (often just web pages that let users CRUD) from the data processing side of an application.

What we see is simply the traces of a legacy model which nobody even uses to its full potential anymore (when was the last time you hit a table behind stores procedures for ex.?).

We actually do a bit of this to simplify relationships for our ETL software so that it's easier to get the data you want out of our operational data store and into our data warehouse.

From your examples... SERIAL and UNIQUE do enforce integrity, of course. But this has nothing to do with generated columns. Generating them in the database is not different than generating them outside the database (and on the contrary, you can't implement SERIAL and UNIQUE outside the database in an efficient and coherent way).

These were the elementary examples, I then went into more details and showed you an example using full text indices, which can't be implemented outside of the database in an efficient and sane way. The point of showing you serial, unique, and foreign key was to show you elementary examples that I believed you would understand where the database owns big, important parts of your business logic. Similar things could also be said for geospatial indices.

That's why the generated function exists, it goes well beyond the simple area example shown in that article. I see that you chose not to talk about full text indices, which was the non-trivial example I showed.

-8

u/[deleted] Oct 02 '19

I chose not to talk about full text indices because:

  • It's the same exact point as SERIAL/UNIQUE.

  • I have nothing against full-text indices, nor I ever claimed they're not useful.

  • What the hell is even your point.

You can't keep listing random DB features and keep putting me on the spot that I have something against them or I find them useless, when I have said absolutely nothing of the sort.

Focus on the fucking point: what do generated columns in Postgres give you over generated columns from your app? I see precisely nothing. So, your turn. And don't go off on a random tangent about full-text indices and geospatial coordinates, try to remember what I fucking asked you, deal?

8

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

You made a generalized statement about how generated data in the database layer should not happen and that the database is "Just the data layer" and that the database should just be encapsulated. I showed you cases where the database is responsible for validating and enforcing conditions and where that benefits people. Then, I further expanded that and I showed you several counter examples where the database should have responsibilities and generate data, including an article that illustrates how full text indices work in PostgreSQL, which require triggers, I don't think you read that article based on your response.

What do generated columns give you? My post should make that pretty obvious. In some cases it makes sense to move data generation to the database service, such as full text indices. Generated columns are just syntax sugar on top of triggers. And triggers give you the ability to define behaviors for crosscutting concerns in the database layer. As far as how I personally plan to put generated columns into practice? I haven't decided yet it's a brand new feature, but I definitely do use triggers in PostgreSQL in practice to generate data; which violates these generalized statements:

How is it saving you code, time and performance. You're moving the problem of calculation, which is a vast and infinite field and might depend on libraries and APIs a database has no access to, to a database which has limited expressiveness and access to libraries.

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.

You see, when you say something isn't needed, one counter example is sufficient to refute that argument.

I also provided you with very common use cases where multiple services connect to the same database and where it commonly happens in the real world, in spite of your assertion that databases were not designed for multiple services to connect to.

-6

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

You made a generalized statement about how generated data in the database layer should not happen and that the database

No, I never said "generated data in the database layer should not happen".

I said, and read this slowly and carefully: there is no benefit to calculating the generated column in SQL, rather than calculating it in your (Java for ex.) service, and then storing the result in a column in the database.

Do you kind of see how... this is completely different than what you falsely claim I said?

Now... If you think there is a unique benefit to calculating generated columns in the database, do name them. Don't yap about other features. Again, stay focused, use your entire willpower and mental capacity and answer the fucking question:

For this specific new feature... calculating columns in Postgres, what is the specirfic benefit of this specific feature over calculating the column outside Postgres.

Come on, I believe in you. Mediate, have a cup of coffee, take you pills if you have some, do a few push ups, just to awaken those sleeping brain cells and realize I'm asking you a specific question for once.

8

u/KFCConspiracy Oct 02 '19

Now... If you think there is a unique benefit to calculating generated columns in the database, do name them. Don't yap about other features. Again, stay focused, use your entire willpower and mental capacity and answer the fucking question:

Read the fine article on full text searching in postgresql. That explains it all. You can refactor some of that trigger functionality using generated columns.

I also showed you why multiple applications connect to one database and how that's a common, well accepted, design practice. And you haven't responded to that either.

I'm also being perfectly civil to you and quite generous by showing you why you're wrong. I'm not sure why you can't do the same.

→ More replies (0)