Does PostgreSQL support computed / calculated columns, like MS SQL Server? I can't find anything in the docs, but as this feature is included in many other DBMSs I thought I might be missing something.
相关问题
- Django distinct is not working
- PostgreSQL: left outer join syntax
- Connecting Python to a Heroku PostgreSQL DB?
- PostgreSQL - Deleting data that are older than an
- Does PLV8 support making http calls to other serve
相关文章
- postgresql 关于使用between and 中是字符串的问题
- postgresql 月份差计算问题
- Using boolean expression in order by clause
- Table valued Parameter Equivalent in Postgresql
- in redshift postgresql can I skip columns with the
- Oracle equivalent of PostgreSQL INSERT…RETURNING *
- PostgreSQL field data type for IPv4 addresses
- Using prepared statement in stored function
Well, not sure if this is what You mean but Posgres normally support "dummy" ETL syntax. I created one empty column in table and then needed to fill it by calculated records depending on values in row.
YES you can!! The solution should be easy, safe, and performant...
I'm new to postgresql, but it seems you can create computed columns by using an expression index, paired with a view (the view is optional, but makes makes life a bit easier).
Suppose my computation is
md5(some_string_field)
, then I create the index as:Now, any queries that act on
MD5(some_string_field)
will use the index rather than computing it from scratch. For example:You can check this with explain.
However at this point you are relying on users of the table knowing exactly how to construct the column. To make life easier, you can create a
VIEW
onto an augmented version of the original table, adding in the computed value as a new column:Now any queries using
some_table_augmented
will be able to usesome_string_field_md5
without worrying about how it works..they just get good performance. The view doesn't copy any data from the original table, so it is good memory-wise as well as performance-wise. Note however that you can't update/insert into a view, only into the source table, but if you really want, I believe you can redirect inserts and updates to the source table using rules (I could be wrong on that last point as I've never tried it myself).Edit: it seems if the query involves competing indices, the planner engine may sometimes not use the expression-index at all. The choice seems to be data dependant.
A lightweight solution with Check constraint:
I have a code that works and use the term calculated, I'm not on postgresSQL pure tho we run on PADB
here is how it's used
One way to do this is with a trigger!
The trigger is fired before the row is updated or inserted. It changes the field that we want to compute of
NEW
record and then it returns that record.Up to Postgres 11 generated columns are not supported - as defined in the SQL standard and implemented by some RDBMS including DB2, MySQL and Oracle. Nor the similar "computed columns" of SQL Server.
The feature is in development for Postgres 12, principle author Peter Eisentraut.
For now, you can emulate with a function using attribute notation (
tbl.col
) that looks and works much like a virtual generated column. That's a bit of a syntax oddity which exists in Postgres for historic reasons and happens to fit the case. This related answer has code examples:The expression (looking like a column) is not included in a
SELECT * FROM tbl
, though. You always have to list it explicitly.Can also be supported with a matching expression index - provided the function is
IMMUTABLE
. Like:Alternatives
Alternatively, you can implement similar functionality with a
VIEW
, optionally coupled with expression indexes. ThenSELECT *
can include the generated column."Persisted" computed columns can be implemented with triggers in a functionally identical way.
Materialized views are a closely related concept, implemented since Postgres 9.3.
In earlier versions one can manage MVs manually.