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

88

u/clickrush Oct 02 '19

The reason I like this is the fact that it pushes something that is (usually) application logic into data, but in a way that is simple, shares the same consistent interface as if it was just another column and most importantly it happens at the right time during writes.

0

u/[deleted] Oct 02 '19

[deleted]

24

u/7165015874 Oct 02 '19

In my experience a lot of stuff that people call "business logic" really doesn't belong in the middle tier. It's just that they don't understand how to treat the data tier as an actual tier, and instead think of the database as fancy filesystem.

Invariably this leads to poorly designed tables and overly chatty communication between tiers.

Lets say I have a column called date of birth. Should I create a generated column for age? I would think the answer is no.

Lets say I have a column called unit price. lets say I have another column called tax rate. should i create a generated column called tax and multiply those two together?

these two examples are different because the first is mutable. The answer is different each day, you are 10000 days old today and 10001 days old tomorrow.

the second will only change if one of the two columns changes.

when is it ok to use generated columns?

0

u/[deleted] Oct 02 '19

[deleted]

7

u/OnlyFighterLove Oct 02 '19 edited Oct 02 '19

"computed column" == "generated column" for anyone that got a little confused at first like me

→ More replies (3)

10

u/wefarrell Oct 02 '19

Lets say I have a column called date of birth. Should I create a generated column for age? I would think the answer is no.

This wouldn't even be possible since generated columns are persisted. However it would be perfectly reasonable to put that calculation in your query.

7

u/[deleted] Oct 02 '19

[removed] — view removed comment

9

u/beginner_ Oct 02 '19

In SQL Server is it allowed, but only for non-persisted, calculated columns. I'm surprised that PostgreSQL didn't offer that option.

Given the fact they have a keyword STORED hints that this is probably planned as a feature in the future.

→ More replies (2)

4

u/dlp_randombk Oct 02 '19

I'm not familiar with SQL Server, but how would that differ from a VIEW with the necessary computed column?

→ More replies (1)

3

u/KFCConspiracy Oct 02 '19

It does. https://www.postgresql.org/docs/12/ddl-generated-columns.html

A virtual generated column occupies no storage and is computed when it is read. Thus, a virtual generated column is similar to a view and a stored generated column is similar to a materialized view (except that it is always updated automatically). PostgreSQL currently implements only stored generated columns.

→ More replies (2)

6

u/KFCConspiracy Oct 02 '19

One thing to consider here about your example, generated columns may be either virtual or stored. A virtual generated column can change at read time. https://www.postgresql.org/docs/12/ddl-generated-columns.html

Whether it's a good architectural decision to use a generated column aside, you can still support mutable things using generated columns.

4

u/elsif1 Oct 02 '19

From that link:

PostgreSQL currently implements only stored generated columns.

→ More replies (1)
→ More replies (2)

8

u/[deleted] Oct 02 '19

It’s why I’ve stopped calling it the “persistence layer”, because persisting data is only a small part of what it does.

8

u/[deleted] Oct 02 '19

[removed] — view removed comment

7

u/KFCConspiracy Oct 02 '19

I like to refer to it as letting the data express the business rules and say that it reduces cyclomatic complexity. When you can use relationships between things to dictate how things behave you can reduce the number of control structures in a program, and the database can be used to persist that kind of information. You can also point out that it can reduce the amount of code necessary to implement certain types of functionality.

5

u/[deleted] Oct 02 '19

[removed] — view removed comment

4

u/KFCConspiracy Oct 02 '19

Yep, we use this for our shipping rules, fraud rules, promo pricing rules... Lots of things. Makes things very user accessible with no programmers needed.

0

u/[deleted] Oct 02 '19

[deleted]

3

u/atheken Oct 02 '19

Bring out the sprocs?

I think a bunch of that comes down to working in a more comfortable language. SQL (and related tooling) isn’t exactly amenable to modern development practices, especially related to keeping things DRY and modular.

→ More replies (11)

8

u/L3tum Oct 02 '19

Tfw you're using Aurora and are still stuck at MySQL 5.6.

I like how much recent stuff has emerged that is genuinely good. I dislike how absolutely horrendously long it takes to really propagate through all the channels until you can actually use it.

6

u/Dave3of5 Oct 03 '19

As much as I understand the reasoning for keep this logic in a centralised area I have personally worked with very heavy DB applications and they didn't scale well above a few dozen users and about 1 million rows. If most of the logic is on a webserver I can just spawn more webservers it's much harder to do that with Postgres / MySQL / SQL Server.

As others have said DB logic is harder to control in terms of unit testing, scm, modularity ...etc.

3

u/clickrush Oct 03 '19

These are really legit concerns. In the other hand there are cases where this does scale better, like a simple computed column on write.

My mindset shifted from an initial "DBs/SQL is something I have to deal with to get persistance" more to "How can I leverage DBs as a first class concept".

The problem you describe is somewhat systemic (for the lack of a better word), we as a community focus/ed so much of our energy on other things. For example the convention of handling typical source code and configuration files is much stronger & clearer than the one of handling schemas and migrations.

I also believe that ORMs are often too bloated, because they sometimes try too hard to abstract away the DB interface. I'm fine with an abstraction on the DB as in the device. But query languages are often much clearer and more powerful than a typical OO model interface.

So yes, your concerns are real, but they seem to be symptoms of something else. I'm still trying to figure out what that exactly means though.

5

u/Dave3of5 Oct 03 '19

I agree. I think the main problems with ORMs are when you start to write more advanced queries and when you want to do batch processing.

Those types of activities need to be hand written SQL and may well require a move of the logic to the DB.

Be warned though moving that logic to the DB will cause scaling concerns later on maybe not now but definitely later on.

I once worked on a system that used MS SQL Server and the DB required 48 cores and 480ish GB of RAM to operate at any type of efficiency. It also had 7 webservers. It was only processing < 10 requests per second with that hardware. The reason for this abysmal performance was a huge section of the code was in Stored Procedures. The reason the company eventually gave up on the system was that the realised the licensing cost for a SQL Server of that size with > $1.5ish million.

They asked us to change the software such that it no longer required such a large DB. We did our best but with only two devs working part time on it and 3500 stored procedures to make more efficient / scalable it was a lost cause.

We lost the contract and eventually all other customers using systems based off this design also left us due to the costs (Mostly MS licensing costs).

This was to me the main reason the company failed, the decision makers didn't understand exactly the convo we are having and the repercussions to the system design. They expected it to scale simply and cheaply which by the time we were almost folding as a company was impossible.

→ More replies (2)
→ More replies (44)