pg_get_databasebyid(oid)

Started by Sergei Kornilovabout 7 years ago8 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t41147
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 28, 2026 at 03:52 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t41147_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t41147_1 && git checkout t41147_1

Patchset v1 (message #1) is on t41147_1

Jump to latest

Hello
We already have function pg_get_userbyid(oid) with lookup in pg_authid catalog. My collegue ask me can we add similar function pg_get_databasebyid(oid) with lookup in pg_databases.
It is simple function to get a database name by oid and fallback to 'unknown (OID=n)' if missing.

The proposed patch is attached. Currently I missed the tests - I doubt which file in src/test/regress/sql/ is the most suitable. pg_get_userbyid is called from privileges.sql only.

regards, Sergei

Attachments:

t41147_1
v1_pg_get_databasebyid.patchtext/x-diff; name=v1_pg_get_databasebyid.patchDownload+51-2
#2Ibrar Ahmed
ibrar.ahmad@gmail.com
In reply to: Sergei Kornilov (#1)
Re: pg_get_databasebyid(oid)

On Wed, Aug 28, 2019 at 5:38 PM Sergei Kornilov <sk@zsrv.org> wrote:

Hello
We already have function pg_get_userbyid(oid) with lookup in pg_authid
catalog. My collegue ask me can we add similar function
pg_get_databasebyid(oid) with lookup in pg_databases.
It is simple function to get a database name by oid and fallback to
'unknown (OID=n)' if missing.

The proposed patch is attached. Currently I missed the tests - I doubt
which file in src/test/regress/sql/ is the most suitable. pg_get_userbyid
is called from privileges.sql only.

regards, Sergei

Please add that to commitfest.

--
Ibrar Ahmed

In reply to: Ibrar Ahmed (#2)
Re: pg_get_databasebyid(oid)

Please add that to commitfest.

Done: https://commitfest.postgresql.org/24/2261/

regards, Sergei

#4Ibrar Ahmed
ibrar.ahmad@gmail.com
In reply to: Sergei Kornilov (#3)
Re: pg_get_databasebyid(oid)

On Wed, Aug 28, 2019 at 6:05 PM Sergei Kornilov <sk@zsrv.org> wrote:

Please add that to commitfest.

Done: https://commitfest.postgresql.org/24/2261/

regards, Sergei

Hi,

I have checked the code, the function "pg_get_userbyid" is used in many
places in code. I am just curious why we need that "pg_get_databasebyid"
function. Is there a need for this function for the user?

--
Ibrar Ahmed

In reply to: Ibrar Ahmed (#4)
Re: pg_get_databasebyid(oid)

Hello

Is there a need for this function for the user?

This was feature request from user. I got such comment:

This function is useful when working with pg_stat_statements. For obtaining a databаse name for particular query you need to join pg_database relation, but for obtaining an username you just need pg_get_userbyid(). So it will be useful not to join extra relation and get a database name using the similar function - pg_get_databasebyid().

regards, Sergei

#6Ibrar Ahmed
ibrar.ahmad@gmail.com
In reply to: Sergei Kornilov (#5)
Re: pg_get_databasebyid(oid)

On Thu, Aug 29, 2019 at 3:16 PM Sergei Kornilov <sk@zsrv.org> wrote:

Hello

Is there a need for this function for the user?

This was feature request from user. I got such comment:

This function is useful when working with pg_stat_statements. For
obtaining a databаse name for particular query you need to join pg_database
relation, but for obtaining an username you just need pg_get_userbyid(). So
it will be useful not to join extra relation and get a database name using
the similar function - pg_get_databasebyid().

regards, Sergei

Hi,
I think its a user request and don't require to be in the core of
PostgreSQL.
A simple SQL function can fulfill the requirement of the user.

CREATE OR REPLACE FUNCTION pg_get_databasebyid(dboid integer) RETURNS name
AS $$

SELECT datname from pg_database WHERE oid = dboid;
$$ LANGUAGE SQL;

--
Ibrar Ahmed

#7Michael Paquier
michael@paquier.xyz
In reply to: Ibrar Ahmed (#6)
Re: pg_get_databasebyid(oid)

On Thu, Aug 29, 2019 at 03:47:40PM +0500, Ibrar Ahmed wrote:

I think its a user request and don't require to be in the core of
PostgreSQL.
A simple SQL function can fulfill the requirement of the user.

CREATE OR REPLACE FUNCTION pg_get_databasebyid(dboid integer) RETURNS name
AS $$

SELECT datname from pg_database WHERE oid = dboid;
$$ LANGUAGE SQL;

Indeed, I think that we can drop the patch. FWIW, I find the
semantics of pg_get_userbyid() horrible when it comes to return a
result for a non-existing user with its own way of defining how this
information should show up. Returning NULL would be more natural, so
I don't think that we should make more functions behave the same way.
--
Michael

In reply to: Michael Paquier (#7)
Re: pg_get_databasebyid(oid)

Hello

Thank you for attention! I marked CF entry as returned with feedback.

regards, Sergei