Properly done

This commit is contained in:
kssenii 2021-10-23 01:33:17 +03:00
parent 057fe51ec8
commit 1fa123ee5c
3 changed files with 38 additions and 9 deletions

View File

@ -90,14 +90,23 @@ bool DatabasePostgreSQL::empty() const
DatabaseTablesIteratorPtr DatabasePostgreSQL::getTablesIterator(ContextPtr local_context, const FilterByNameFunction & /* filter_by_table_name */) const
{
std::lock_guard<std::mutex> lock(mutex);
Tables tables;
auto connection_holder = pool->get();
auto table_names = fetchPostgreSQLTablesList(connection_holder->get(), configuration.schema);
for (const auto & table_name : table_names)
if (!detached_or_dropped.count(table_name))
tables[table_name] = fetchTable(table_name, local_context, true);
/// Do not allow to throw here, because this might be, for example, a query to system.tables.
/// It must not fail on case of some postgres error.
try
{
auto connection_holder = pool->get();
auto table_names = fetchPostgreSQLTablesList(connection_holder->get(), configuration.schema);
for (const auto & table_name : table_names)
if (!detached_or_dropped.count(table_name))
tables[table_name] = fetchTable(table_name, local_context, true);
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
return std::make_unique<DatabaseTablesSnapshotIterator>(tables, database_name);
}

View File

@ -191,7 +191,10 @@ std::shared_ptr<NamesAndTypesList> readNamesAndTypesList(
columns[i] = NameAndTypePair(name_and_type.name, type);
}
}
catch (const pqxx::syntax_error & e)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Error: {} (in query: {})", e.what(), query);
}
catch (const pqxx::undefined_table &)
{
throw Exception(ErrorCodes::UNKNOWN_TABLE, "PostgreSQL table {} does not exist", postgres_table);
@ -213,7 +216,9 @@ PostgreSQLTableStructure fetchPostgreSQLTableStructure(
PostgreSQLTableStructure table;
auto where = fmt::format("relname = {}", quoteString(postgres_table));
if (!postgres_schema.empty())
if (postgres_schema.empty())
where += " AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public')";
else
where += fmt::format(" AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = {})", quoteString(postgres_schema));
std::string query = fmt::format(
@ -225,6 +230,9 @@ PostgreSQLTableStructure fetchPostgreSQLTableStructure(
table.columns = readNamesAndTypesList(tx, postgres_table, query, use_nulls, false);
if (!table.columns)
throw Exception(ErrorCodes::UNKNOWN_TABLE, "PostgreSQL table {} does not exist", postgres_table);
if (with_primary_key)
{
/// wiki.postgresql.org/wiki/Retrieve_primary_key_columns

View File

@ -230,6 +230,8 @@ ASTPtr PostgreSQLReplicationHandler::getCreateNestedTableQuery(StorageMaterializ
postgres::Connection connection(connection_info);
pqxx::nontransaction tx(connection.getRef());
auto table_structure = std::make_unique<PostgreSQLTableStructure>(fetchPostgreSQLTableStructure(tx, table_name, postgres_schema, true, true, true));
if (!table_structure)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Failed to get PostgreSQL table structure");
return storage->getCreateNestedTableQuery(std::move(table_structure));
}
@ -649,7 +651,17 @@ PostgreSQLTableStructurePtr PostgreSQLReplicationHandler::fetchTableStructure(
if (!is_materialized_postgresql_database)
return nullptr;
return std::make_unique<PostgreSQLTableStructure>(fetchPostgreSQLTableStructure(tx, table_name, postgres_schema, true, true, true));
PostgreSQLTableStructure structure;
try
{
structure = fetchPostgreSQLTableStructure(tx, table_name, postgres_schema, true, true, true);
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
return std::make_unique<PostgreSQLTableStructure>(std::move(structure));
}