Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
closed by

I tried creating some PostgreSQL table but I got stuck on this:

CREATE TABLE (

...

) WITH ( OIDS = FALSE );

I went through the documentation provided by postgres and I know the concept of object identifier from OOP but I could not get it properly. Why are these kinds of identifiers useful in a database?

closed

4 Answers

0 votes
by (11.4k points)
 
Best answer
In PostgreSQL, the option "WITH (OIDS = FALSE)" specifies whether the table should include an object identifier column (OID column) or not. An OID column is a unique identifier assigned to each row in a table automatically by PostgreSQL.

OIDs can be useful in certain scenarios, such as:

Referencing rows: The OID column can serve as a unique identifier for referencing rows within the same table or across different tables. For example, if you have a relationship between two tables, you can use the OID column as a foreign key.

System administration: The OID column can be helpful for system administrators in managing and identifying specific rows. It can be used for administrative tasks like tracking changes, auditing, or managing access permissions.

However, it's important to note that the use of OIDs has been deprecated in PostgreSQL since version 8.1, and the option "WITH (OIDS = FALSE)" is now the default behavior. OIDs can consume additional storage space and have certain performance implications, so it's generally recommended to avoid using them unless there is a specific requirement for their usage.

If you're creating a new table in PostgreSQL and don't need OIDs, you can simply omit the "WITH (OIDS = FALSE)" option, as it is not necessary. The basic syntax for creating a table would be:
 

CREATE TABLE table_name (

  column1 datatype1,

  column2 datatype2,

  ...

);
0 votes
by (11.7k points)

Whenever you want to have a built-in id for every row, it is provided by OID which is contained in the system column, It is very helpful for the tables we do not have a primary key etc. For instance, if we have a table with two identical rows and want to delete the oldest of the two, we can do that by using an oid column. 

We can implement OIDs using 4-byte unsigned integers. They are not unique–the 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).

If I tell you my experiences, the feature is mostly unused in most postgres-backed applications. In PostgreSQL 8.1 default_with_oids is off by default; in prior versions of PostgreSQL, it was on by default.

OIDs used in user tables are considered deprecated, so maximum installations should leave this variable disabled. Applications that require OIDs for a particular table must specify WITH OIDS when creating the table. We can enable these variables for compatibility with old applications that have a different approach.

If you want to get more insights into SQL, checkout this SQL Course from Intellipaat.

0 votes
by (13k points)

In PostgreSQL, the concept of Object Identifier (OID) refers to a unique identifier assigned to each row in a table. By default, PostgreSQL used to assign OIDs to tables, but it has been deprecated and is no longer enabled by default in recent versions.

OIDs can be useful in certain scenarios. Here are a few reasons why they were used in the past:

1. Data referencing: OIDs can serve as a reference to a specific row within a table. Other tables or columns can store the OID values to establish relationships or point to a specific row in a different table.

2. Internal system tables: PostgreSQL uses OIDs internally to manage system tables. These tables store metadata and information about the database system itself, and the OIDs help to uniquely identify and manage these system tables.

3. Large Object (LOB) support: PostgreSQL has a feature called Large Objects, which allows storing binary data (such as images, audio, or documents) in the database. OIDs were used to manage and reference these large objects.

However, due to various reasons including performance concerns and potential security issues, the use of OIDs in user-defined tables has been deprecated. The "WITH (OIDS = FALSE)" clause in the table creation statement you mentioned is used to explicitly disable the use of OIDs for the specific table.

In modern PostgreSQL databases, it is recommended to use primary keys, foreign keys, and other relational mechanisms to establish relationships between tables and reference specific rows, rather than relying on OIDs.

0 votes
by (7.8k points)

In PostgreSQL, the "WITH (OIDS = FALSE)" clause you encountered in the CREATE TABLE statement is used to specify whether the table should have an internal object identifier (OID) column or not.

OIDs are essentially unique identifiers assigned to each row in a table by default in PostgreSQL. They can be useful in certain scenarios, such as:

  1. Referencing Rows: OIDs can serve as a way to refer to a specific row within a table, similar to a primary key. You can use the OID value to uniquely identify and reference a row from another table or in specific operations.

  2. System Catalog Tables: PostgreSQL system catalog tables, which store metadata about database objects, use OIDs internally to identify tables, indexes, and other objects. The OIDs can help in managing and querying system catalog tables efficiently.

  3. System Administration: OIDs can be useful for administrative tasks such as tracking and managing database objects, analyzing object dependencies, and performing low-level operations on the database.

However, it's important to note that starting from PostgreSQL version 12, the default behavior is to not include OIDs in user-created tables. This is because OIDs have some drawbacks, including potential performance and storage overhead. Therefore, unless you have specific use cases that require OIDs, it is generally recommended to omit the "WITH (OIDS = FALSE)" clause and let PostgreSQL create tables without OIDs.

Browse Categories

...