1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +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

@@ -192,6 +192,18 @@ SELECT regnamespace('"pg_catalog"');
pg_catalog
(1 row)
SELECT regdatabase('template1');
regdatabase
-------------
template1
(1 row)
SELECT regdatabase('"template1"');
regdatabase
-------------
template1
(1 row)
SELECT to_regrole('regress_regrole_test');
to_regrole
----------------------
@@ -216,6 +228,132 @@ SELECT to_regnamespace('"pg_catalog"');
pg_catalog
(1 row)
SELECT to_regdatabase('template1');
to_regdatabase
----------------
template1
(1 row)
SELECT to_regdatabase('"template1"');
to_regdatabase
----------------
template1
(1 row)
-- special "single dash" case
SELECT regproc('-')::oid;
regproc
---------
0
(1 row)
SELECT regprocedure('-')::oid;
regprocedure
--------------
0
(1 row)
SELECT regclass('-')::oid;
regclass
----------
0
(1 row)
SELECT regcollation('-')::oid;
regcollation
--------------
0
(1 row)
SELECT regtype('-')::oid;
regtype
---------
0
(1 row)
SELECT regconfig('-')::oid;
regconfig
-----------
0
(1 row)
SELECT regdictionary('-')::oid;
regdictionary
---------------
0
(1 row)
SELECT regrole('-')::oid;
regrole
---------
0
(1 row)
SELECT regnamespace('-')::oid;
regnamespace
--------------
0
(1 row)
SELECT regdatabase('-')::oid;
regdatabase
-------------
0
(1 row)
SELECT to_regproc('-')::oid;
to_regproc
------------
0
(1 row)
SELECT to_regprocedure('-')::oid;
to_regprocedure
-----------------
0
(1 row)
SELECT to_regclass('-')::oid;
to_regclass
-------------
0
(1 row)
SELECT to_regcollation('-')::oid;
to_regcollation
-----------------
0
(1 row)
SELECT to_regtype('-')::oid;
to_regtype
------------
0
(1 row)
SELECT to_regrole('-')::oid;
to_regrole
------------
0
(1 row)
SELECT to_regnamespace('-')::oid;
to_regnamespace
-----------------
0
(1 row)
SELECT to_regdatabase('-')::oid;
to_regdatabase
----------------
0
(1 row)
-- constant cannot be used here
CREATE TABLE regrole_test (rolid OID DEFAULT 'regress_regrole_test'::regrole);
ERROR: constant of the type regrole cannot be used here
CREATE TABLE regdatabase_test (datid OID DEFAULT 'template1'::regdatabase);
ERROR: constant of the type regdatabase cannot be used here
/* If objects don't exist, raise errors. */
DROP ROLE regress_regrole_test;
-- without schemaname
@@ -305,6 +443,18 @@ SELECT regnamespace('foo.bar');
ERROR: invalid name syntax
LINE 1: SELECT regnamespace('foo.bar');
^
SELECT regdatabase('Nonexistent');
ERROR: database "nonexistent" does not exist
LINE 1: SELECT regdatabase('Nonexistent');
^
SELECT regdatabase('"Nonexistent"');
ERROR: database "Nonexistent" does not exist
LINE 1: SELECT regdatabase('"Nonexistent"');
^
SELECT regdatabase('foo.bar');
ERROR: invalid name syntax
LINE 1: SELECT regdatabase('foo.bar');
^
/* If objects don't exist, return NULL with no error. */
-- without schemaname
SELECT to_regoper('||//');
@@ -447,6 +597,24 @@ SELECT to_regnamespace('foo.bar');
(1 row)
SELECT to_regdatabase('Nonexistent');
to_regdatabase
----------------
(1 row)
SELECT to_regdatabase('"Nonexistent"');
to_regdatabase
----------------
(1 row)
SELECT to_regdatabase('foo.bar');
to_regdatabase
----------------
(1 row)
-- Test to_regtypemod
SELECT to_regtypemod('text');
to_regtypemod
@@ -569,6 +737,12 @@ SELECT * FROM pg_input_error_info('no_such_type', 'regtype');
type "no_such_type" does not exist | | | 42704
(1 row)
SELECT * FROM pg_input_error_info('Nonexistent', 'regdatabase');
message | detail | hint | sql_error_code
---------------------------------------+--------+------+----------------
database "nonexistent" does not exist | | | 42704
(1 row)
-- Some cases that should be soft errors, but are not yet
SELECT * FROM pg_input_error_info('incorrect type name syntax', 'regtype');
ERROR: syntax error at or near "type"

