Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 131 additions & 18 deletions apple/Plugins/PostgreSQLDriverPlugin/LibPQCopyIn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import Foundation
import TableProPluginKit
import TableProPostgreSQLCore

/// Sends a `COPY ... FROM STDIN` its rows, after the statement answered `PGRES_COPY_IN`, the way psql 18 sends a
/// script's rows: the data as `CopyData`, never the `\.` line, then `CopyDone`. Measured with libpq 17.4 against
/// PostgreSQL 17.11 and 18.6 (`apple/scripts/probes/check-postgresql-copy-in.sh`).
/// Sends a `COPY ... FROM STDIN` its rows the way psql 18 sends a script's rows: the data as `CopyData`, never the
/// `\.` line, then `CopyDone`. Measured with libpq 17.4 against PostgreSQL 17.11 and 18.6
/// (`apple/scripts/probes/check-postgresql-copy-in.sh`).
///
/// The connection is nonblocking from the statement to its result and every wait is a slice, so a Stop is seen while
/// the server takes no data. A Stop ends the rows with `CopyFail`; when the server has not answered a slice later
/// (a row lock wait, or rows a proxy still holds), one cancel request follows from this thread, so it has reached the
/// server before the results are read and cannot stop the statement after the COPY.
nonisolated enum LibPQCopyIn {
struct Sent {
// The copy's own result, or nil when libpq had none to give. The caller clears it.
Expand All @@ -26,6 +31,29 @@ nonisolated enum LibPQCopyIn {
case connection
}

// Read on the connection's queue only, for one COPY.
final class Stop {
private let requested: () -> Bool
private let closing: () -> Bool
private let sendCancelRequest: () -> Void
private var sentCancelRequest = false

init(requested: @escaping () -> Bool, closing: @escaping () -> Bool, sendCancelRequest: @escaping () -> Void) {
self.requested = requested
self.closing = closing
self.sendCancelRequest = sendCancelRequest
}

var isRequested: Bool { requested() }
var isClosing: Bool { closing() }

func cancelServerOnce() {
guard !sentCancelRequest else { return }
sentCancelRequest = true
sendCancelRequest()
}
}

// psql's own CopyFail reasons, which the server echoes as "COPY from stdin failed: <reason>".
static let cancelledReason = "canceled by user"
static let sourceFailedReason = "aborted because of read failure"
Expand All @@ -37,41 +65,76 @@ nonisolated enum LibPQCopyIn {

// PQputCopyData takes an `int` length and grows its buffer to hold a whole message.
private static let sliceLength = 1 << 20
private static let waitMicroseconds: Int64 = 100_000

static func send(
_ rows: PluginCopyData,
format: LibPQCopyFormat,
conn: OpaquePointer,
isCancelled: () -> Bool
) -> Sent {
/// The statement's result as `PQexec` gives it: the last one, or the first that starts a copy. Nil when the
/// connection was lost. The connection stays nonblocking only after `PGRES_COPY_IN`, for `send`.
static func begin(_ statement: String, conn: OpaquePointer, stop: Stop) -> OpaquePointer? {
guard PQsetnonblocking(conn, 1) == 0, statement.withCString({ PQsendQuery(conn, $0) }) == 1 else {
_ = PQsetnonblocking(conn, 0)
return nil
}
var last: OpaquePointer?
// Nothing to retract while the statement starts, so a Stop sends the cancel request at once.
while awaitResult(conn, stop: stop, patience: 0) == .ready, let next = PQgetResult(conn) {
PQclear(last)
last = next
let status = PQresultStatus(next)
if status == PGRES_COPY_IN || status == PGRES_COPY_OUT || status == PGRES_COPY_BOTH
|| PQstatus(conn) == CONNECTION_BAD {
break
}
}
if last.map({ PQresultStatus($0) != PGRES_COPY_IN }) ?? true {
_ = PQsetnonblocking(conn, 0)
}
return last
}

/// After `begin` answered `PGRES_COPY_IN`. Returns with the connection blocking again.
static func send(_ rows: PluginCopyData, format: LibPQCopyFormat, conn: OpaquePointer, stop: Stop) -> Sent {
// A script holds text, and psql would send the rest of the file as the binary stream.
let interruption: Interruption? = format == .binary
? .binaryFormat
: sendPieces(rows, conn: conn, isCancelled: isCancelled)
let interruption: Interruption? = format == .binary ? .binaryFormat : sendPieces(rows, conn: conn, stop: stop)
let ended: Int32
let stopped: Bool
switch interruption {
case nil:
ended = PQputCopyEnd(conn, nil)
stopped = false
case .connection:
ended = -1
stopped = false
case .cancelled:
ended = cancelledReason.withCString { PQputCopyEnd(conn, $0) }
stopped = true
case .binaryFormat:
ended = binaryReason.withCString { PQputCopyEnd(conn, $0) }
stopped = false
case .source(let error):
let reason = isStop(error) ? cancelledReason : sourceFailedReason
ended = reason.withCString { PQputCopyEnd(conn, $0) }
stopped = isStop(error)
ended = (stopped ? cancelledReason : sourceFailedReason).withCString { PQputCopyEnd(conn, $0) }
}
// A CopyFail can still be answered on its own, so the cancel request waits a slice; after CopyDone it cannot.
let answered = ended == 1
&& awaitResult(conn, stop: stop, patience: interruption == nil ? 0 : 1, stopped: stopped) == .ready
_ = PQsetnonblocking(conn, 0)
guard answered else {
return Sent(result: nil, interruption: interruption ?? .connection)
}
let reported = ended == 1 ? interruption : (interruption ?? .connection)
return Sent(result: finalResult(conn), interruption: reported)
return Sent(result: finalResult(conn), interruption: interruption)
}

private static func sendPieces(_ rows: PluginCopyData, conn: OpaquePointer, isCancelled: () -> Bool) -> Interruption? {
private static func sendPieces(_ rows: PluginCopyData, conn: OpaquePointer, stop: Stop) -> Interruption? {
do {
while true {
if isCancelled() { return .cancelled }
if stop.isRequested { return .cancelled }
guard let piece = try rows.nextPiece() else { return nil }
guard put(piece, conn: conn) else { return .connection }
switch flush(conn, stop: stop) {
case .sent: continue
case .stopped: return .cancelled
case .lost: return .connection
}
}
} catch {
return .source(error)
Expand All @@ -83,6 +146,7 @@ nonisolated enum LibPQCopyIn {
error is PluginImportCancellationError || error is CancellationError
}

// Nonblocking, so each slice only queues it: `flush` sends it.
private static func put(_ piece: Data, conn: OpaquePointer) -> Bool {
piece.withUnsafeBytes { raw in
guard let base = raw.baseAddress?.assumingMemoryBound(to: CChar.self) else { return true }
Expand All @@ -96,6 +160,55 @@ nonisolated enum LibPQCopyIn {
}
}

private enum Flush {
case sent
case stopped
case lost
}

private static func flush(_ conn: OpaquePointer, stop: Stop) -> Flush {
while true {
let flushed = PQflush(conn)
guard flushed >= 0, !stop.isClosing, PQstatus(conn) != CONNECTION_BAD else { return .lost }
if flushed == 0 { return .sent }
if stop.isRequested { return .stopped }
guard wait(conn, forWrite: true) else { return .lost }
// The server's notices, so it never blocks writing to us while we wait to write to it.
_ = PQconsumeInput(conn)
}
}

private enum Answer {
case ready
case lost
}

// Until libpq holds a result and has sent everything queued. A proxy can take every byte while the server reads
// none, so this is the wait that tells such a server. `patience` is how many slices a Stop waits before its
// cancel request goes.
private static func awaitResult(_ conn: OpaquePointer, stop: Stop, patience: Int, stopped: Bool = false) -> Answer {
var unanswered = 0
while true {
let flushed = PQflush(conn)
guard flushed >= 0, !stop.isClosing else { return .lost }
if flushed == 0, PQisBusy(conn) == 0 { return .ready }
if stopped || stop.isRequested {
if unanswered >= patience { stop.cancelServerOnce() }
unanswered += 1
}
guard wait(conn, forWrite: flushed == 1) else { return .lost }
// A failed read leaves the connection bad, and PQgetResult then gives the reason.
if PQconsumeInput(conn) == 0, PQisBusy(conn) == 0 { return .ready }
}
}

private static func wait(_ conn: OpaquePointer, forWrite: Bool) -> Bool {
let socket = PQsocket(conn)
guard socket >= 0 else { return false }
let deadline = pg_usec_time_t(Int64(PQgetCurrentTimeUSec()) + waitMicroseconds)
return PQsocketPoll(socket, 1, forWrite ? 1 : 0, deadline) >= 0 || errno == EINTR
}

// While libpq still answers PGRES_COPY_IN the copy is not over: end it again, as psql does. The first other
// result is the copy's; the rest are drained so the connection takes the next statement.
private static func finalResult(_ conn: OpaquePointer) -> OpaquePointer? {
Expand Down
42 changes: 36 additions & 6 deletions apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ final class LibPQPluginConnection: @unchecked Sendable {
private var _lastTransactionState: LibPQTransactionState = .idle
private var _hasLostConnection = false
private var _interruptsBlockedCallOnDisconnect = false
// A copy-in sends its own cancel request (LibPQCopyIn), so none outlives it.
private var copyInRunning = false
private var serverMessages: Unmanaged<LibPQServerMessageSink>?
private var _standardConformingStrings = true
// Queue-confined. Non-nil until every schema/flavor/catalog bootstrap probe has completed.
Expand Down Expand Up @@ -711,7 +713,7 @@ final class LibPQPluginConnection: @unchecked Sendable {
// MARK: - Query Cancellation

func cancelCurrentQuery() {
guard cancellationGate.cancel() != nil else { return }
guard cancellationGate.cancel() != nil, !stateLock.withLock({ copyInRunning }) else { return }
requestServerCancelOnCurrentConnection()
}

Expand Down Expand Up @@ -1087,13 +1089,26 @@ final class LibPQPluginConnection: @unchecked Sendable {
rows: PluginCopyData,
conn: OpaquePointer
) throws -> LibPQPluginQueryResult {
// Set around the whole generation, so no Stop finds it running without the flag.
stateLock.withLock { copyInRunning = true }
let generation = cancellationGate.beginQuery()
defer { cancellationGate.endQuery(generation) }
defer {
cancellationGate.endQuery(generation)
stateLock.withLock { copyInRunning = false }
}
defer { refreshStandardConformingStrings(from: conn) }

// PQexec drained them itself; PQsendQuery refuses while any are left.
let cancelsOutput = cancelsAbandonedOutput(conn)
finishAbandonedResults(conn, cancellingOutput: cancelsOutput)
if let ended = sessionEndedBeforeSending(conn) { throw ended }
guard let start = statement.withCString({ PQexec(conn, $0) }) else {
if cancellationGate.isCancelled(generation) { throw CancellationError() }
let stop = LibPQCopyIn.Stop(
requested: { [cancellationGate] in cancellationGate.isCancelled(generation) },
closing: { [self] in isShuttingDown },
sendCancelRequest: { [self] in sendServerCancelAndWait(on: conn) }
)
guard let start = LibPQCopyIn.begin(statement, conn: conn, stop: stop) else {
throw lostConnection(getError(from: conn), on: conn, sent: true)
}
recordTransactionState(of: conn)
Expand All @@ -1108,9 +1123,7 @@ final class LibPQPluginConnection: @unchecked Sendable {
}
PQclear(start)

let sent = LibPQCopyIn.send(rows, format: copy.format, conn: conn) {
cancellationGate.isCancelled(generation)
}
let sent = LibPQCopyIn.send(rows, format: copy.format, conn: conn, stop: stop)
recordTransactionState(of: conn)
guard let result = sent.result else {
throw lostConnection(getError(from: conn), on: conn, sent: true)
Expand Down Expand Up @@ -1977,6 +1990,23 @@ extension LibPQPluginConnection {

// Outside the class body, which is at its length limit. An extension in this file still reaches its private members.
extension LibPQPluginConnection {
// The call waits for it, unlike the queued requests: PQcancel returns once the server has the request, so it
// cannot reach a statement the call sends later. PQcancel has no timeout of its own, so the wait ends at the
// connect timeout or a disconnect.
fileprivate func sendServerCancelAndWait(on connection: OpaquePointer) {
guard !suppressServerSideCancel, let cancelObject = PQgetCancel(connection) else { return }
let request = LibPQCancelRequest(cancelObject)
let delivered = DispatchSemaphore(value: 0)
Self.cancellationQueue.async {
request.perform()
delivered.signal()
}
let deadline = DispatchTime.now() + .milliseconds(connectTimeout.milliseconds)
while delivered.wait(timeout: .now() + .milliseconds(100)) == .timedOut {
guard !isShuttingDown, DispatchTime.now() < deadline else { return }
}
}

// `executeReadOnlyStatement` bounded at the source: the statement stops at `bound`, so no more than that is ever
// held. Stopping ends only the read-only transaction this opened. Unknown column types are looked up after its
// ROLLBACK, since a cancel can leave it aborted.
Expand Down
61 changes: 60 additions & 1 deletion apple/Plugins/PostgreSQLDriverTests/LibPQCopyInTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct LibPQCopyInTests {
defer { connection.disconnect() }

let pieces = Pieces(["1\tone\n", "2\ttwo\n", "3\tthree\n"]) { count in
if count == 1 { _ = connection.cancellationGate.cancel() }
if count == 1 { connection.cancelCurrentQuery() }
}
await #expect(throws: CancellationError.self) {
_ = try await connection.copyFromStandardInput(Self.statement, rows: pieces.data, readOnly: nil)
Expand All @@ -140,6 +140,65 @@ struct LibPQCopyInTests {
#expect(server.copyData == Data("1\tone\n".utf8))
#expect(server.copyFailMessages == ["canceled by user"])
#expect(pieces.piecesRead == 1)
_ = try await connection.executeQuery("SELECT 1")
#expect(server.cancelRequests == 0)
}

@Test("A Stop while the server takes no data ends the copy with one cancel request, and the session goes on")
func stopReachesAServerThatTakesNoData() async throws {
let server = try Self.server(.init(takesNoDataUntilCancelled: true))
defer { server.close() }
let connection = try await Self.connected(to: server)
defer { connection.disconnect() }

let pieces = Pieces(["1\tone\n", "2\ttwo\n"]) { count in
if count == 1 { connection.cancelCurrentQuery() }
}
await #expect(throws: CancellationError.self) {
_ = try await connection.copyFromStandardInput(Self.statement, rows: pieces.data, readOnly: nil)
}

#expect(server.cancelRequests == 1)
#expect(server.copyFailMessages == ["canceled by user"])
_ = try await connection.executeQuery("SELECT 1")
#expect(server.cancelRequests == 1)
}

@Test("A Stop while the COPY waits to start sends the cancel request at once and reads no piece")
func stopBeforeTheCopyStarts() async throws {
let stop = StopBox()
let server = try LibPQScriptedServer(
onQuery: { query in if query.hasPrefix("COPY") { stop.press() } },
answer: { query in query.hasPrefix("COPY") ? LibPQScriptedServer.Answer(waitsForCancel: true) : nil }
)
server.start()
defer { server.close() }
let connection = try await Self.connected(to: server)
defer { connection.disconnect() }
stop.connection = connection

let pieces = Pieces(["1\tone\n"])
await #expect(throws: CancellationError.self) {
_ = try await connection.copyFromStandardInput(Self.statement, rows: pieces.data, readOnly: nil)
}

#expect(pieces.piecesRead == 0)
#expect(server.cancelRequests == 1)
_ = try await connection.executeQuery("SELECT 1")
}

private final class StopBox: @unchecked Sendable {
private let lock = NSLock()
private weak var _connection: LibPQPluginConnection?

var connection: LibPQPluginConnection? {
get { lock.withLock { _connection } }
set { lock.withLock { _connection = newValue } }
}

func press() {
connection?.cancelCurrentQuery()
}
}

@Test("A binary COPY is refused with CopyFail before any piece is read")
Expand Down
18 changes: 17 additions & 1 deletion apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import Foundation
// completion, except that a statement holding `failingStatement` gets an error and the ones after it nothing. Cancel
// requests arrive on connections of their own: each is counted, and a statement waiting for one then fails as a
// cancelled statement does. A query answered with `copyIn` starts a copy-in: the server keeps every CopyData byte, and
// answers CopyDone with `COPY <line count>` (or the scripted failure) and CopyFail with 57014, as PostgreSQL does.
// answers CopyDone with `COPY <line count>` (or the scripted failure) and CopyFail with 57014, as PostgreSQL does, or
// takes no data until a cancel request arrives.
final class LibPQScriptedServer: @unchecked Sendable {
struct SetupFailure: Error {}

Expand Down Expand Up @@ -42,6 +43,9 @@ final class LibPQScriptedServer: @unchecked Sendable {
var binary = false
// Sent after CopyDone in place of the completion, as a row the server refuses is reported.
var failure: Failure?
// Reads nothing until a cancel request arrives, then fails with 57014 and passes over the rest of the copy,
// as a server waiting for a row lock does.
var takesNoDataUntilCancelled = false
}

static let idle = UInt8(ascii: "I")
Expand Down Expand Up @@ -243,6 +247,18 @@ final class LibPQScriptedServer: @unchecked Sendable {
guard send(client, Self.message("G", [format] + Self.bytes(UInt16(1)) + Self.bytes(UInt16(format)))) else {
return false
}
if copyIn.takesNoDataUntilCancelled {
guard cancelArrived.wait(timeout: .now() + Self.cancelWait) == .success else { return false }
failed = true
guard send(client, Self.errorResponse(Failure(
sqlState: "57014", message: "canceling statement due to user request"
))) else { return false }
while let type = receive(client, 1)?.first, let body = receiveBody(client) {
if type == UInt8(ascii: "f") { lock.withLock { copyFailures.append(Self.cString(body, from: 0).text) } }
if type == UInt8(ascii: "c") || type == UInt8(ascii: "f") { return true }
}
return false
}
var lines = 0
while let type = receive(client, 1)?.first, let body = receiveBody(client) {
switch type {
Expand Down
Loading
Loading