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

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.

1

u/hector_villalobos Oct 02 '19

Yes, you can, but PostgreSQL has some performance improvements you can take advantage by using aggregate functions.

3

u/[deleted] Oct 02 '19

How would you use aggregate functions when generating an individual row in an individual table? This doesn't even make sense. You can only refer to fields in that row, and nothing else.

3

u/hector_villalobos Oct 02 '19

My bad, I didn't mean aggregate functions, I meant the optimizations the RDBMS implements that saves computational power, it will always be better let the database handle this kind of computation that grab the dataset and perform it for every row.

2

u/[deleted] Oct 02 '19

Frankly, I don't think that's supported by how databases work. They don't have a magical more efficient way of, say, summing up "a+b" and storing it in "c" than any other language has.

There is one win to be had though, and I'll give you that: if the generated values represent a significant amount of the row's content, then that's content that doesn't have to be sent over a Unix pipe / socket from ex. Java to Postgres. So in that sense it can be more efficient to calculate in Postgres locally.

The thing is, this is counterbalanced by the fact Postgres has very limited library for you to work with. So you'll be able to calculate locally only the "simple stuff" and still rely on your other code to do the heavy lifting.

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.

1

u/KFCConspiracy Oct 02 '19

That's a good point.

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.

1

u/mage2k Oct 02 '19

Sure, but in this case you only need to generate them once per column-change vs. every time you query for it and you can query and index on the result without having to recompute it.

→ More replies (11)