1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Add new OID alias type regdatabase.

This provides a convenient way to look up a database's OID.  For
example, the query

    SELECT * FROM pg_shdepend
    WHERE dbid = (SELECT oid FROM pg_database
                  WHERE datname = current_database());

can now be simplified to

    SELECT * FROM pg_shdepend
    WHERE dbid = current_database()::regdatabase;

Like the regrole type, regdatabase has cluster-wide scope, so we
disallow regdatabase constants from appearing in stored
expressions.

Bumps catversion.

Author: Ian Lawrence Barwick <barwick@gmail.com>
Reviewed-by: Greg Sabino Mullane <htamfids@gmail.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Reviewed-by: Fabrízio de Royes Mello <fabriziomello@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/aBpjJhyHpM2LYcG0%40nathan
This commit is contained in:
Nathan Bossart
2025-06-30 15:38:54 -05:00
parent f20a347e1a
commit bd09f024a1
18 changed files with 424 additions and 4 deletions

View File

@ -4737,6 +4737,10 @@ INSERT INTO mytable VALUES(-1); -- fails
<primary>regconfig</primary>
</indexterm>
<indexterm zone="datatype-oid">
<primary>regdatabase</primary>
</indexterm>
<indexterm zone="datatype-oid">
<primary>regdictionary</primary>
</indexterm>
@ -4878,6 +4882,13 @@ SELECT * FROM pg_attribute
<entry><literal>english</literal></entry>
</row>
<row>
<entry><type>regdatabase</type></entry>
<entry><structname>pg_database</structname></entry>
<entry>database name</entry>
<entry><literal>template1</literal></entry>
</row>
<row>
<entry><type>regdictionary</type></entry>
<entry><structname>pg_ts_dict</structname></entry>
@ -5049,8 +5060,8 @@ WHERE ...
be dropped without first removing the default expression. The
alternative of <literal>nextval('my_seq'::text)</literal> does not
create a dependency.
(<type>regrole</type> is an exception to this property. Constants of this
type are not allowed in stored expressions.)
(<type>regdatabase</type> and <type>regrole</type> are exceptions to this
property. Constants of these types are not allowed in stored expressions.)
</para>
<para>

View File

@ -26750,6 +26750,23 @@ SELECT currval(pg_get_serial_sequence('sometable', 'id'));
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>to_regdatabase</primary>
</indexterm>
<function>to_regdatabase</function> ( <type>text</type> )
<returnvalue>regdatabase</returnvalue>
</para>
<para>
Translates a textual database name to its OID. A similar result is
obtained by casting the string to type <type>regdatabase</type> (see
<xref linkend="datatype-oid"/>); however, this function will return
<literal>NULL</literal> rather than throwing an error if the name is
not found.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>

View File

@ -1110,7 +1110,8 @@ psql --username=postgres --file=script.sql postgres
<member><type>regproc</type></member>
<member><type>regprocedure</type></member>
</simplelist>
(<type>regclass</type>, <type>regrole</type>, and <type>regtype</type> can be upgraded.)
(<type>regclass</type>, <type>regdatabase</type>, <type>regrole</type>, and
<type>regtype</type> can be upgraded.)
</para>
<para>