Merge pull request #39283 from kssenii/fixe-postgres-fetch-tables

Fix incorrect postgresql query which fetches tables list
This commit is contained in:
Kseniia Sumarokova 2022-07-18 12:00:08 +02:00 committed by GitHub
commit 5022eace6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 6 deletions

View File

@ -39,9 +39,10 @@ std::set<String> fetchPostgreSQLTablesList(T & tx, const String & postgres_schem
std::set<std::string> tables;
if (schemas.size() <= 1)
{
std::string query = fmt::format("SELECT tablename FROM pg_catalog.pg_tables "
"WHERE schemaname != 'pg_catalog' AND {}",
postgres_schema.empty() ? "schemaname != 'information_schema'" : "schemaname = " + quoteString(postgres_schema));
std::string query = fmt::format(
"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = {}",
postgres_schema.empty() ? quoteString("public") : quoteString(postgres_schema));
for (auto table_name : tx.template stream<std::string>(query))
tables.insert(std::get<0>(table_name));
@ -53,9 +54,10 @@ std::set<String> fetchPostgreSQLTablesList(T & tx, const String & postgres_schem
/// If we add schema to table name then table can be accessed only this way: database_name.`schema_name.table_name`
for (const auto & schema : schemas)
{
std::string query = fmt::format("SELECT tablename FROM pg_catalog.pg_tables "
"WHERE schemaname != 'pg_catalog' AND {}",
postgres_schema.empty() ? "schemaname != 'information_schema'" : "schemaname = " + quoteString(schema));
std::string query = fmt::format(
"SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = {}",
quoteString(schema));
for (auto table_name : tx.template stream<std::string>(query))
tables.insert(schema + '.' + std::get<0>(table_name));
}

View File

@ -349,6 +349,29 @@ def test_postgres_database_old_syntax(started_cluster):
node1.query("DROP DATABASE IF EXISTS postgres_database;")
def test_postgresql_fetch_tables(started_cluster):
conn = get_postgres_conn(
started_cluster.postgres_ip, started_cluster.postgres_port, database=True
)
cursor = conn.cursor()
cursor.execute("DROP SCHEMA IF EXISTS test_schema CASCADE")
cursor.execute("CREATE SCHEMA test_schema")
cursor.execute("CREATE TABLE test_schema.table1 (a integer)")
cursor.execute("CREATE TABLE test_schema.table2 (a integer)")
cursor.execute("CREATE TABLE table3 (a integer)")
node1.query(
"CREATE DATABASE postgres_database ENGINE = PostgreSQL('postgres1:5432', 'postgres_database', 'postgres', 'mysecretpassword')"
)
assert node1.query("SHOW TABLES FROM postgres_database") == "table3\n"
assert not node1.contains_in_log("PostgreSQL table table1 does not exist")
cursor.execute(f"DROP TABLE table3")
cursor.execute("DROP SCHEMA IF EXISTS test_schema CASCADE")
if __name__ == "__main__":
cluster.start()
input("Cluster created, press any key to destroy...")