Skip to content
2 changes: 2 additions & 0 deletions apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ internal final class BigQueryPluginDriver: PluginDatabaseDriver, @unchecked Send
)
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let datasetId = dataset(for: schema)
guard !datasetId.isEmpty else { return [:] }
Expand Down
2 changes: 2 additions & 0 deletions apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen
}
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let ks = resolveKeyspace(schema)
let query = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ nonisolated extension ClickHousePluginDriver {
"""
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
// Pre-fetch PK columns for all tables. Falls back to sorting_key when
// primary_key is empty (MergeTree without explicit PRIMARY KEY clause).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ nonisolated final class CloudflareD1PluginDriver: PluginDatabaseDriver, @uncheck
}
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let query = """
SELECT m.name AS tbl, p.cid, p.name, p.type, p."notnull", p.dflt_value, p.pk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ extension DamengPluginDriver {
}
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let owner = effectiveSchema(schema)
let result = try await executeParameterized(
Expand Down
2 changes: 2 additions & 0 deletions apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,8 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
}
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let schemaName = resolveSchema(schema)
let catalog = try requireCatalog()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ extension DynamoDBPluginDriver {
)
}

var fetchesAllColumnsInOneRequest: Bool { true }

/// Columns for autocomplete, from the tables already described, with no request at all. The
/// app asks for every table on connect, and answering by sampling each one read up to 300
/// tables in the background.
Expand Down
2 changes: 2 additions & 0 deletions apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
}
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let query = """
SELECT m.name AS tbl, p.cid, p.name, p.type, p."notnull", p.dflt_value, p.pk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ extension MSSQLPluginDriver {
return "DROP TRIGGER \(quoteIdentifier(resolved)).\(quoteIdentifier(name))"
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let result = try await execute(query: MSSQLSchemaQueries.allColumns(schema: effectiveSchema(schema)))
var columnsByTable: [String: [PluginColumnInfo]] = [:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
]
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
[MemcachedPlugin.keysTable: try await fetchColumns(table: MemcachedPlugin.keysTable, schema: schema)]
}
Expand Down
2 changes: 2 additions & 0 deletions apple/Plugins/OracleDriverPlugin/OraclePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable {
OracleObjectQueries.dropTrigger(name: name, schema: schema, currentSchema: _currentSchema)
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let result = try await rawQuery(
OracleSchemaQueries.allColumns(schema: effectiveSchema(schema), release: serverRelease())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ final class CockroachPluginDriver: LibPQBackedDriver, @unchecked Sendable {
return result.rows.compactMap { Self.mapColumnRow($0, includesTableName: false) }
}

var fetchesAllColumnsInOneRequest: Bool { true }

func fetchAllColumns(schema: String?) async throws -> [String: [PluginColumnInfo]] {
let resolvedSchema = schema ?? core.currentSchema
let query = CockroachRelationSQL.columnsQuery(schema: resolvedSchema, table: nil)
Expand Down
57 changes: 57 additions & 0 deletions apple/Plugins/PostgreSQLDriverPlugin/LibPQDriverCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,63 @@ final class LibPQDriverCore: @unchecked Sendable {
return try await executeTransactionScopedRead(path + statement)
}

// The server runs one Query's statements as one implicit transaction, so one `SET LOCAL` covers every read.
func executeCatalogReads(_ statements: [String]) async -> [Result<PluginQueryResult, Error>] {
guard pinsCatalogSearchPath, statements.count > 1, let pqConn = libpqConnection else {
return await catalogReadsOneByOne(statements)
}
let startTime = Date()
let answered: [Result<LibPQPluginQueryResult, Error>]
do {
let text = PostgreSQLCatalogSearchPath.pinned + statements.joined(separator: ";\n")
guard let batch = try await pqConn.executeConnectStatements(text) else {
return await catalogReadsOneByOne(statements)
}
// The first result is the pinned path's own.
answered = Array(batch.dropFirst())
} catch {
// A round trip that failed fails the connect in `finishConnecting`, so no read is sent again.
return statements.map { _ in .failure(error) }
}
let executionTime = Date().timeIntervalSince(startTime)
var reads: [Result<PluginQueryResult, Error>] = []
for (index, statement) in statements.enumerated() {
guard index < answered.count else {
reads.append(await catalogReadResult(statement))
continue
}
reads.append(answered[index].map { Self.pluginResult($0, executionTime: executionTime) })
}
return reads
}

private func catalogReadsOneByOne(_ statements: [String]) async -> [Result<PluginQueryResult, Error>] {
var reads: [Result<PluginQueryResult, Error>] = []
for statement in statements {
reads.append(await catalogReadResult(statement))
}
return reads
}

private func catalogReadResult(_ statement: String) async -> Result<PluginQueryResult, Error> {
do {
return .success(try await executeCatalogRead(statement))
} catch {
return .failure(error)
}
}

private static func pluginResult(_ result: LibPQPluginQueryResult, executionTime: TimeInterval) -> PluginQueryResult {
PluginQueryResult(
columns: result.columns,
columnTypeNames: result.columnTypeNames,
rows: result.rows,
rowsAffected: result.affectedRows,
executionTime: executionTime,
isTruncated: result.isTruncated
)
}

private func executeTransactionScopedRead(_ statement: String) async throws -> PluginQueryResult {
let pqConn = try connection()
let startTime = Date()
Expand Down
100 changes: 81 additions & 19 deletions apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,14 @@ final class LibPQPluginConnection: @unchecked Sendable {
deadline: LibPQConnectDeadline
) throws {
logUnexpectedClientEncoding(of: connection)
try runSessionSetupStatement(
LibPQStringConformance.enableStatement,
on: connection,
deadline: deadline
)
// Every server since 8.2 reports the setting and PgBouncer tracks it, so a reported `on` needs no round trip.
if reportedStandardConformingStrings(on: connection) != true {
try runSessionSetupStatement(
LibPQStringConformance.enableStatement,
on: connection,
deadline: deadline
)
}
let standardConformingStrings = try (
reportedStandardConformingStrings(on: connection)
?? queriedStandardConformingStrings(on: connection, deadline: deadline)
Expand Down Expand Up @@ -476,21 +479,37 @@ final class LibPQPluginConnection: @unchecked Sendable {
on connection: OpaquePointer,
deadline: LibPQConnectDeadline
) throws -> OpaquePointer {
// One element: only the last result is kept, and no result at all throws.
try bootstrapResults(for: statement, on: connection, deadline: deadline, keepsOnlyLast: true)[0]
}

private func bootstrapResults(
for statements: String,
on connection: OpaquePointer,
deadline: LibPQConnectDeadline,
keepsOnlyLast: Bool
) throws -> [OpaquePointer] {
do {
return try uncheckedBootstrapResult(for: statement, on: connection, deadline: deadline)
return try uncheckedBootstrapResults(
for: statements,
on: connection,
deadline: deadline,
keepsOnlyLast: keepsOnlyLast
)
} catch {
bootstrapTransportFailed = true
throw error
}
}

private func uncheckedBootstrapResult(
for statement: String,
private func uncheckedBootstrapResults(
for statements: String,
on connection: OpaquePointer,
deadline: LibPQConnectDeadline
) throws -> OpaquePointer {
deadline: LibPQConnectDeadline,
keepsOnlyLast: Bool
) throws -> [OpaquePointer] {
try checkBootstrapState(connection, deadline: deadline)
let sent = statement.withCString { PQsendQuery(connection, $0) }
let sent = statements.withCString { PQsendQuery(connection, $0) }
guard sent != 0 else { throw connectionError(from: connection) }

while true {
Expand All @@ -501,10 +520,10 @@ final class LibPQPluginConnection: @unchecked Sendable {
try pollBootstrapSocket(connection, reads: false, writes: true, deadline: deadline)
}

var finalResult: OpaquePointer?
var returnsFinalResult = false
var results: [OpaquePointer] = []
var returnsResults = false
defer {
if !returnsFinalResult, let finalResult { PQclear(finalResult) }
if !returnsResults { results.forEach { PQclear($0) } }
}
while true {
while PQisBusy(connection) != 0 {
Expand All @@ -515,13 +534,13 @@ final class LibPQPluginConnection: @unchecked Sendable {
}

guard let result = PQgetResult(connection) else { break }
if let finalResult { PQclear(finalResult) }
finalResult = result
if keepsOnlyLast, let earlier = results.popLast() { PQclear(earlier) }
results.append(result)
}

guard let finalResult else { throw connectionError(from: connection) }
returnsFinalResult = true
return finalResult
guard !results.isEmpty else { throw connectionError(from: connection) }
returnsResults = true
return results
}

private func pollBootstrapSocket(
Expand Down Expand Up @@ -757,6 +776,17 @@ final class LibPQPluginConnection: @unchecked Sendable {
}
}

// Nil once the connect has finished, when a block of the user's may be open: the caller then scopes each read alone.
func executeConnectStatements(_ statements: String) async throws -> [Result<LibPQPluginQueryResult, Error>]? {
let statementsToRun = String(statements)

return try await pluginDispatchAsync(on: queue) { [self] () throws -> [Result<LibPQPluginQueryResult, Error>]? in
guard !isShuttingDown else { throw LibPQPluginError.notConnected }
guard let deadline = activeConnectDeadline else { return nil }
return try executeConnectStatementsSync(statementsToRun, deadline: deadline)
}
}

// A catalog query the driver sends on its own while reading a result, on the connection's queue, scoped the way
// `executeTransactionScopedRead` scopes one. The caller clears the result. Inside an aborted block it hands back
// the savepoint's own `25P02` failure.
Expand Down Expand Up @@ -962,6 +992,38 @@ final class LibPQPluginConnection: @unchecked Sendable {
return decoded
}

// One simple Query: the server answers each statement in order and runs none after the first that fails.
private func executeConnectStatementsSync(
_ statements: String,
deadline: LibPQConnectDeadline
) throws -> [Result<LibPQPluginQueryResult, Error>] {
guard !isShuttingDown, let connection = connectionHandle else {
throw LibPQPluginError.notConnected
}

let generation = cancellationGate.beginQuery()
defer { cancellationGate.endQuery(generation) }
defer { refreshStandardConformingStrings(from: connection) }

let cancelsOutput = cancelsAbandonedOutput(connection)
if let ended = sessionEndedBeforeSending(connection) { throw ended }
let results = try bootstrapResults(for: statements, on: connection, deadline: deadline, keepsOnlyLast: false)
recordTransactionState(of: connection)
let decoded = results.map { result in
Result<LibPQPluginQueryResult, Error> {
try queryResult(
from: result,
connection: connection,
generation: generation,
cancelsOutput: cancelsOutput,
allowsSideQueries: false
)
}
}
try checkBootstrapState(connection, deadline: deadline)
return decoded
}

private func executeQuerySync(_ query: String, singleStatement: Bool = false) throws -> LibPQPluginQueryResult {
let conn = connectionHandle

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ private let enumProbeLogger = Logger(
)

extension PostgreSQLPluginDriver {
func probeEnumOids() async {
func applyEnumProbe(_ read: Result<PluginQueryResult, Error>) {
do {
let result = try await core.executeCatalogRead(PostgreSQLSchemaQueries.enumTypeOidQuery)
let result = try read.get()
let rows = result.rows.map { row in row.map(\.asText) }
core.mergeCatalogTypeNames(PostgreSQLCatalogTypeNames.enumProbeNames(rows: rows))
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ internal extension PostgreSQLPluginDriver {
PostgreSQLVersionedStatements.refusal(for: operation, capabilities: versionedCapabilities)
}

func probeSessionFacts() async {
func applySessionFactsProbe(_ read: Result<PluginQueryResult, Error>) {
do {
let result = try await core.executeCatalogRead(PostgreSQLSessionFacts.probeQuery)
let result = try read.get()
let row = result.rows.first?.map(\.asText) ?? []
let facts = PostgreSQLSessionFacts(probeRow: row)
sessionFacts.withLock { $0 = facts }
Expand Down
Loading
Loading