Subject: [pgjdbc/pgjdbc] issue #3729: PgDatabaseMetaData.getColumns returns duplicate columnNames

From: "Chitrae92 (@Chitrae92)" <[email-masked]>
Date: Fri, 18 Jul 2025 01:31:15 +0000

Postgres jdbc version - **42.6.1**
Java version - 17

When we execute code to retrieve column names for a table with schema name having _ in them and there exists similar table in another schema, we currently get duplicate columns due to the like clause used in getColumns

Example:
**Schema Name:** test_ **Table Name:** tableA *Column Names* c1, c2
 **Schema Name:** test1 **Table Name:** tableA *Column Names* c1, c2

PgDatabaseMetaData.getColumns function ends up returning c1, c2, c1, c2 when getting the column Names for  test_.tableA since the LIKE clause satisfies for both the schema names. Is this expected by design? Should we not escape _ in schema or table names instead of treating them as wild characters.

    if (schemaPattern != null && !schemaPattern.isEmpty()) {
      sql += " AND n.nspname LIKE " + escapeQuotes(schemaPattern);
    }
    if (tableNamePattern != null && !tableNamePattern.isEmpty()) {
      sql += " AND c.relname LIKE " + escapeQuotes(tableNamePattern);
    }

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Fri, 18 Jul 2025 09:37:45 +0000

good call, yes, we should escape the _

Feel free to send in a PR

---

From: "vlsi (@vlsi)" <[email-masked]>
Date: Fri, 18 Jul 2025 10:06:18 +0000

pgjdbc should not escape `_` here.

`java.sql.DatabaseMetaData#getColumns` intentionally uses **patterns** for the parameter names, and it refers to `java.sql.DatabaseMetaData#getSearchStringEscape`.

So the resolution is that end-users should escape the pattern characters like `_` and `%` with a help of `getSearchStringEscape`.

See https://www.postgrespro.com/list/thread-id/1937525

---

From: "davecramer (@davecramer)" <[email-masked]>
Date: Fri, 18 Jul 2025 10:10:25 +0000

@vlsi Thanks for catching that.