@@ -4632,7 +4632,8 @@ INSERT INTO mytable VALUES(-1); -- fails
4632
4632
<productname>PostgreSQL</productname> as primary keys for various
4633
4633
system tables.
4634
4634
Type <type>oid</type> represents an object identifier. There are also
4635
- several alias types for <type>oid</type> named <type>reg<replaceable>something</replaceable></type>.
4635
+ several alias types for <type>oid</type>, each
4636
+ named <type>reg<replaceable>something</replaceable></type>.
4636
4637
<xref linkend="datatype-oid-table"/> shows an
4637
4638
overview.
4638
4639
</para>
@@ -4780,10 +4781,14 @@ SELECT * FROM pg_attribute
4780
4781
</table>
4781
4782
4782
4783
<para>
4783
- All of the OID alias types for objects grouped by namespace accept
4784
- schema-qualified names, and will
4784
+ All of the OID alias types for objects that are grouped by namespace
4785
+ accept schema-qualified names, and will
4785
4786
display schema-qualified names on output if the object would not
4786
4787
be found in the current search path without being qualified.
4788
+ For example, <literal>myschema.mytable</literal> is acceptable input
4789
+ for <type>regclass</type> (if there is such a table). That value
4790
+ might be output as <literal>myschema.mytable</literal>, or
4791
+ just <literal>mytable</literal>, depending on the current search path.
4787
4792
The <type>regproc</type> and <type>regoper</type> alias types will only
4788
4793
accept input names that are unique (not overloaded), so they are
4789
4794
of limited use; for most uses <type>regprocedure</type> or
@@ -4792,6 +4797,87 @@ SELECT * FROM pg_attribute
4792
4797
operand.
4793
4798
</para>
4794
4799
4800
+ <para>
4801
+ The input functions for these types allow whitespace between tokens,
4802
+ and will fold upper-case letters to lower case, except within double
4803
+ quotes; this is done to make the syntax rules similar to the way
4804
+ object names are written in SQL. Conversely, the output functions
4805
+ will use double quotes if needed to make the output be a valid SQL
4806
+ identifier. For example, the OID of a function
4807
+ named <literal>Foo</literal> (with upper case <literal>F</literal>)
4808
+ taking two integer arguments could be entered as
4809
+ <literal>' "Foo" ( int, integer ) '::regprocedure</literal>. The
4810
+ output would look like <literal>"Foo"(integer,integer)</literal>.
4811
+ Both the function name and the argument type names could be
4812
+ schema-qualified, too.
4813
+ </para>
4814
+
4815
+ <para>
4816
+ Many built-in <productname>PostgreSQL</productname> functions accept
4817
+ the OID of a table, or another kind of database object, and for
4818
+ convenience are declared as taking <type>regclass</type> (or the
4819
+ appropriate OID alias type). This means you do not have to look up
4820
+ the object's OID by hand, but can just enter its name as a string
4821
+ literal. For example, the <function>nextval(regclass)</function> function
4822
+ takes a sequence relation's OID, so you could call it like this:
4823
+ <programlisting>
4824
+ nextval('foo') <lineannotation>operates on sequence <literal>foo</literal></lineannotation>
4825
+ nextval('FOO') <lineannotation>same as above</lineannotation>
4826
+ nextval('"Foo"') <lineannotation>operates on sequence <literal>Foo</literal></lineannotation>
4827
+ nextval('myschema.foo') <lineannotation>operates on <literal>myschema.foo</literal></lineannotation>
4828
+ nextval('"myschema".foo') <lineannotation>same as above</lineannotation>
4829
+ nextval('foo') <lineannotation>searches search path for <literal>foo</literal></lineannotation>
4830
+ </programlisting>
4831
+ </para>
4832
+
4833
+ <note>
4834
+ <para>
4835
+ When you write the argument of such a function as an unadorned
4836
+ literal string, it becomes a constant of type <type>regclass</type>
4837
+ (or the appropriate type).
4838
+ Since this is really just an OID, it will track the originally
4839
+ identified object despite later renaming, schema reassignment,
4840
+ etc. This <quote>early binding</quote> behavior is usually desirable for
4841
+ object references in column defaults and views. But sometimes you might
4842
+ want <quote>late binding</quote> where the object reference is resolved
4843
+ at run time. To get late-binding behavior, force the constant to be
4844
+ stored as a <type>text</type> constant instead of <type>regclass</type>:
4845
+ <programlisting>
4846
+ nextval('foo'::text) <lineannotation><literal>foo</literal> is looked up at runtime</lineannotation>
4847
+ </programlisting>
4848
+ The <function>to_regclass()</function> function and its siblings
4849
+ can also be used to perform run-time lookups. See
4850
+ <xref linkend="functions-info-catalog-table"/>.
4851
+ </para>
4852
+ </note>
4853
+
4854
+ <para>
4855
+ Another practical example of use of <type>regclass</type>
4856
+ is to look up the OID of a table listed in
4857
+ the <literal>information_schema</literal> views, which don't supply
4858
+ such OIDs directly. One might for example wish to call
4859
+ the <function>pg_relation_size()</function> function, which requires
4860
+ the table OID. Taking the above rules into account, the correct way
4861
+ to do that is
4862
+ <programlisting>
4863
+ SELECT table_schema, table_name,
4864
+ pg_relation_size((quote_ident(table_schema) || '.' ||
4865
+ quote_ident(table_name))::regclass)
4866
+ FROM information_schema.tables
4867
+ WHERE ...
4868
+ </programlisting>
4869
+ The <function>quote_ident()</function> function will take care of
4870
+ double-quoting the identifiers where needed. The seemingly easier
4871
+ <programlisting>
4872
+ SELECT pg_relation_size(table_name)
4873
+ FROM information_schema.tables
4874
+ WHERE ...
4875
+ </programlisting>
4876
+ is <emphasis>not recommended</emphasis>, because it will fail for
4877
+ tables that are outside your search path or have names that require
4878
+ quoting.
4879
+ </para>
4880
+
4795
4881
<para>
4796
4882
An additional property of most of the OID alias types is the creation of
4797
4883
dependencies. If a
@@ -4801,19 +4887,13 @@ SELECT * FROM pg_attribute
4801
4887
expression <literal>nextval('my_seq'::regclass)</literal>,
4802
4888
<productname>PostgreSQL</productname>
4803
4889
understands that the default expression depends on the sequence
4804
- <literal>my_seq</literal>; the system will not let the sequence be dropped
4805
- without first removing the default expression.
4806
- <type>regrole</type> is the only exception for the property. Constants of this
4807
- type are not allowed in such expressions.
4808
- </para>
4809
-
4810
- <note>
4811
- <para>
4812
- The OID alias types do not completely follow transaction isolation
4813
- rules. The planner also treats them as simple constants, which may
4814
- result in sub-optimal planning.
4890
+ <literal>my_seq</literal>, so the system will not let the sequence
4891
+ be dropped without first removing the default expression. The
4892
+ alternative of <literal>nextval('my_seq'::text)</literal> does not
4893
+ create a dependency.
4894
+ (<type>regrole</type> is an exception to this property. Constants of this
4895
+ type are not allowed in stored expressions.)
4815
4896
</para>
4816
- </note>
4817
4897
4818
4898
<para>
4819
4899
Another identifier type used by the system is <type>xid</type>, or transaction
0 commit comments