Configure connection pragmas. * in_memory — :memory: DB (synchronous OFF, no journal file). * read_only — query-only connection opened SQLITE_OPEN_READONLY. Runs * ONLY non-writing pragmas (foreign_keys, temp_store, * busy_timeout, mmap_size) and SKIPS journal_mode=WAL, * wal_checkpoint and synchronous — those WRITE to the DB/WAL *
| 680 | * fail on a read-only DB file / filesystem. |
| 681 | * in_memory and read_only are mutually exclusive. */ |
| 682 | static int configure_pragmas(cbm_store_t *s, bool in_memory, bool read_only) { |
| 683 | int rc; |
| 684 | rc = exec_sql(s, "PRAGMA foreign_keys = ON;"); |
| 685 | if (rc != CBM_STORE_OK) { |
| 686 | return rc; |
| 687 | } |
| 688 | rc = exec_sql(s, "PRAGMA temp_store = MEMORY;"); |
| 689 | if (rc != CBM_STORE_OK) { |
| 690 | return rc; |
| 691 | } |
| 692 | |
| 693 | if (in_memory) { |
| 694 | rc = exec_sql(s, "PRAGMA synchronous = OFF;"); |
| 695 | } else if (read_only) { |
| 696 | /* Non-writing pragmas only — see the function comment. */ |
| 697 | rc = exec_sql(s, "PRAGMA busy_timeout = 10000;"); |
| 698 | if (rc != CBM_STORE_OK) { |
| 699 | return rc; |
| 700 | } |
| 701 | char mmap_sql[ST_BUF_64]; |
| 702 | snprintf(mmap_sql, sizeof(mmap_sql), "PRAGMA mmap_size = %lld;", |
| 703 | (long long)cbm_store_resolve_mmap_size()); |
| 704 | rc = exec_sql(s, mmap_sql); |
| 705 | } else { |
| 706 | rc = exec_sql(s, "PRAGMA busy_timeout = 10000;"); |
| 707 | if (rc != CBM_STORE_OK) { |
| 708 | return rc; |
| 709 | } |
| 710 | rc = exec_sql(s, "PRAGMA journal_mode = WAL;"); |
| 711 | if (rc != CBM_STORE_OK) { |
| 712 | return rc; |
| 713 | } |
| 714 | /* Recover stale WAL from previous crash (best-effort). |
| 715 | * PASSIVE never blocks readers and never ftruncates. |
| 716 | * May fail with SQLITE_BUSY if another process holds a lock. */ |
| 717 | (void)sqlite3_exec(s->db, "PRAGMA wal_checkpoint(PASSIVE)", NULL, NULL, NULL); |
| 718 | rc = exec_sql(s, "PRAGMA synchronous = NORMAL;"); |
| 719 | if (rc != CBM_STORE_OK) { |
| 720 | return rc; |
| 721 | } |
| 722 | /* #1083: bound the WAL file so a checkpoint-starved log is physically |
| 723 | * reclaimed the next time a checkpoint can reset it. Shared/live |
| 724 | * checkpoints are PASSIVE (they never ftruncate — see |
| 725 | * cbm_store_checkpoint's SIGBUS note), so without a size limit the -wal |
| 726 | * file only ever grows; journal_size_limit truncates it back to N bytes |
| 727 | * on the next successful |
| 728 | * reset. N is far above the healthy WAL (~64 MiB: the default |
| 729 | * 1000-page autocheckpoint at the 64 KiB page size sqlite_writer.c |
| 730 | * gives every index), so normal indexing never triggers |
| 731 | * truncate/regrow churn — it only fires after abnormal growth. |
| 732 | * Shared/live paths do NOT use a TRUNCATE checkpoint: truncating the WAL |
| 733 | * to zero can raise SIGBUS in a sibling process that has the DB mmap'd |
| 734 | * on macOS. Exclusive staging publication seals separately below. */ |
| 735 | rc = exec_sql(s, "PRAGMA journal_size_limit = 268435456;"); /* 256 MiB */ |
| 736 | if (rc != CBM_STORE_OK) { |
| 737 | return rc; |
| 738 | } |
| 739 | rc = exec_sql(s, ST_WRITE_CACHE_SQL); |
no test coverage detected