PostgreSQL Generated Column (Computed Column) is an operation on two or more columns and printing them into a different column.
Computed Column is available with postgresql12.
STORED parameter: It is used to read the value written in the column when it is wanted to be written and read while inserting the table.
VIRTUAL parameter: It is not written while inserting into the table, and when it is wanted to be read, it is calculated by using the view logic and the result is returned.
Let’s reinforce it with an example.
First, let’s create our 3-column table. The first two columns are the column we will use to enter our data, and the last column is the column where the sum of the two columns will be printed.
CREATE TABLE total (
, number2 int
, totalvalue bigint GENERATED ALWAYS AS(number1 + number2) STORED
We created the table. Let’s insert our data as follows.
insert into total values (1,2);
insert into total values (4,5);
We have inserted the data. Let’s see the result by selecting from the table.
As seen above, when we enter the values in the number1 and number2 column, the total value of the number1 and number2 column is printed in the totalvalue column.