top | item 45214209

My Favorite PostgreSQL 18 Feature: Virtual Generated Columns

54 points| fforflo | 5 months ago |tselai.com

9 comments

order

garyclarke27|5 months ago

Stored generated columns would be much more useful if they could reference other rows and tables, they are limited to only the current row. Automatic incremental view maintenance would also solve this problem, but is unfortunately still a glaring omission in Postgres capabilities.

saltcured|5 months ago

Aside from the SQL syntax, what is the difference between this and the longstanding virtual column feature, where you can define a function that takes the table's record type as input and return a scalar? PostgreSQL already let you use table.funcname as a virtual column, as I recall.

mdaniel|5 months ago

It's possible the following argument is what you meant by "from the SQL syntax," and if so apologies for the noise

At least one demonstrable difference I can see between your recollection and this feature is that the column name being part of the DDL pushes the logic down into the DDL, versus every client having to carry that magic expression around with them. I think it's likely the age-old application-logic-client-side versus stored-proc camps

  db=# select id, cast(length(name) AS text)||'0' as silly from my_table
as compared to <<select id, silly from my_table>> for the consumer

sharlos201068|5 months ago

That's still not a column and isn't included with things like select *

icedchai|5 months ago

In the past, I've used views to add "virtual columns" where I didn't want to hard code them into the application.

sgarland|5 months ago

Welp, that’s it. That was the last thing MySQL had that Postgres didn’t (modulo clustering index, which OrioleDB does, soooo…).

Can’t wait!

_el1s7|5 months ago

I don't really find these kind of features necessary.

You can, and probably should, do these things in your application code. I mean, it's not really that hard to make a string lowercase, you don't need to define a "virtual colum" for that...

combatentropy|5 months ago

You're right, it's not that hard. But why would you rather do it there?