389825 {
389826 auto &storage_manager =
db.GetStorageManager().Cast<SingleFileStorageManager>();
389827 if (storage_manager.InMemory()) {
389828 return;
389829 }
389830 if (ValidChecker::IsInvalidated(
db.GetDatabase())) {
389831
389832 return;
389833 }
389834
389836
389837 auto &block_manager = GetBlockManager();
389838 auto &metadata_manager = GetMetadataManager();
389839
389843
389844
389846
389847
389848
389849
389850
389851
389852
389853
389854
389855
389856 auto &transaction_manager =
db.GetTransactionManager().Cast<DuckTransactionManager>();
389857 ActiveCheckpointWrapper active_checkpoint(transaction_manager);
389858 auto has_wal = storage_manager.WALStartCheckpoint(meta_block,
options);
389859
389860 catalog_entry_vector_t catalog_entries;
389861 try {
389862 auto checkpoint_sleep_ms = Settings::Get<DebugCheckpointSleepMsSetting>(
db.GetDatabase());
389863 if (checkpoint_sleep_ms > 0) {
389864 ThreadUtil::SleepMs(checkpoint_sleep_ms);
389865 }
389866
389867 vector<reference<SchemaCatalogEntry>> schemas;
389868
389870 catalog.ScanSchemas([&](SchemaCatalogEntry &entry) { schemas.push_back(entry); });
389871
389872 D_ASSERT(catalog.IsDuckCatalog());
389873
389874 auto &dependency_manager = *catalog.GetDependencyManager();
389875 catalog_entries = GetCatalogEntries(schemas);
389876 dependency_manager.ReorderEntries(catalog_entries);
389877
389878
389879
389880
389881
389882
389883
389884
389885
389886
389887
389888
389889
389890
389891
389892
389893
389894
389895
389896
389897
389899 serializer.Begin();
389900 serializer.WriteList(100,
"catalog_entries", catalog_entries.size(), [&](Serializer::List &list,
idx_t i) {
389901 auto &entry = catalog_entries[i];
389902 list.WriteObject([&](Serializer &obj) { WriteEntry(entry.get(), obj); });
389903 });
389904 serializer.End();
389905
389908
389909 auto debug_checkpoint_abort = Settings::Get<DebugCheckpointAbortSetting>(
db.GetDatabase());
389910 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_HEADER) {
389911 throw FatalException("Checkpoint aborted before header write because of PRAGMA checkpoint_abort flag");
389912 }
389913
389914
389915 DatabaseHeader header;
389916 header.meta_block = meta_block.block_pointer;
389917 header.block_alloc_size = block_manager.GetBlockAllocSize();
389918 header.vector_size = STANDARD_VECTOR_SIZE;
389919 block_manager.WriteHeader(context, header);
389920
389921 auto debug_verify_blocks = Settings::Get<DebugVerifyBlocksSetting>(
db.GetDatabase());
389922 if (debug_verify_blocks) {
389923
389924 auto metadata_info = storage_manager.GetMetadataInfo();
389925 for (auto &info : metadata_info) {
389927 }
389928 for (auto &entry_ref : catalog_entries) {
389929 auto &entry = entry_ref.get();
389930 if (entry.type != CatalogType::TABLE_ENTRY) {
389931 continue;
389932 }
389933 auto &table = entry.Cast<DuckTableEntry>();
389934 auto &storage = table.GetStorage();
389935 auto segment_info = storage.GetColumnSegmentInfo(context);
389936 for (auto &segment : segment_info) {
389939 auto overflow_blocks =
389940 StringUtil::Replace(segment.segment_info, "Overflow String Block Ids: ", "");
389942 for (auto &split : splits) {
389943 auto overflow_block_id = std::stoll(split);
389945 }
389946 }
389947 }
389948 }
389950 }
389951
389952 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_TRUNCATE) {
389953 throw FatalException("Checkpoint aborted before truncate because of PRAGMA checkpoint_abort flag");
389954 }
389955
389956
389957 block_manager.Truncate();
389958
389959 if (debug_checkpoint_abort == CheckpointAbort::DEBUG_ABORT_BEFORE_WAL_FINISH) {
389960 throw FatalException("Checkpoint aborted before truncate because of PRAGMA checkpoint_abort flag");
389961 }
389962
389963
389964 if (has_wal) {
389965 unique_ptr<lock_guard<mutex>> owned_wal_lock;
389966 optional_ptr<lock_guard<mutex>> wal_lock;
389968
389969 owned_wal_lock = storage_manager.GetWALLock();
389970 wal_lock = *owned_wal_lock;
389971 } else {
389972
389973 wal_lock =
options.wal_lock;
389974 }
389975 storage_manager.WALFinishCheckpoint(*wal_lock);
389976 }
389977 } catch (std::exception &ex) {
389978
389979 ErrorData error(ex);
389980 if (error.Type() == ExceptionType::FATAL) {
389981 ValidChecker::Invalidate(
db.GetDatabase(), error.Message());
389982 throw;
389983 }
389984 throw FatalException("Failed to create checkpoint: %s", error.Message());
389985 }
389986
389987
389988
389989
389990
389991
389992
389993
389994 for (auto &entry_ref : catalog_entries) {
389995 auto &entry = entry_ref.get();
389996 if (entry.type != CatalogType::TABLE_ENTRY) {
389997 continue;
389998 }
389999 auto &table = entry.Cast<DuckTableEntry>();
390000 auto &storage = table.GetStorage();
390001 auto &table_info = storage.GetDataTableInfo();
390002 auto &index_list = table_info->GetIndexes();
390003 index_list.MergeCheckpointDeltas(
options.transaction_id);
390004 }
390005 active_checkpoint.Clear();
390006}
static DUCKDB_API Catalog & GetCatalog(ClientContext &context, const string &catalog_name)
Get the specified Catalog from the ClientContext.
Definition duckdb.cpp:7211
unordered_map< block_id_t, idx_t > verify_block_usage_count
Block usage count for verification purposes.
Definition duckdb.cpp:388988
unique_ptr< MetadataWriter > metadata_writer
The metadata writer is responsible for writing schema information.
Definition duckdb.cpp:388979
unique_ptr< MetadataWriter > table_metadata_writer
The table data writer is responsible for writing the DataPointers used by the table chunks.
Definition duckdb.cpp:388981
static DUCKDB_API bool Contains(const string &haystack, const string &needle)
Returns true if the needle string exists in the haystack.
Definition duckdb.cpp:109323
static DUCKDB_API vector< string > Split(const string &str, char delimiter)
Split the input string based on newline char.
Definition duckdb.cpp:109734
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237