SQL, Postgres OIDs, What are they and why are they useful

databaseperformancepostgresqlsql

I am looking at some PostgreSQL table creation and I stumbled upon this:

CREATE TABLE (
...
) WITH ( OIDS = FALSE );

I read the documentation provided by postgres and I know the concept of object identifier from OOP but still I do not grasp,

  • why such identifier would be useful in a database?
  • to make queries shorter?
  • when should it be used?

Best Answer

OIDs basically give you a built-in id for every row, contained in a system column (as opposed to a user-space column). That's handy for tables where you don't have a primary key, have duplicate rows, etc. For example, if you have a table with two identical rows, and you want to delete the oldest of the two, you could do that using the oid column.

OIDs are implemented using 4-byte unsigned integers. They are not unique–OID counter will wrap around at 2³²-1. OID are also used to identify data types (see /usr/include/postgresql/server/catalog/pg_type_d.h).

In my experience, the feature is generally unused in most postgres-backed applications (probably in part because they're non-standard), and their use is essentially deprecated:

In PostgreSQL 8.1 default_with_oids is off by default; in prior versions of PostgreSQL, it was on by default.

The use of OIDs in user tables is considered deprecated, so most installations should leave this variable disabled. Applications that require OIDs for a particular table should specify WITH OIDS when creating the table. This variable can be enabled for compatibility with old applications that do not follow this behavior.