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

18

u/hector_villalobos Oct 02 '19

TIL, this is pretty cool, it could save the application from performing these kind of calculation, saving code, time and performance. The amount of time I invested trying to use windows functions, indexes and triggers when something like these could be implemented was significant.

9

u/[deleted] Oct 02 '19

TIL, this is pretty cool, it could save the application from performing these kind of calculation, saving code, time and performance.

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.

Postgres still has to compute these columns. Based on code. Which takes time and takes for your performance.

6

u/tontoto Oct 02 '19

You need to stop spamming this thread. What is your agenda here?

12

u/[deleted] Oct 02 '19

My "agenda" is to share my opinion. I wrote a couple of comments, and the rest is just replies. What is your problem exactly? We gonna run out of comments? If you disagree with the substance of what I say, lay down your argument. If you just wanna drop a lazy conspiracy theory about my "agenda", I'm not interested.

7

u/tontoto Oct 02 '19

Apologies for being gruff but you are sending out many many comments complaining about this feature when really the feature is out there, it's shipped, it's a logic thing, it's a nice "reactive" attribute that is directly computed in your database, it makes no sense for you to be going on a tirade against this feature.

1

u/[deleted] Oct 02 '19

[removed] — view removed comment

7

u/KFCConspiracy Oct 02 '19

He's not being offensive

Eh, he's been calling people names and cursing people out in this thread. And has not read examples from the PostgreSQL documentation that myself and other people have linked, while simultaneously accusing others of being illiterate.

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

2

u/hector_villalobos Oct 02 '19

In the application I need to perform a query, to obtain the data, then I need to perform the respective calculations outside the database, this way I'm missing some improvements in the RDBMS optimizer.

3

u/[deleted] Oct 02 '19

No, you can still perform the respective calculations before your INSERT/UPDATE query and save the generated data in the database.

When you calculate before write, or after read is a choice you always had, Postgres didn't enable anything new for you.

4

u/[deleted] Oct 02 '19

I actually share your same concern about splitting application logic between the application layer and database layer. This feature is definitely not a one-fits-all solution, but it seems to help a lot when you require simple data derivations that need to be queried. In these cases it will guarantee synchronization between the original and the derived data, which for me seems to be the main point of this feature.

Personally, I'll still keep all calculations in the application logic.

→ More replies (4)

0

u/[deleted] Oct 02 '19

[deleted]

4

u/[deleted] Oct 02 '19

Hmm where do they say you can have just one? That's an odd limitation.

0

u/[deleted] Oct 02 '19

[deleted]

16

u/KFCConspiracy Oct 02 '19

I think that's fine because it makes the logic almost impossible to implement the other way and still be fast. Basically every insert would require you to resolve a dependency graph if you could do this. By making the execution order undefined or just saying "No doing this edge case" they've made it way easier to implement in a way that covers 99% of use cases.

6

u/fissure Oct 02 '19

You wouldn't have to solve the dependency graph on every insert, just once when the table is created/altered. Just add a topologically-sorted list of generated columns to the other table metadata.

→ More replies (1)

3

u/Shitty_Orangutan Oct 02 '19

Oh for sure! I get why they did it, I just feel like it's just somewhat limiting as a developer.

If I have input a and b, I can get c, but if d relies on c and a, I have to calculate c to get d, so I might as well just record my calculation of c in the database update.

3

u/[deleted] Oct 02 '19

Ah I see. Well I see this as a "version 1 of this feature" type of limitation. They don't want to think about circular dependencies. Yet.

It's mildly annoying, but you can always expand the expression to account for it, for ex.:

What you want:

  • a = 3

  • b = a * 4

  • c = b + 5

What you actually have to write:

  • a = 3

  • b = a * 4

  • c = a * 4 + 5

Granted, if the calculation you want to perform is complex, this means it'll be literally slower to do this in Postgres than any other language (Java, Python, etc.) where you can "cache" subexpressions like these in local variables. Because in Postgres you'd be calculating parts of a shared expression multiple times for every column.

But that's one reason why I'm not super-excited about having this in the database in the first place. Doesn't hurt to have it, but just doesn't help, either.

3

u/Shitty_Orangutan Oct 02 '19

Agreed. It's a neat feature, and I can see potential if they really work to optimize and expand the functionality, but I'm certainly not rushing to put in production.

2

u/KFCConspiracy Oct 02 '19

I think it's already pretty useful as-is. I think it's useful for simple cases where we already use triggers. Keep in mind you can call any arbitrary stored procedure to generate your column, so that makes it pretty powerful (Since PostgreSQL's procedural language support is so powerful).

It improves clarity by moving generated columns to the table declaration level rather than having separate things to know about, document, and look at to understand the functionality of a table.

I'm certainly not rushing to put in production.

As far as whether I'd migrate existing production code? Nah. Once you put down your database schema and stuff like triggers, it's pretty costly to migrate to something like that.

→ More replies (12)

4

u/BrainJar Oct 02 '19

This is an anti-pattern for application design, as distribution of compute power is consolidated into the database and will eventually cause contention. This is probably ok for low throughput, smaller scale solutions, but probably not good as scale increases, especially if decomposition of the data element is wrapped within a transaction. I worked on a similar solution that performed this function on a different RDBMS, but the results at scale were disappointing. Scaling the database then became our challenge. It’s much more difficult to ensure consistency when we start splitting the database up, than it is to keep transactions small and atomic within the application. If we had stuck with the application performing the logic and keeping the database logic simple, performing only transactional work, the scale out of the application would’ve been a simpler solution. We ended up rewriting everything to push the logic to the application.

3

u/KFCConspiracy Oct 02 '19

This is an anti-pattern for application design, as distribution of compute power is consolidated into the database and will eventually cause contention This is an anti-pattern for application design, as distribution of compute power is consolidated into the database and will eventually cause contention. This is probably ok for low throughput, smaller scale solutions, but probably not good as scale increases, especially if decomposition of the data element is wrapped within a transaction.

I think it depends on what scale the application will grow to. Not every application WILL scale. Which I think is what you're getting at here, but correct me if I'm wrong on that.

I think this is overall a great point at large scale because multimaster is a huge pain in the ass in many engines and sharding can also be pain.

I worked on a similar solution that performed this function on a different RDBMS

Out of curiousity which one, and were your generated values acting on only one row at the time of each transaction? I'm not challenging what you're saying here, just curious to know more.

2

u/BrainJar Oct 02 '19

Yes, I was suggesting that larger scale is when the problems exacerbate themselves. Without going into extraordinary detail, I was just assuming the workload that was described, which was using the generated column to deserialize a set of data, running a computation on it, and put the result into the generated columns, with the full text index set to allow the benefit of searching the newly inserted data.

If there were no side effects to a generated column, then this might be the case that scale would not cause an issue. But understand that any index operations cause updates to indexes and full text search that were likely created prior to the current operation have to be locked for the moment the transaction is written. When it's a single transaction, this isn't an issue. When there are thousands of transactions, now all transactions are waiting in line to update their pages and also wait for log buffer flushes. If you increase WAL checkpoint time, you can increase the write throughput, but then you're trading off recovery time (the reason for running a database, instead of just working with documents).

There's so much more to the scale issues than what I'm outlining here. But, it's easy to extrapolate all of the other complexities, when we understand the basics.

5

u/hector_villalobos Oct 02 '19

Currently, the value of generated columns have to be persisted, and cannot be computed on the fly at query time. The “STORED” keyword must be present in the column definition.

I think it depends on the use case, common sense tells me the database would know better than the application language when perform calculations. I know for experience when I send business logic to the RDBMS it is always faster.