Repository navigation
feat(sidebar): list the session's temporary tables - #3746
Conversation
J2TeamNNL
left a comment
There was a problem hiding this comment.
Review findings, posted inline. The SQL Server schema-qualified #temp finding is untested.
| return nil | ||
| } | ||
| } | ||
| return DatabaseTreeTableRef(database: database, schema: nil, table: table) |
There was a problem hiding this comment.
A SQL Server temp table row has schema nil, so opening it fills in the browse schema (dbo) and the query becomes [dbo].[#scratch]. I believe SQL Server rejects a schema-qualified #temp name (not run). If so, clicking a Temporary row on MSSQL errors instead of showing rows. Tests only cover the listing SQL and a SQLite fixture.
| tabMgr.onTableOpened = { tableName, schemaName, databaseName, isView, objectType, isPreview in | ||
| // A temporary table goes with its session, and a Recent entry would outlive it. | ||
| let scope = DatabaseScope(connectionId: connectionId, database: databaseName, schema: schemaName) | ||
| guard !TemporaryTableStore.shared.holds(table: tableName, in: scope) else { return } |
There was a problem hiding this comment.
This skips Recent whenever a temp table with the same name exists, but never checks that the opened table is the temp one. If a temp orders hides the permanent orders, opening the permanent one (via FK, quick switcher) is never recorded in Recent. The callback has no isTemporary parameter.
| func fetchTemporaryTables() async throws -> [PluginTableInfo] { | ||
| guard listsTemporaryTables else { return [] } | ||
| var tables: [String: PluginTableInfo] = [:] | ||
| for candidate in temporaryTableCandidates { |
There was a problem hiding this comment.
One serial SHOW CREATE TABLE runs per candidate name, and names are never removed after DROP TEMPORARY TABLE. A script that creates and drops 500 temp tables leaves 500 candidates, and every table-affecting catalog change then queues 500 round trips on the user's session. Drop candidates on a seen DROP, or prune names answered with 1146.
| } | ||
|
|
||
| // A listing read on a connection that has since been replaced names tables that went with it. | ||
| private func isCurrent(_ listing: TemporaryTableListing, connectionId: UUID, database: String) -> Bool { |
There was a problem hiding this comment.
Session identity is an ObjectIdentifier of a driver, which can be reused once the old driver is freed. After a reconnect, a new driver at the same address makes a stale listing pass isCurrent, so dead temp tables stay in the sidebar and keep suppressing Recent. A generation counter or a weak driver reference avoids this.
| let database: String | ||
| let schema: String? | ||
| // Set for an object only the session can see, a temporary table: a pooled connection is another session. | ||
| let isSessionBound: Bool |
There was a problem hiding this comment.
isSessionBound is part of the synthesized Hashable identity, so every cache keyed by scope forks. A temp tab gets its own schema provider that does a full catalog populate, and checks like browseScope == scope and DataRefreshRequest.reaches needed hand patches. Keep routing separate from identity (a flag on the call, or a separate key).
| if reader.accept("IF") { | ||
| guard reader.accept("NOT"), reader.accept("EXISTS") else { return nil } | ||
| } | ||
| return reader.tableName() |
There was a problem hiding this comment.
created(by:) and tableName() drop the schema qualifier, and candidates are matched by bare name. RENAME TABLE other_db.t TO x is treated as renaming the temp t, and a temp t created under another database is reported as a candidate for the browsed one. The tracker should key on (schema, name).
| var columnContentWidths: [String: CGFloat]? | ||
| var windowGroupIndex: Int? | ||
| var isPinned: Bool = false | ||
| var isTemporary: Bool = false |
There was a problem hiding this comment.
isTemporary is persisted with the tab, but the temp table does not survive the session. After a restart or reconnect the tab restores with isTemporary, the old pg_temp_N schema on PostgreSQL, and auto-loads a query on a table that no longer exists. Drop restored temp tabs, or open them without auto-executing.
| guard viewModel != nil else { return [] } | ||
| let database = browsingDatabase | ||
| let search = SidebarSearch(searchText) | ||
| return temporaryTables.tables(connectionId: connectionId, database: database ?? "").compactMap { table in |
There was a problem hiding this comment.
The Temporary section lists only the browse database's temp tables. If the user creates one in database a, then browses b, the section disappears but the open tab keeps its session-bound scope for a. executionRoute returns .unavailable for the foreign database, so refresh, pagination and structure fail with the pooled connection error.
| let sql = MSSQLSchemaQueries.columns(schema: effectiveSchema(schema), table: table) | ||
| let sql = MSSQLSchemaQueries.namesTemporaryTable(table) | ||
| ? MSSQLSchemaQueries.temporaryColumns(table: table) | ||
| : MSSQLSchemaQueries.columns(schema: effectiveSchema(schema), table: table) |
There was a problem hiding this comment.
namesTemporaryTable is only hasPrefix("#"). A permanent table named #audit is read from tempdb..#audit and shows an empty structure. A global ##shared table is routed to tempdb but excluded from the sidebar listing (LIKE N'#[^#]%'), so the two paths disagree on what is temporary.
| // 12-digit suffix (measured on SQL Server 2022), so the name is the prefix `OBJECT_ID` resolves back to the row. | ||
| // Trailing underscores of the name itself are lost to the padding, so up to three are tried: every candidate is | ||
| // tried against every session's tables, and trying all 116 took 1.4 s with 300 temporary tables on the server. | ||
| public static let temporaryTables = """ |
There was a problem hiding this comment.
The listing recovers the name by trying up to 3 trailing underscores, so #stage____ (4 or more) is silently omitted. The CROSS APPLY also runs 1 to 4 OBJECT_ID lookups per foreign session's temp table on every catalog change, and the comment itself notes 1.4 s with 300 tables.
|
Fixes for the review findings are in J2TeamNNL@9ebac7d Fixed (5):
Skipped (5):
Not built or run locally (no Xcode). SwiftLint crashed here; changed files pass Cherry-pick: |
Summary
pg_my_temp_schema(), SQLite fromsqlite_temp_master, SQL Server fromtempdb.sys.tablesmatched back to the session withOBJECT_ID, MySQL and MariaDB from the namesCREATE TEMPORARY TABLEandRENAMEstatements gave, checked withSHOW CREATE TABLE.DISCARD TEMPandDISCARD ALL), and a list read on a connection that has since been replaced is hidden.TabTableContext.isTemporary, persisted) and a session-boundDatabaseScope, so its rows, columns and structure all come from the session. Its rows and structure are read-only, and it keeps no Recent entry, saved filters or hidden columns, which are keyed by a name a permanent table can share.PluginCapabilities.temporaryTablesandfetchTemporaryTables(), additive with a default. Kit stays 40.tempbeforemain; SQL Server columns and indexes fromtempdb; MySQL and MariaDB skipINFORMATION_SCHEMAandSHOW TABLE STATUSfor one, which describe the table it hides.Test plan
TemporaryTableStoreTests,ScopedDriverRoutingTests,SidebarObjectSelectionTests,DatabaseTreeMenuSpecTests,DatabaseTreeTemporarySelectionTests,DataRefreshScopeTests,TableTabDuplicationTests,TabObjectKindTests.SQLiteTemporaryTableTests(live in-process SQLite),MySQLTemporaryTableNamesTests,PostgreSQLTableListingTests,MSSQLTemporaryTableQueriesTests,SQLCatalogEffectClassifierTests.TemporaryTableSidebarUITests:CREATE TEMP TABLE scratch AS SELECT 'kept' AS notein a query tab listsscratchunder Temporary, and opening it shows its row.#tleft out,[dbo].[#t]reads, andINFORMATION_SCHEMA.COLUMNSanswering with the hidden table's default.Screenshots
CREATE TEMP TABLE scratch AS SELECT 'kept' AS note, 42 AS answeron SQLite, then a click on the new row. Before this change the sidebar lists nothing new.Not in this PR
#namewith more than three trailing underscores only by trying every padding length, which took 1.4 s with 300 temporary tables on the server, so such a name is not listed.DataGridCoordinator.columnLayoutKey).Fixes #2510