2 8.19. Object Identifier Types #
4 Object identifiers (OIDs) are used internally by PostgreSQL as primary
5 keys for various system tables. Type oid represents an object
6 identifier. There are also several alias types for oid, each named
7 regsomething. Table 8.26 shows an overview.
9 The oid type is currently implemented as an unsigned four-byte integer.
10 Therefore, it is not large enough to provide database-wide uniqueness
11 in large databases, or even in large individual tables.
13 The oid type itself has few operations beyond comparison. It can be
14 cast to integer, however, and then manipulated using the standard
15 integer operators. (Beware of possible signed-versus-unsigned confusion
18 The OID alias types have no operations of their own except for
19 specialized input and output routines. These routines are able to
20 accept and display symbolic names for system objects, rather than the
21 raw numeric value that type oid would use. The alias types allow
22 simplified lookup of OID values for objects. For example, to examine
23 the pg_attribute rows related to a table mytable, one could write:
24 SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
27 SELECT * FROM pg_attribute
28 WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
30 While that doesn't look all that bad by itself, it's still
31 oversimplified. A far more complicated sub-select would be needed to
32 select the right OID if there are multiple tables named mytable in
33 different schemas. The regclass input converter handles the table
34 lookup according to the schema path setting, and so it does the “right
35 thing” automatically. Similarly, casting a table's OID to regclass is
36 handy for symbolic display of a numeric OID.
38 Table 8.26. Object Identifier Types
39 Name References Description Value Example
40 oid any numeric object identifier 564182
41 regclass pg_class relation name pg_type
42 regcollation pg_collation collation name "POSIX"
43 regconfig pg_ts_config text search configuration english
44 regdictionary pg_ts_dict text search dictionary simple
45 regnamespace pg_namespace namespace name pg_catalog
46 regoper pg_operator operator name +
47 regoperator pg_operator operator with argument types
48 *(integer,integer) or -(NONE,integer)
49 regproc pg_proc function name sum
50 regprocedure pg_proc function with argument types sum(int4)
51 regrole pg_authid role name smithee
52 regtype pg_type data type name integer
54 All of the OID alias types for objects that are grouped by namespace
55 accept schema-qualified names, and will display schema-qualified names
56 on output if the object would not be found in the current search path
57 without being qualified. For example, myschema.mytable is acceptable
58 input for regclass (if there is such a table). That value might be
59 output as myschema.mytable, or just mytable, depending on the current
60 search path. The regproc and regoper alias types will only accept input
61 names that are unique (not overloaded), so they are of limited use; for
62 most uses regprocedure or regoperator are more appropriate. For
63 regoperator, unary operators are identified by writing NONE for the
66 The input functions for these types allow whitespace between tokens,
67 and will fold upper-case letters to lower case, except within double
68 quotes; this is done to make the syntax rules similar to the way object
69 names are written in SQL. Conversely, the output functions will use
70 double quotes if needed to make the output be a valid SQL identifier.
71 For example, the OID of a function named Foo (with upper case F) taking
72 two integer arguments could be entered as ' "Foo" ( int, integer )
73 '::regprocedure. The output would look like "Foo"(integer,integer).
74 Both the function name and the argument type names could be
75 schema-qualified, too.
77 Many built-in PostgreSQL functions accept the OID of a table, or
78 another kind of database object, and for convenience are declared as
79 taking regclass (or the appropriate OID alias type). This means you do
80 not have to look up the object's OID by hand, but can just enter its
81 name as a string literal. For example, the nextval(regclass) function
82 takes a sequence relation's OID, so you could call it like this:
83 nextval('foo') operates on sequence foo
84 nextval('FOO') same as above
85 nextval('"Foo"') operates on sequence Foo
86 nextval('myschema.foo') operates on myschema.foo
87 nextval('"myschema".foo') same as above
88 nextval('foo') searches search path for foo
92 When you write the argument of such a function as an unadorned literal
93 string, it becomes a constant of type regclass (or the appropriate
94 type). Since this is really just an OID, it will track the originally
95 identified object despite later renaming, schema reassignment, etc.
96 This “early binding” behavior is usually desirable for object
97 references in column defaults and views. But sometimes you might want
98 “late binding” where the object reference is resolved at run time. To
99 get late-binding behavior, force the constant to be stored as a text
100 constant instead of regclass:
101 nextval('foo'::text) foo is looked up at runtime
103 The to_regclass() function and its siblings can also be used to perform
104 run-time lookups. See Table 9.76.
106 Another practical example of use of regclass is to look up the OID of a
107 table listed in the information_schema views, which don't supply such
108 OIDs directly. One might for example wish to call the
109 pg_relation_size() function, which requires the table OID. Taking the
110 above rules into account, the correct way to do that is
111 SELECT table_schema, table_name,
112 pg_relation_size((quote_ident(table_schema) || '.' ||
113 quote_ident(table_name))::regclass)
114 FROM information_schema.tables
117 The quote_ident() function will take care of double-quoting the
118 identifiers where needed. The seemingly easier
119 SELECT pg_relation_size(table_name)
120 FROM information_schema.tables
123 is not recommended, because it will fail for tables that are outside
124 your search path or have names that require quoting.
126 An additional property of most of the OID alias types is the creation
127 of dependencies. If a constant of one of these types appears in a
128 stored expression (such as a column default expression or view), it
129 creates a dependency on the referenced object. For example, if a column
130 has a default expression nextval('my_seq'::regclass), PostgreSQL
131 understands that the default expression depends on the sequence my_seq,
132 so the system will not let the sequence be dropped without first
133 removing the default expression. The alternative of
134 nextval('my_seq'::text) does not create a dependency. (regrole is an
135 exception to this property. Constants of this type are not allowed in
138 Another identifier type used by the system is xid, or transaction
139 (abbreviated xact) identifier. This is the data type of the system
140 columns xmin and xmax. Transaction identifiers are 32-bit quantities.
141 In some contexts, a 64-bit variant xid8 is used. Unlike xid values,
142 xid8 values increase strictly monotonically and cannot be reused in the
143 lifetime of a database cluster. See Section 67.1 for more details.
145 A third identifier type used by the system is cid, or command
146 identifier. This is the data type of the system columns cmin and cmax.
147 Command identifiers are also 32-bit quantities.
149 A final identifier type used by the system is tid, or tuple identifier
150 (row identifier). This is the data type of the system column ctid. A
151 tuple ID is a pair (block number, tuple index within block) that
152 identifies the physical location of the row within its table.
154 (The system columns are further explained in Section 5.6.)