]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/datatype-oid.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / datatype-oid.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.19. Object Identifier Types</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="domains.html" title="8.18. Domain Types" /><link rel="next" href="datatype-pg-lsn.html" title="8.20. pg_lsn Type" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">8.19. Object Identifier Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="domains.html" title="8.18. Domain Types">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><th width="60%" align="center">Chapter 8. Data Types</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="datatype-pg-lsn.html" title="8.20. pg_lsn Type">Next</a></td></tr></table><hr /></div><div class="sect1" id="DATATYPE-OID"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.19. Object Identifier Types <a href="#DATATYPE-OID" class="id_link">#</a></h2></div></div></div><a id="id-1.5.7.27.2" class="indexterm"></a><a id="id-1.5.7.27.3" class="indexterm"></a><a id="id-1.5.7.27.4" class="indexterm"></a><a id="id-1.5.7.27.5" class="indexterm"></a><a id="id-1.5.7.27.6" class="indexterm"></a><a id="id-1.5.7.27.7" class="indexterm"></a><a id="id-1.5.7.27.8" class="indexterm"></a><a id="id-1.5.7.27.9" class="indexterm"></a><a id="id-1.5.7.27.10" class="indexterm"></a><a id="id-1.5.7.27.11" class="indexterm"></a><a id="id-1.5.7.27.12" class="indexterm"></a><a id="id-1.5.7.27.13" class="indexterm"></a><a id="id-1.5.7.27.14" class="indexterm"></a><a id="id-1.5.7.27.15" class="indexterm"></a><a id="id-1.5.7.27.16" class="indexterm"></a><a id="id-1.5.7.27.17" class="indexterm"></a><a id="id-1.5.7.27.18" class="indexterm"></a><p>
3     Object identifiers (OIDs) are used internally by
4     <span class="productname">PostgreSQL</span> as primary keys for various
5     system tables.
6     Type <code class="type">oid</code> represents an object identifier.  There are also
7     several alias types for <code class="type">oid</code>, each
8     named <code class="type">reg<em class="replaceable"><code>something</code></em></code>.
9     <a class="xref" href="datatype-oid.html#DATATYPE-OID-TABLE" title="Table 8.26. Object Identifier Types">Table 8.26</a> shows an
10     overview.
11    </p><p>
12     The <code class="type">oid</code> type is currently implemented as an unsigned
13     four-byte integer.  Therefore, it is not large enough to provide
14     database-wide uniqueness in large databases, or even in large
15     individual tables.
16    </p><p>
17     The <code class="type">oid</code> type itself has few operations beyond comparison.
18     It can be cast to integer, however, and then manipulated using the
19     standard integer operators.  (Beware of possible
20     signed-versus-unsigned confusion if you do this.)
21    </p><p>
22     The OID alias types have no operations of their own except
23     for specialized input and output routines.  These routines are able
24     to accept and display symbolic names for system objects, rather than
25     the raw numeric value that type <code class="type">oid</code> would use.  The alias
26     types allow simplified lookup of OID values for objects.  For example,
27     to examine the <code class="structname">pg_attribute</code> rows related to a table
28     <code class="literal">mytable</code>, one could write:
29 </p><pre class="programlisting">
30 SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
31 </pre><p>
32     rather than:
33 </p><pre class="programlisting">
34 SELECT * FROM pg_attribute
35   WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
36 </pre><p>
37     While that doesn't look all that bad by itself, it's still oversimplified.
38     A far more complicated sub-select would be needed to
39     select the right OID if there are multiple tables named
40     <code class="literal">mytable</code> in different schemas.
41     The <code class="type">regclass</code> input converter handles the table lookup according
42     to the schema path setting, and so it does the <span class="quote">“<span class="quote">right thing</span>”</span>
43     automatically.  Similarly, casting a table's OID to
44     <code class="type">regclass</code> is handy for symbolic display of a numeric OID.
45    </p><div class="table" id="DATATYPE-OID-TABLE"><p class="title"><strong>Table 8.26. Object Identifier Types</strong></p><div class="table-contents"><table class="table" summary="Object Identifier Types" border="1"><colgroup><col /><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>References</th><th>Description</th><th>Value Example</th></tr></thead><tbody><tr><td><code class="type">oid</code></td><td>any</td><td>numeric object identifier</td><td><code class="literal">564182</code></td></tr><tr><td><code class="type">regclass</code></td><td><code class="structname">pg_class</code></td><td>relation name</td><td><code class="literal">pg_type</code></td></tr><tr><td><code class="type">regcollation</code></td><td><code class="structname">pg_collation</code></td><td>collation name</td><td><code class="literal">"POSIX"</code></td></tr><tr><td><code class="type">regconfig</code></td><td><code class="structname">pg_ts_config</code></td><td>text search configuration</td><td><code class="literal">english</code></td></tr><tr><td><code class="type">regdictionary</code></td><td><code class="structname">pg_ts_dict</code></td><td>text search dictionary</td><td><code class="literal">simple</code></td></tr><tr><td><code class="type">regnamespace</code></td><td><code class="structname">pg_namespace</code></td><td>namespace name</td><td><code class="literal">pg_catalog</code></td></tr><tr><td><code class="type">regoper</code></td><td><code class="structname">pg_operator</code></td><td>operator name</td><td><code class="literal">+</code></td></tr><tr><td><code class="type">regoperator</code></td><td><code class="structname">pg_operator</code></td><td>operator with argument types</td><td><code class="literal">*(integer,​integer)</code>
46          or <code class="literal">-(NONE,​integer)</code></td></tr><tr><td><code class="type">regproc</code></td><td><code class="structname">pg_proc</code></td><td>function name</td><td><code class="literal">sum</code></td></tr><tr><td><code class="type">regprocedure</code></td><td><code class="structname">pg_proc</code></td><td>function with argument types</td><td><code class="literal">sum(int4)</code></td></tr><tr><td><code class="type">regrole</code></td><td><code class="structname">pg_authid</code></td><td>role name</td><td><code class="literal">smithee</code></td></tr><tr><td><code class="type">regtype</code></td><td><code class="structname">pg_type</code></td><td>data type name</td><td><code class="literal">integer</code></td></tr></tbody></table></div></div><br class="table-break" /><p>
47     All of the OID alias types for objects that are grouped by namespace
48     accept schema-qualified names, and will
49     display schema-qualified names on output if the object would not
50     be found in the current search path without being qualified.
51     For example, <code class="literal">myschema.mytable</code> is acceptable input
52     for <code class="type">regclass</code> (if there is such a table).  That value
53     might be output as <code class="literal">myschema.mytable</code>, or
54     just <code class="literal">mytable</code>, depending on the current search path.
55     The <code class="type">regproc</code> and <code class="type">regoper</code> alias types will only
56     accept input names that are unique (not overloaded), so they are
57     of limited use; for most uses <code class="type">regprocedure</code> or
58     <code class="type">regoperator</code> are more appropriate.  For <code class="type">regoperator</code>,
59     unary operators are identified by writing <code class="literal">NONE</code> for the unused
60     operand.
61    </p><p>
62     The input functions for these types allow whitespace between tokens,
63     and will fold upper-case letters to lower case, except within double
64     quotes; this is done to make the syntax rules similar to the way
65     object names are written in SQL.  Conversely, the output functions
66     will use double quotes if needed to make the output be a valid SQL
67     identifier.  For example, the OID of a function
68     named <code class="literal">Foo</code> (with upper case <code class="literal">F</code>)
69     taking two integer arguments could be entered as
70     <code class="literal">' "Foo" ( int, integer ) '::regprocedure</code>.  The
71     output would look like <code class="literal">"Foo"(integer,integer)</code>.
72     Both the function name and the argument type names could be
73     schema-qualified, too.
74    </p><p>
75     Many built-in <span class="productname">PostgreSQL</span> functions accept
76     the OID of a table, or another kind of database object, and for
77     convenience are declared as taking <code class="type">regclass</code> (or the
78     appropriate OID alias type).  This means you do not have to look up
79     the object's OID by hand, but can just enter its name as a string
80     literal.  For example, the <code class="function">nextval(regclass)</code> function
81     takes a sequence relation's OID, so you could call it like this:
82 </p><pre class="programlisting">
83 nextval('foo')              <em class="lineannotation"><span class="lineannotation">operates on sequence <code class="literal">foo</code></span></em>
84 nextval('FOO')              <em class="lineannotation"><span class="lineannotation">same as above</span></em>
85 nextval('"Foo"')            <em class="lineannotation"><span class="lineannotation">operates on sequence <code class="literal">Foo</code></span></em>
86 nextval('myschema.foo')     <em class="lineannotation"><span class="lineannotation">operates on <code class="literal">myschema.foo</code></span></em>
87 nextval('"myschema".foo')   <em class="lineannotation"><span class="lineannotation">same as above</span></em>
88 nextval('foo')              <em class="lineannotation"><span class="lineannotation">searches search path for <code class="literal">foo</code></span></em>
89 </pre><p>
90    </p><div class="note"><h3 class="title">Note</h3><p>
91      When you write the argument of such a function as an unadorned
92      literal string, it becomes a constant of type <code class="type">regclass</code>
93      (or the appropriate type).
94      Since this is really just an OID, it will track the originally
95      identified object despite later renaming, schema reassignment,
96      etc.  This <span class="quote">“<span class="quote">early binding</span>”</span> behavior is usually desirable for
97      object references in column defaults and views.  But sometimes you might
98      want <span class="quote">“<span class="quote">late binding</span>”</span> where the object reference is resolved
99      at run time.  To get late-binding behavior, force the constant to be
100      stored as a <code class="type">text</code> constant instead of <code class="type">regclass</code>:
101 </p><pre class="programlisting">
102 nextval('foo'::text)      <em class="lineannotation"><span class="lineannotation"><code class="literal">foo</code> is looked up at runtime</span></em>
103 </pre><p>
104      The <code class="function">to_regclass()</code> function and its siblings
105      can also be used to perform run-time lookups.  See
106      <a class="xref" href="functions-info.html#FUNCTIONS-INFO-CATALOG-TABLE" title="Table 9.76. System Catalog Information Functions">Table 9.76</a>.
107     </p></div><p>
108     Another practical example of use of <code class="type">regclass</code>
109     is to look up the OID of a table listed in
110     the <code class="literal">information_schema</code> views, which don't supply
111     such OIDs directly.  One might for example wish to call
112     the <code class="function">pg_relation_size()</code> function, which requires
113     the table OID.  Taking the above rules into account, the correct way
114     to do that is
115 </p><pre class="programlisting">
116 SELECT table_schema, table_name,
117        pg_relation_size((quote_ident(table_schema) || '.' ||
118                          quote_ident(table_name))::regclass)
119 FROM information_schema.tables
120 WHERE ...
121 </pre><p>
122     The <code class="function">quote_ident()</code> function will take care of
123     double-quoting the identifiers where needed.  The seemingly easier
124 </p><pre class="programlisting">
125 SELECT pg_relation_size(table_name)
126 FROM information_schema.tables
127 WHERE ...
128 </pre><p>
129     is <span class="emphasis"><em>not recommended</em></span>, because it will fail for
130     tables that are outside your search path or have names that require
131     quoting.
132    </p><p>
133     An additional property of most of the OID alias types is the creation of
134     dependencies.  If a
135     constant of one of these types appears in a stored expression
136     (such as a column default expression or view), it creates a dependency
137     on the referenced object.  For example, if a column has a default
138     expression <code class="literal">nextval('my_seq'::regclass)</code>,
139     <span class="productname">PostgreSQL</span>
140     understands that the default expression depends on the sequence
141     <code class="literal">my_seq</code>, so the system will not let the sequence
142     be dropped without first removing the default expression.  The
143     alternative of <code class="literal">nextval('my_seq'::text)</code> does not
144     create a dependency.
145     (<code class="type">regrole</code> is an exception to this property. Constants of this
146     type are not allowed in stored expressions.)
147    </p><p>
148     Another identifier type used by the system is <code class="type">xid</code>, or transaction
149     (abbreviated <abbr class="abbrev">xact</abbr>) identifier.  This is the data type of the system columns
150     <code class="structfield">xmin</code> and <code class="structfield">xmax</code>.  Transaction identifiers are 32-bit quantities.
151     In some contexts, a 64-bit variant <code class="type">xid8</code> is used.  Unlike
152     <code class="type">xid</code> values, <code class="type">xid8</code> values increase strictly
153     monotonically and cannot be reused in the lifetime of a database
154     cluster.  See <a class="xref" href="transaction-id.html" title="67.1. Transactions and Identifiers">Section 67.1</a> for more details.
155    </p><p>
156     A third identifier type used by the system is <code class="type">cid</code>, or
157     command identifier.  This is the data type of the system columns
158     <code class="structfield">cmin</code> and <code class="structfield">cmax</code>. Command identifiers are also 32-bit quantities.
159    </p><p>
160     A final identifier type used by the system is <code class="type">tid</code>, or tuple
161     identifier (row identifier).  This is the data type of the system column
162     <code class="structfield">ctid</code>.  A tuple ID is a pair
163     (block number, tuple index within block) that identifies the
164     physical location of the row within its table.
165    </p><p>
166     (The system columns are further explained in <a class="xref" href="ddl-system-columns.html" title="5.6. System Columns">Section 5.6</a>.)
167    </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="domains.html" title="8.18. Domain Types">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="Chapter 8. Data Types">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="datatype-pg-lsn.html" title="8.20. pg_lsn Type">Next</a></td></tr><tr><td width="40%" align="left" valign="top">8.18. Domain Types </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 8.20. <code class="type">pg_lsn</code> Type</td></tr></table></div></body></html>