Skip to content

fix: current_database() returns the configured catalog name, not the literal "datafusion"#375

Merged
sunng87 merged 1 commit into
datafusion-contrib:masterfrom
muratdursun:fix/current-database-returns-catalog-name
Jul 16, 2026
Merged

fix: current_database() returns the configured catalog name, not the literal "datafusion"#375
sunng87 merged 1 commit into
datafusion-contrib:masterfrom
muratdursun:fix/current-database-returns-catalog-name

Conversation

@muratdursun

Copy link
Copy Markdown
Contributor

Problem

create_current_database_udf() registers a current_database() UDF that returns the hardcoded literal "datafusion", regardless of the catalog the session was actually set up with:

builder.append_value("datafusion"); // always

But setup_pg_catalog(session_context, catalog_name, ...) already knows the real catalog name — it registers pg_catalog under exactly that name a few lines above. So a client that connects to catalog mydb and runs SELECT current_database() gets "datafusion" instead of "mydb".

In PostgreSQL, current_database() reflects the database the client connected to and is fixed for the life of the connection. Tools that key off it — psql's \conninfo / prompt substitution, some ORMs and schema-migration tools that route by current_database(), pg_table_is_visible()-style visibility helpers — misbehave against the hardcoded value.

Fix

Thread the catalog name that setup_pg_catalog already has in scope into the UDF, and return it verbatim:

pub fn create_current_database_udf(database_name: &str) -> ScalarUDF {
    let database_name = database_name.to_string();
    let func = move |_args: &[ColumnarValue]| {
        let mut builder = StringBuilder::new();
        builder.append_value(&database_name);
        ...
    };
    ...
}

and at the call site:

session_context.register_udf(create_current_database_udf(catalog_name));

No new plumbing — catalog_name is already a parameter of setup_pg_catalog.

Compatibility note

create_current_database_udf is pub, so adding the &str parameter is a breaking signature change for anyone calling it directly (the in-repo call site is the only caller in this workspace, and it's updated here). If you'd prefer to preserve the old zero-arg signature, I'm happy to instead add a second create_current_database_udf_for(name: &str) and keep the old one as a thin wrapper — just let me know which you'd rather carry.

Test

Added a regression test in datafusion-pg-catalog's pg_catalog test module: set up the catalog under "my_database", run SELECT current_database(), and assert it returns "my_database" (not "datafusion").

Context

Found while building a multi-catalog pg-wire frontend on top of datafusion-pg-catalog, where each connection maps to a distinct catalog and current_database() needs to reflect it.

🤖 Generated with Claude Code

…literal "datafusion"

create_current_database_udf hardcoded "datafusion" regardless of the
catalog the session was set up with. setup_pg_catalog already has the
catalog name in scope; thread it through so SELECT current_database()
reflects the database the client connected to, per PostgreSQL semantics.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@sunng87 sunng87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muratdursun thank you!

@sunng87
sunng87 merged commit 5d3f714 into datafusion-contrib:master Jul 16, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants