Skip to content

Commit abf339d

Browse files
committed
libsql-sqlite3: Add libsql_wal_disable_checkpoint() API
SQLite unconditionally checkpoints when the last connection is closed to a database. Let's add a libSQL extension API to give callers control over that.
1 parent 7521bc0 commit abf339d

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

libsql-sqlite3/src/main.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,30 @@ void *libsql_close_hook(
24252425
return pRet;
24262426
}
24272427

2428+
/*
2429+
** Disable WAL checkpointing.
2430+
**
2431+
** Note: This function disables WAL checkpointing entirely, including when
2432+
** the last database connection is closed. This is different from
2433+
** sqlite3_wal_autocheckpoint() which only disables automatic checkpoints
2434+
** for the current connection, but still allows checkpointing when the
2435+
** connection is closed.
2436+
**/
2437+
int libsql_wal_disable_checkpoint(sqlite3 *db) {
2438+
#ifndef SQLITE_OMIT_WAL
2439+
#ifdef SQLITE_ENABLE_API_ARMOR
2440+
if( !sqlite3SafetyCheckOk(db) ){
2441+
return SQLITE_MISUSE_BKPT;
2442+
}
2443+
#endif
2444+
sqlite3_mutex_enter(db->mutex);
2445+
db->walCheckPointDisabled = 1;
2446+
db->xWalCallback = 0;
2447+
sqlite3_mutex_leave(db->mutex);
2448+
#endif
2449+
return SQLITE_OK;
2450+
}
2451+
24282452
/*
24292453
** Register a function to be invoked prior to each autovacuum that
24302454
** determines the number of pages to vacuum.
@@ -2522,6 +2546,7 @@ void *sqlite3_wal_hook(
25222546
#endif
25232547
sqlite3_mutex_enter(db->mutex);
25242548
pRet = db->pWalArg;
2549+
db->walCheckPointDisabled = 0;
25252550
db->xWalCallback = xCallback;
25262551
db->pWalArg = pArg;
25272552
sqlite3_mutex_leave(db->mutex);

libsql-sqlite3/src/sqlite.h.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10549,6 +10549,21 @@ int sqlite3_preupdate_blobwrite(sqlite3 *);
1054910549
*/
1055010550
void *libsql_close_hook(sqlite3 *db, void (*xClose)(void *pCtx, sqlite3 *db), void *arg);
1055110551

10552+
/*
10553+
** CAPI3REF: Disable WAL checkpointing
10554+
** METHOD: sqlite3
10555+
**
10556+
** ^The [libsql_wal_disable_checkpoint(D)] interface disables automatic
10557+
** checkpointing of the WAL file for [database connection] D.
10558+
**
10559+
** Note: This function disables WAL checkpointing entirely, including when
10560+
** the last database connection is closed. This is different from
10561+
** sqlite3_wal_autocheckpoint() which only disables automatic checkpoints
10562+
** for the current connection, but still allows checkpointing when the
10563+
** connection is closed.
10564+
*/
10565+
int libsql_wal_disable_checkpoint(sqlite3 *db);
10566+
1055210567
/*
1055310568
** CAPI3REF: Low-level system error code
1055410569
** METHOD: sqlite3

libsql-sqlite3/src/sqliteInt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,7 @@ struct sqlite3 {
17551755
PreUpdate *pPreUpdate; /* Context for active pre-update callback */
17561756
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
17571757
#ifndef SQLITE_OMIT_WAL
1758+
int walCheckPointDisabled;
17581759
int (*xWalCallback)(void *, sqlite3 *, const char *, int);
17591760
void *pWalArg;
17601761
#endif

libsql-sqlite3/src/wal.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,9 +2256,14 @@ static int sqlite3WalClose(
22562256
if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
22572257
pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
22582258
}
2259-
rc = sqlite3WalCheckpoint(pWal, db,
2260-
SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0, NULL, NULL
2261-
);
2259+
/* Don't checkpoint on close if automatic WAL checkpointing is disabled. */
2260+
if( !db->walCheckPointDisabled ){
2261+
rc = sqlite3WalCheckpoint(pWal, db,
2262+
SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0, NULL, NULL
2263+
);
2264+
} else {
2265+
rc = SQLITE_ERROR;
2266+
}
22622267
if( rc==SQLITE_OK ){
22632268
int bPersist = -1;
22642269
sqlite3OsFileControlHint(

0 commit comments

Comments
 (0)