Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f33a178

Browse files
committed
Doc: improve and centralize the documentation for OID alias types.
Previously, a lot of information about type regclass existed only in the discussion of the sequence functions. Maybe that made sense in the beginning, because I think originally those were the only functions taking regclass. But it doesn't make sense anymore. Move that material to the "Object Identifier Types" section in datatype.sgml, generalize it to talk about the other reg* types as well, and add more examples. Per bug #16991 from Federico Caselli. Discussion: https://postgr.es/m/16991-bcaeaafa17e0a723@postgresql.org
1 parent 38f36aa commit f33a178

File tree

2 files changed

+102
-64
lines changed

2 files changed

+102
-64
lines changed

doc/src/sgml/datatype.sgml

+95-15
Original file line numberDiff line numberDiff line change
@@ -4632,7 +4632,8 @@ INSERT INTO mytable VALUES(-1); -- fails
46324632
<productname>PostgreSQL</productname> as primary keys for various
46334633
system tables.
46344634
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>.
46364637
<xref linkend="datatype-oid-table"/> shows an
46374638
overview.
46384639
</para>
@@ -4780,10 +4781,14 @@ SELECT * FROM pg_attribute
47804781
</table>
47814782

47824783
<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
47854786
display schema-qualified names on output if the object would not
47864787
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.
47874792
The <type>regproc</type> and <type>regoper</type> alias types will only
47884793
accept input names that are unique (not overloaded), so they are
47894794
of limited use; for most uses <type>regprocedure</type> or
@@ -4792,6 +4797,87 @@ SELECT * FROM pg_attribute
47924797
operand.
47934798
</para>
47944799

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+
47954881
<para>
47964882
An additional property of most of the OID alias types is the creation of
47974883
dependencies. If a
@@ -4801,19 +4887,13 @@ SELECT * FROM pg_attribute
48014887
expression <literal>nextval('my_seq'::regclass)</literal>,
48024888
<productname>PostgreSQL</productname>
48034889
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.)
48154896
</para>
4816-
</note>
48174897

48184898
<para>
48194899
Another identifier type used by the system is <type>xid</type>, or transaction

doc/src/sgml/func.sgml

+7-49
Original file line numberDiff line numberDiff line change
@@ -14429,8 +14429,9 @@ SELECT xmltable.*
1442914429
<function>table_to_xml</function> maps the content of the named
1443014430
table, passed as parameter <parameter>table</parameter>. The
1443114431
<type>regclass</type> type accepts strings identifying tables using the
14432-
usual notation, including optional schema qualifications and
14433-
double quotes. <function>query_to_xml</function> executes the
14432+
usual notation, including optional schema qualification and
14433+
double quotes (see <xref linkend="datatype-oid"/> for details).
14434+
<function>query_to_xml</function> executes the
1443414435
query whose text is passed as parameter
1443514436
<parameter>query</parameter> and maps the result set.
1443614437
<function>cursor_to_xml</function> fetches the indicated number of
@@ -17316,49 +17317,9 @@ SELECT setval('myseq', 42, false); <lineannotation>Next <function>nextval</fu
1731617317
a <type>regclass</type> argument, which is simply the OID of the sequence in the
1731717318
<structname>pg_class</structname> system catalog. You do not have to look up the
1731817319
OID by hand, however, since the <type>regclass</type> data type's input
17319-
converter will do the work for you. Just write the sequence name enclosed
17320-
in single quotes so that it looks like a literal constant. For
17321-
compatibility with the handling of ordinary
17322-
<acronym>SQL</acronym> names, the string will be converted to lower case
17323-
unless it contains double quotes around the sequence name. Thus:
17324-
<programlisting>
17325-
nextval('foo') <lineannotation>operates on sequence <literal>foo</literal></lineannotation>
17326-
nextval('FOO') <lineannotation>operates on sequence <literal>foo</literal></lineannotation>
17327-
nextval('"Foo"') <lineannotation>operates on sequence <literal>Foo</literal></lineannotation>
17328-
</programlisting>
17329-
The sequence name can be schema-qualified if necessary:
17330-
<programlisting>
17331-
nextval('myschema.foo') <lineannotation>operates on <literal>myschema.foo</literal></lineannotation>
17332-
nextval('"myschema".foo') <lineannotation>same as above</lineannotation>
17333-
nextval('foo') <lineannotation>searches search path for <literal>foo</literal></lineannotation>
17334-
</programlisting>
17335-
See <xref linkend="datatype-oid"/> for more information about
17336-
<type>regclass</type>.
17320+
converter will do the work for you. See <xref linkend="datatype-oid"/>
17321+
for details.
1733717322
</para>
17338-
17339-
<note>
17340-
<para>
17341-
When you write the argument of a sequence function as an unadorned
17342-
literal string, it becomes a constant of type <type>regclass</type>.
17343-
Since this is really just an OID, it will track the originally
17344-
identified sequence despite later renaming, schema reassignment,
17345-
etc. This <quote>early binding</quote> behavior is usually desirable for
17346-
sequence references in column defaults and views. But sometimes you might
17347-
want <quote>late binding</quote> where the sequence reference is resolved
17348-
at run time. To get late-binding behavior, force the constant to be
17349-
stored as a <type>text</type> constant instead of <type>regclass</type>:
17350-
<programlisting>
17351-
nextval('foo'::text) <lineannotation><literal>foo</literal> is looked up at runtime</lineannotation>
17352-
</programlisting>
17353-
</para>
17354-
17355-
<para>
17356-
Of course, the argument of a sequence function can be an expression
17357-
as well as a constant. If it is a text expression then the implicit
17358-
coercion will result in a run-time lookup.
17359-
</para>
17360-
</note>
17361-
1736217323
</sect1>
1736317324

1736417325

@@ -26474,11 +26435,8 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
2647426435
<type>regclass</type> argument, which is simply the OID of the table or index
2647526436
in the <structname>pg_class</structname> system catalog. You do not have to look up
2647626437
the OID by hand, however, since the <type>regclass</type> data type's input
26477-
converter will do the work for you. Just write the table name enclosed in
26478-
single quotes so that it looks like a literal constant. For compatibility
26479-
with the handling of ordinary <acronym>SQL</acronym> names, the string
26480-
will be converted to lower case unless it contains double quotes around
26481-
the table name.
26438+
converter will do the work for you. See <xref linkend="datatype-oid"/>
26439+
for details.
2648226440
</para>
2648326441

2648426442
<para>

0 commit comments

Comments
 (0)