| 274 | } |
| 275 | |
| 276 | void table_storage::load_table_metadata() |
| 277 | { |
| 278 | // Prevent recursion from SQL queries triggering hooks that call back here |
| 279 | static thread_local bool loading_in_progress = false; |
| 280 | if (loading_in_progress) { |
| 281 | return; |
| 282 | } |
| 283 | loading_in_progress = true; |
| 284 | struct guard { ~guard() { loading_in_progress = false; } } recursion_guard; |
| 285 | |
| 286 | const auto root_dir = []() { |
| 287 | auto root = session_credentials::get_root_path(); |
| 288 | if (root.empty()) { |
| 289 | root = pg::utils::get_deeplake_root_directory(); |
| 290 | } |
| 291 | return root; |
| 292 | }(); |
| 293 | auto creds = session_credentials::get_credentials(); |
| 294 | |
| 295 | // Stateless catalog sync (only when enabled) |
| 296 | if (pg::stateless_enabled) { |
| 297 | // Fast path: if already loaded, just check version without ensure_catalog |
| 298 | if (tables_loaded_) { |
| 299 | const auto current_version = pg::dl_catalog::get_catalog_version(root_dir, creds); |
| 300 | if (current_version == catalog_version_) { |
| 301 | return; |
| 302 | } |
| 303 | // Version changed, need to reload |
| 304 | tables_.clear(); |
| 305 | views_.clear(); |
| 306 | tables_loaded_ = false; |
| 307 | catalog_version_ = current_version; |
| 308 | } |
| 309 | |
| 310 | // Ensure catalog exists and get version in one call |
| 311 | const auto version = pg::dl_catalog::ensure_catalog(root_dir, creds); |
| 312 | if (catalog_version_ == 0) { |
| 313 | catalog_version_ = version; |
| 314 | } |
| 315 | tables_loaded_ = true; |
| 316 | |
| 317 | // Load tables and columns in parallel |
| 318 | auto [catalog_tables, catalog_columns] = pg::dl_catalog::load_tables_and_columns(root_dir, creds); |
| 319 | |
| 320 | if (!catalog_tables.empty()) { |
| 321 | for (const auto& meta : catalog_tables) { |
| 322 | const std::string qualified_name = meta.schema_name + "." + meta.table_name; |
| 323 | auto* rel = makeRangeVar(pstrdup(meta.schema_name.c_str()), pstrdup(meta.table_name.c_str()), -1); |
| 324 | Oid relid = RangeVarGetRelid(rel, NoLock, true); |
| 325 | if (!OidIsValid(relid)) { |
| 326 | // Table exists in catalog but not in PostgreSQL. |
| 327 | if (in_ddl_context()) { |
| 328 | // During DDL (CREATE TABLE), skip auto-creation to avoid races. |
| 329 | // The table might be in the middle of being created by another backend. |
| 330 | continue; |
| 331 | } |
| 332 | |
| 333 | // Gather columns for this table, sorted by position |
no test coverage detected