Skip to content

Adding public method to reset a prepared statement #1145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 23, 2022

Conversation

adamwulf
Copy link
Contributor

For context, the sqlite docs: https://www.sqlite.org/lang_transaction.html#implicit_versus_explicit_transactions

An implicit transaction (a transaction that is started automatically, not a transaction started by BEGIN) is committed automatically when the last active statement finishes. A statement finishes when its last cursor closes, which is guaranteed to happen when the prepared statement is reset or finalized. Some statements might "finish" for the purpose of transaction control prior to being reset or finalized, but there is no guarantee of this. The only way to ensure that a statement has "finished" is to invoke sqlite3_reset() or sqlite3_finalize() on that statement.

For clients that keep and reuse prepared statements, that statement prevents transactions from fully closing (even after a COMMIT). This, in turn, prevents a wal checkpoint (both explicitly requested checkpoint and auto checkpoints) from succeeding. Since reset is only currently called when starting a new query or in deinit, this means for any long-lived prepared statement will prevent a wal checkpoint, causing the wal file to grow unbounded.

Instead, allowing clients to explicitly reset their prepared statements allows them to control when the transaction closes, which allows wal checkpoints to succeed.

… that clients can reset any prepared statements they keep in memory, which allows for transactions to completely finish. without closing transactions (regardless of commit), the wal file cannot be checkpointed and will grow unbounded.
Copy link
Collaborator

@nathanfallet nathanfallet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Can you add one line somewhere in documentation to mention the existence of this method?

// create new db on disk in wal mode
let db = try Connection(.uri(path))
let url = URL(fileURLWithPath: db.description)
XCTAssertEqual(url.lastPathComponent, "SQLite.swift Tests.sqlite3")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary test

let path = "\(NSTemporaryDirectory())/SQLite.swift Tests.sqlite3"
try? FileManager.default.removeItem(atPath: path)
try? FileManager.default.removeItem(atPath: path + "-shm")
try? FileManager.default.removeItem(atPath: path + "-wal")
Copy link
Collaborator

@jberkel jberkel Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to create new and unique temporary file instead: see temporaryFile() in Fixtures

Copy link
Collaborator

@jberkel jberkel Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to remove these files since they are created each time

@adamwulf
Copy link
Contributor Author

Just pushed up changes to the test, as well as a documentation mention and context for its use.

XCTFail("Database should be locked")
} catch {
// pass
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XCTAssertThrowsError

let statement = try db.prepare("SELECT email FROM users")
XCTAssert(try statement.step())
let blob = statement.row[0] as Blob
XCTAssertEqual("[email protected]", String(bytes: blob.bytes, encoding: .utf8)!)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not relevant to the test

@adamwulf adamwulf force-pushed the feature/reset-statement branch from ac29da7 to 5a30f29 Compare July 22, 2022 22:45
@adamwulf
Copy link
Contributor Author

ok - new changes pushed up with the test cleanup

// create new db on disk in wal mode
let path = temporaryFile() + ".sqlite3"
let db = try Connection(.uri(path))
try db.run("PRAGMA journal_mode=WAL;")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test also passes with just

let db = try Connection(.inMemory)

looks like the exclusive lock is obtained even when WAL mode is not active

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah interesting, i wouldn't have expected a wal_checkpoint to affect a non-wal mode db. i'll remove that line then

Copy link
Collaborator

@jberkel jberkel Jul 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at this a bit more, I would change the test to not depend on any WAL specifics at all. Another way the db can end up in a locked state is when you have an open reader and then try to drop the table from the same connection:

let statement = try db.prepare("SELECT email FROM users")
_ = try statement.step()

// verify that the transaction is not closed, which prevents schema changes
XCTAssertThrowsError(try db.run("DROP TABLE users")) { error in
    if case let Result.error(_, code, _) = error {
        XCTAssertEqual(code, SQLITE_LOCKED)
    } else {
        XCTFail("unexpected error")
    }
}

// reset the prepared statement, allowing the implicit transaction to close
statement.reset()

// DROP table succeeds now
try db.run("DROP TABLE users")

The db should be opened in-memory. Additionally, can you simplify the db schema? Most of the fields defined are not relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea to use the drop table - i've just pushed up a commit that uses the .inMemory db and users table from the setup() method, and swaps to that drop table query

@jberkel jberkel merged commit c94aca2 into stephencelis:master Jul 23, 2022
@adamwulf adamwulf deleted the feature/reset-statement branch July 23, 2022 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants