Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is there difference between Identity vs Generated in Postgres / pgAdmin4?

Given this UI in pgAdmin4, what's the difference between these two options:

  • Identity
  • Generated

enter image description here

I'm from a SQL Server background, so I'm used to setting the IDENTITY of an INT, for those MSSql tables to auto-generate that column value.

How is this different/similar in Postgres?

What is interesting is when I check out the GENERATED option, I get this menu, now?

enter image description here

While the IDENTITY option looks like I need to specify everything. So is the difference that IDENTITY is very hard-coded specific, while the GENERATED is 'let me auto set everything up, except for some expression thingy?'

NOTE: Using the latest Docker image of Postgres, so let's assume v10+ (with regards to serial now being off the table in this discussion).

Similar questions:

  • How do I specify that a column should be auto-incremented in pgAdmin?
like image 866
Pure.Krome Avatar asked Jun 27 '26 02:06

Pure.Krome


1 Answers

Identity works the same way as in mssql or SQL in general, PostgreSQL 10+ used generated as identity more as a compliant on SQL standard, compared to the older serial. I think This answer explained it well here.
For GENERATED, It's a column that will always be created as a computed value from other columns.
Let's say monthly_salary GENERATED ALWAYS AS (salary/12) STORED will use value from salary column, calculate, and stored as monthly_salary. see The documentation here.

For syntax purposes, both are similar. But there is a little difference.\

# This one is IDENTITY
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] 

# This one is generated from other column
GENERATED ALWAYS AS ( generation_expr ) STORED 
like image 192
Ponsakorn30214 Avatar answered Jun 30 '26 06:06

Ponsakorn30214



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!