View File

@@ -711,6 +711,7 @@ CREATE TABLE tab_core_types AS SELECT
'regtype'::regtype type,
'pg_monitor'::regrole,
'pg_class'::regclass::oid,
'template1'::regdatabase,
'(1,1)'::tid, '2'::xid, '3'::cid,
'10:20:10,14,15'::txid_snapshot,
'10:20:10,14,15'::pg_snapshot,

View File

@@ -47,11 +47,42 @@ SELECT regrole('regress_regrole_test');
SELECT regrole('"regress_regrole_test"');
SELECT regnamespace('pg_catalog');
SELECT regnamespace('"pg_catalog"');
SELECT regdatabase('template1');
SELECT regdatabase('"template1"');
SELECT to_regrole('regress_regrole_test');
SELECT to_regrole('"regress_regrole_test"');
SELECT to_regnamespace('pg_catalog');
SELECT to_regnamespace('"pg_catalog"');
SELECT to_regdatabase('template1');
SELECT to_regdatabase('"template1"');
-- special "single dash" case
SELECT regproc('-')::oid;
SELECT regprocedure('-')::oid;
SELECT regclass('-')::oid;
SELECT regcollation('-')::oid;
SELECT regtype('-')::oid;
SELECT regconfig('-')::oid;
SELECT regdictionary('-')::oid;
SELECT regrole('-')::oid;
SELECT regnamespace('-')::oid;
SELECT regdatabase('-')::oid;
SELECT to_regproc('-')::oid;
SELECT to_regprocedure('-')::oid;
SELECT to_regclass('-')::oid;
SELECT to_regcollation('-')::oid;
SELECT to_regtype('-')::oid;
SELECT to_regrole('-')::oid;
SELECT to_regnamespace('-')::oid;
SELECT to_regdatabase('-')::oid;
-- constant cannot be used here
CREATE TABLE regrole_test (rolid OID DEFAULT 'regress_regrole_test'::regrole);
CREATE TABLE regdatabase_test (datid OID DEFAULT 'template1'::regdatabase);
/* If objects don't exist, raise errors. */
@@ -88,6 +119,9 @@ SELECT regrole('foo.bar');
SELECT regnamespace('Nonexistent');
SELECT regnamespace('"Nonexistent"');
SELECT regnamespace('foo.bar');
SELECT regdatabase('Nonexistent');
SELECT regdatabase('"Nonexistent"');
SELECT regdatabase('foo.bar');
/* If objects don't exist, return NULL with no error. */
@@ -122,6 +156,9 @@ SELECT to_regrole('foo.bar');
SELECT to_regnamespace('Nonexistent');
SELECT to_regnamespace('"Nonexistent"');
SELECT to_regnamespace('foo.bar');
SELECT to_regdatabase('Nonexistent');
SELECT to_regdatabase('"Nonexistent"');
SELECT to_regdatabase('foo.bar');
-- Test to_regtypemod
SELECT to_regtypemod('text');
@@ -147,6 +184,7 @@ SELECT * FROM pg_input_error_info('ng_catalog.abs(numeric)', 'regprocedure');
SELECT * FROM pg_input_error_info('ng_catalog.abs(numeric', 'regprocedure');
SELECT * FROM pg_input_error_info('regress_regrole_test', 'regrole');
SELECT * FROM pg_input_error_info('no_such_type', 'regtype');
SELECT * FROM pg_input_error_info('Nonexistent', 'regdatabase');
-- Some cases that should be soft errors, but are not yet
SELECT * FROM pg_input_error_info('incorrect type name syntax', 'regtype');

View File

@@ -539,6 +539,7 @@ CREATE TABLE tab_core_types AS SELECT
'regtype'::regtype type,
'pg_monitor'::regrole,
'pg_class'::regclass::oid,
'template1'::regdatabase,
'(1,1)'::tid, '2'::xid, '3'::cid,
'10:20:10,14,15'::txid_snapshot,
'10:20:10,14,15'::pg_snapshot,