Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::StandardBufferManager Class Reference
Inheritance diagram for duckdb::StandardBufferManager:
Collaboration diagram for duckdb::StandardBufferManager:

Classes

struct  TemporaryFileData

Public Member Functions

 StandardBufferManager (DatabaseInstance &db, string temp_directory)
shared_ptr< BlockHandle > RegisterTransientMemory (const idx_t size, BlockManager &block_manager) final
 Registers a transient memory buffer.
shared_ptr< BlockHandle > RegisterSmallMemory (MemoryTag tag, const idx_t size) final
idx_t GetUsedMemory () const final
 Returns the currently allocated memory.
idx_t GetMaxMemory () const final
 Returns the maximum available memory.
idx_t GetUsedSwap () const final
 Returns the currently used swap space.
optional_idx GetMaxSwap () const final
 Returns the maximum swap space that can be used.
idx_t GetBlockAllocSize () const final
 Returns the block allocation size for buffer-managed blocks.
idx_t GetBlockSize () const final
 Returns the block size for buffer-managed blocks.
idx_t GetQueryMaxMemory () const final
 Returns the maximum available memory for a given query.
DUCKDB_API shared_ptr< BlockHandle > AllocateTemporaryMemory (MemoryTag tag, idx_t block_size, bool can_destroy=true) final
DUCKDB_API shared_ptr< BlockHandle > AllocateMemory (MemoryTag tag, BlockManager *block_manager, bool can_destroy=true) final
 Allocate block-based memory. The block manager provides the block size, and the block header size. Returns the BlockHandle managing the registered memory.
DUCKDB_API BufferHandle Allocate (MemoryTag tag, idx_t block_size, bool can_destroy=true) final
 Allocate (temporary) memory of size block_size, and pin it.
DUCKDB_API BufferHandle Allocate (MemoryTag tag, BlockManager *block_manager, bool can_destroy=true) final
 Allocate block-based memory and pin it.
BufferHandle Pin (shared_ptr< BlockHandle > &handle) final
 Pin a block handle.
BufferHandle Pin (const QueryContext &context, shared_ptr< BlockHandle > &handle) final
void Prefetch (vector< shared_ptr< BlockHandle > > &handles) final
 Pre-fetch a series of blocks. Using this function is a performance suggestion.
void Unpin (shared_ptr< BlockHandle > &handle) final
 Unpin a block handle.
void SetMemoryLimit (idx_t limit=(idx_t) -1) final
void SetSwapLimit (optional_idx limit=optional_idx()) final
 Set a new swap limit.
vector< MemoryInformation > GetMemoryUsageInfo () const override
 Returns information about memory usage.
BlockManager & GetTemporaryBlockManager () final
 Get the block manager used for in-memory data.
vector< TemporaryFileInformation > GetTemporaryFiles () final
 Returns a list of all temporary files.
const string & GetTemporaryDirectory () const final
 Get the path to the temporary file directory.
void SetTemporaryDirectory (const string &new_dir) final
 Set the path to the temporary file directory.
DUCKDB_API Allocator & GetBufferAllocator () final
 Get the buffer allocator.
const DatabaseInstance & GetDatabase () const override
 Get the const database.
DatabaseInstance & GetDatabase () override
 Get the database.
unique_ptr< FileBuffer > ConstructManagedBuffer (idx_t size, idx_t block_header_size, unique_ptr< FileBuffer > &&source, FileBufferType type=FileBufferType::MANAGED_BUFFER) override
 Construct a managed buffer.
DUCKDB_API void ReserveMemory (idx_t size) final
 Reserve memory.
DUCKDB_API void FreeReservedMemory (idx_t size) final
 Free reserved memory.
bool HasTemporaryDirectory () const final
 Returns true, if the path to the temporary file directory is not empty.
bool HasFilesInTemporaryDirectory () const final
 Returns true if there are files found in the temporary directory.
Public Member Functions inherited from duckdb::BufferManager
virtual shared_ptr< BlockHandle > RegisterSmallMemory (const idx_t size)
 Returns a newly registered block of memory that is smaller than the block size setting.

Static Public Member Functions

static unique_ptr< StandardBufferManager > CreateBufferManager (DatabaseInstance &db, string temp_directory)
Static Public Member Functions inherited from duckdb::BufferManager
static DUCKDB_API BufferManager & GetBufferManager (DatabaseInstance &db)
static DUCKDB_API const BufferManager & GetBufferManager (const DatabaseInstance &db)
static DUCKDB_API BufferManager & GetBufferManager (ClientContext &context)
static DUCKDB_API const BufferManager & GetBufferManager (const ClientContext &context)
static DUCKDB_API BufferManager & GetBufferManager (AttachedDatabase &db)
static DUCKDB_API idx_t GetAllocSize (const idx_t alloc_size)

Protected Member Functions

template<typename... ARGS>
TempBufferPoolReservation EvictBlocksOrThrow (MemoryTag tag, idx_t memory_delta, unique_ptr< FileBuffer > *buffer, ARGS...)
 Helper.
shared_ptr< BlockHandle > RegisterMemory (MemoryTag tag, idx_t block_size, idx_t block_header_size, bool can_destroy)
idx_t GetBlockAllocSize (idx_t block_size) const
 Get allocated size for a block.
void PurgeQueue (const BlockHandle &handle) final
 Garbage collect eviction queue.
BufferPool & GetBufferPool () const final
 Get the buffer pool.
TemporaryMemoryManager & GetTemporaryMemoryManager () final
 Get the manager assigning reservations for temporary memory, e.g., for query intermediates.
void WriteTemporaryBuffer (MemoryTag tag, block_id_t block_id, FileBuffer &buffer) final
 Write a temporary buffer to disk.
unique_ptr< FileBuffer > ReadTemporaryBuffer (QueryContext context, MemoryTag tag, BlockHandle &block, unique_ptr< FileBuffer > buffer=nullptr) final
 Read a temporary buffer from disk.
string GetTemporaryPath (block_id_t id)
 Get the path of the temporary buffer.
void DeleteTemporaryFile (BlockMemory &memory) final
 Delete the temporary file containing the block memory.
void RequireTemporaryDirectory ()
void AddToEvictionQueue (shared_ptr< BlockHandle > &handle) final
 Add the block handle to the eviction queue.
const char * InMemoryWarning ()
void VerifyZeroReaders (BlockLock &l, shared_ptr< BlockHandle > &handle)
void BatchRead (vector< shared_ptr< BlockHandle > > &handles, const map< block_id_t, idx_t > &load_map, block_id_t first_block, block_id_t last_block)
bool EncryptTemporaryFiles ()

Static Protected Member Functions

static data_ptr_t BufferAllocatorAllocate (PrivateAllocatorData *private_data, idx_t size)
static void BufferAllocatorFree (PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t size)
static data_ptr_t BufferAllocatorRealloc (PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t old_size, idx_t size)

Protected Attributes

DatabaseInstance & db
 The database instance.
BufferPool & buffer_pool
 The buffer pool.
TemporaryFileData temporary_directory
 The variables related to temporary file management.
atomic< block_id_t > temporary_id
 The temporary id used for managed buffers.
Allocator buffer_allocator
 Allocator associated with the buffer manager, that passes all allocations through this buffer manager.
unique_ptr< BlockManager > temp_block_manager
 Block manager for temp data.
atomic< idx_t > evicted_data_per_tag [MEMORY_TAG_COUNT]
 Temporary evicted memory data per tag.

Friends

class BufferHandle
class BlockHandle
class BlockManager

Detailed Description

The BufferManager is in charge of handling memory management for a single database. It cooperatively shares a BufferPool with other BufferManagers, belonging to different databases. It hands out memory buffers that can be used by the database internally, and offers configuration options specific to a database, which need not be shared by the BufferPool, including whether to support swapping temp buffers to disk, and where to swap them to.

Constructor & Destructor Documentation

◆ StandardBufferManager()

duckdb::StandardBufferManager::StandardBufferManager ( DatabaseInstance & db,
string temp_directory )
421787 : BufferManager(), db(db), buffer_pool(db.GetBufferPool()), temporary_id(MAXIMUM_BLOCK),
421788 buffer_allocator(BufferAllocatorAllocate, BufferAllocatorFree, BufferAllocatorRealloc,
421789 make_uniq<BufferAllocatorData>(*this)) {
421791 make_uniq<InMemoryBlockManager>(*this, DEFAULT_BLOCK_ALLOC_SIZE, DEFAULT_BLOCK_HEADER_STORAGE_SIZE);
421792 temporary_directory.path = std::move(tmp);
421793 for (idx_t i = 0; i < MEMORY_TAG_COUNT; i++) {
421794 evicted_data_per_tag[i] = 0;
421795 }
421796}
Allocator buffer_allocator
Allocator associated with the buffer manager, that passes all allocations through this buffer manager...
Definition duckdb.cpp:276448
BufferPool & buffer_pool
The buffer pool.
Definition duckdb.cpp:276442
DatabaseInstance & db
The database instance.
Definition duckdb.cpp:276440
TemporaryFileData temporary_directory
The variables related to temporary file management.
Definition duckdb.cpp:276444
atomic< block_id_t > temporary_id
The temporary id used for managed buffers.
Definition duckdb.cpp:276446
unique_ptr< BlockManager > temp_block_manager
Block manager for temp data.
Definition duckdb.cpp:276450
atomic< idx_t > evicted_data_per_tag[MEMORY_TAG_COUNT]
Temporary evicted memory data per tag.
Definition duckdb.cpp:276452
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ ~StandardBufferManager()

duckdb::StandardBufferManager::~StandardBufferManager ( )
override
421798 {
421799}

Member Function Documentation

◆ RegisterTransientMemory()

shared_ptr< BlockHandle > duckdb::StandardBufferManager::RegisterTransientMemory ( const idx_t size,
BlockManager & block_manager )
finalvirtual

Registers a transient memory buffer.

Reimplemented from duckdb::BufferManager.

421853 {
421854 D_ASSERT(size <= block_manager.GetBlockSize());
421855
421856 // This comparison is the reason behind passing block_size through transient memory creation.
421857 // Otherwise, any non-default block size would register as small memory, causing problems when
421858 // trying to convert that memory to consistent blocks later on.
421859 if (size < block_manager.GetBlockSize()) {
421860 return RegisterSmallMemory(MemoryTag::IN_MEMORY_TABLE, size);
421861 }
421862
421863 auto buffer_handle = Allocate(MemoryTag::IN_MEMORY_TABLE, &block_manager, false);
421864 return buffer_handle.GetBlockHandle();
421865}
shared_ptr< BlockHandle > RegisterSmallMemory(MemoryTag tag, const idx_t size) final
Definition duckdb.cpp:421867
DUCKDB_API BufferHandle Allocate(MemoryTag tag, idx_t block_size, bool can_destroy=true) final
Allocate (temporary) memory of size block_size, and pin it.
Definition duckdb.cpp:421922
Here is the call graph for this function:

◆ RegisterSmallMemory()

shared_ptr< BlockHandle > duckdb::StandardBufferManager::RegisterSmallMemory ( MemoryTag tag,
const idx_t size )
finalvirtual

Registers an in-memory buffer that cannot be unloaded until it is destroyed. This buffer can be small (smaller than the block size of the temporary block manager). Unpin and Pin are NOPs on this block of memory.

Reimplemented from duckdb::BufferManager.

421867 {
421868 D_ASSERT(size < GetBlockSize());
421869 auto reservation = EvictBlocksOrThrow(tag, size, nullptr, "could not allocate block of size %s%s",
421871
421872 auto buffer = ConstructManagedBuffer(size, DEFAULT_BLOCK_HEADER_STORAGE_SIZE, nullptr, FileBufferType::TINY_BUFFER);
421873
421874 // Create a new block pointer for this block.
421875 auto result = make_shared_ptr<BlockHandle>(*temp_block_manager, ++temporary_id, tag, std::move(buffer),
421876 DestroyBufferUpon::BLOCK, size, std::move(reservation));
421877#ifdef DUCKDB_DEBUG_DESTROY_BLOCKS
421878 // Initialize the memory with garbage data
421879 WriteGarbageIntoBuffer(*result);
421880#endif
421881 return result;
421882}
idx_t GetBlockSize() const final
Returns the block size for buffer-managed blocks.
Definition duckdb.cpp:421832
TempBufferPoolReservation EvictBlocksOrThrow(MemoryTag tag, idx_t memory_delta, unique_ptr< FileBuffer > *buffer, ARGS...)
Helper.
Definition duckdb.cpp:421841
unique_ptr< FileBuffer > ConstructManagedBuffer(idx_t size, idx_t block_header_size, unique_ptr< FileBuffer > &&source, FileBufferType type=FileBufferType::MANAGED_BUFFER) override
Construct a managed buffer.
Definition duckdb.cpp:421759
static DUCKDB_API string BytesToHumanReadableString(idx_t bytes, idx_t multiplier=1024)
Return a string that formats the give number of bytes.
Definition duckdb.cpp:109517
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetUsedMemory()

idx_t duckdb::StandardBufferManager::GetUsedMemory ( ) const
finalvirtual

Returns the currently allocated memory.

Implements duckdb::BufferManager.

421809 {
421810 return buffer_pool.GetUsedMemory();
421811}
Here is the caller graph for this function:

◆ GetMaxMemory()

idx_t duckdb::StandardBufferManager::GetMaxMemory ( ) const
finalvirtual

Returns the maximum available memory.

Implements duckdb::BufferManager.

421812 {
421813 return buffer_pool.GetMaxMemory();
421814}
Here is the caller graph for this function:

◆ GetUsedSwap()

idx_t duckdb::StandardBufferManager::GetUsedSwap ( ) const
finalvirtual

Returns the currently used swap space.

Implements duckdb::BufferManager.

421816 {
421817 return temporary_directory.size_on_disk.load(std::memory_order_relaxed);
421818}

◆ GetMaxSwap()

optional_idx duckdb::StandardBufferManager::GetMaxSwap ( ) const
finalvirtual

Returns the maximum swap space that can be used.

Implements duckdb::BufferManager.

421820 {
421821 lock_guard<mutex> guard(temporary_directory.lock);
421822 if (!temporary_directory.handle) {
421823 return optional_idx();
421824 }
421825 return temporary_directory.handle->GetTempFile().GetMaxSwapSpace();
421826}

◆ GetBlockAllocSize()

idx_t duckdb::StandardBufferManager::GetBlockAllocSize ( ) const
finalvirtual

Returns the block allocation size for buffer-managed blocks.

Implements duckdb::BufferManager.

421828 {
421829 return temp_block_manager->GetBlockAllocSize();
421830}
Here is the caller graph for this function:

◆ GetBlockSize()

idx_t duckdb::StandardBufferManager::GetBlockSize ( ) const
finalvirtual

Returns the block size for buffer-managed blocks.

Implements duckdb::BufferManager.

421832 {
421833 return temp_block_manager->GetBlockSize();
421834}
Here is the caller graph for this function:

◆ GetQueryMaxMemory()

idx_t duckdb::StandardBufferManager::GetQueryMaxMemory ( ) const
finalvirtual

Returns the maximum available memory for a given query.

Implements duckdb::BufferManager.

421836 {
421837 return GetBufferPool().GetQueryMaxMemory();
421838}
BufferPool & GetBufferPool() const final
Get the buffer pool.
Definition duckdb.cpp:421801
Here is the call graph for this function:

◆ AllocateTemporaryMemory()

shared_ptr< BlockHandle > duckdb::StandardBufferManager::AllocateTemporaryMemory ( MemoryTag tag,
idx_t block_size,
bool can_destroy = true )
finalvirtual

Allocate an in-memory buffer with a single pin. The allocated memory is released when the buffer handle is destroyed.

Implements duckdb::BufferManager.

421903 {
421904 return RegisterMemory(tag, block_size, Storage::DEFAULT_BLOCK_HEADER_SIZE, can_destroy);
421905}
shared_ptr< BlockHandle > RegisterMemory(MemoryTag tag, idx_t block_size, idx_t block_header_size, bool can_destroy)
Definition duckdb.cpp:421884
static constexpr idx_t DEFAULT_BLOCK_HEADER_SIZE
The default block header size for blocks written to storage.
Definition duckdb.hpp:9716
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AllocateMemory()

shared_ptr< BlockHandle > duckdb::StandardBufferManager::AllocateMemory ( MemoryTag tag,
BlockManager * block_manager,
bool can_destroy = true )
finalvirtual

Allocate block-based memory. The block manager provides the block size, and the block header size. Returns the BlockHandle managing the registered memory.

Implements duckdb::BufferManager.

421908 {
421909 return RegisterMemory(tag, block_manager->GetBlockSize(), block_manager->GetBlockHeaderSize(), can_destroy);
421910}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Allocate() [1/2]

BufferHandle duckdb::StandardBufferManager::Allocate ( MemoryTag tag,
idx_t block_size,
bool can_destroy = true )
finalvirtual

Allocate (temporary) memory of size block_size, and pin it.

Implements duckdb::BufferManager.

421922 {
421923 auto block = AllocateTemporaryMemory(tag, block_size, can_destroy);
421924
421925#ifdef DUCKDB_DEBUG_DESTROY_BLOCKS
421926 // Initialize the memory with garbage data
421927 WriteGarbageIntoBuffer(*block);
421928#endif
421929 return Pin(block);
421930}
BufferHandle Pin(shared_ptr< BlockHandle > &handle) final
Pin a block handle.
Definition duckdb.cpp:422024
DUCKDB_API shared_ptr< BlockHandle > AllocateTemporaryMemory(MemoryTag tag, idx_t block_size, bool can_destroy=true) final
Definition duckdb.cpp:421902
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Allocate() [2/2]

BufferHandle duckdb::StandardBufferManager::Allocate ( MemoryTag tag,
BlockManager * block_manager,
bool can_destroy = true )
finalvirtual

Allocate block-based memory and pin it.

Implements duckdb::BufferManager.

421912 {
421913 auto block = AllocateMemory(tag, block_manager, can_destroy);
421914
421915#ifdef DUCKDB_DEBUG_DESTROY_BLOCKS
421916 // Initialize the memory with garbage data
421917 WriteGarbageIntoBuffer(*block);
421918#endif
421919 return Pin(block);
421920}
DUCKDB_API shared_ptr< BlockHandle > AllocateMemory(MemoryTag tag, BlockManager *block_manager, bool can_destroy=true) final
Allocate block-based memory. The block manager provides the block size, and the block header size....
Definition duckdb.cpp:421907
Here is the call graph for this function:

◆ Pin() [1/2]

BufferHandle duckdb::StandardBufferManager::Pin ( shared_ptr< BlockHandle > & handle)
finalvirtual

Pin a block handle.

Implements duckdb::BufferManager.

422024 {
422025 return Pin(QueryContext(), handle);
422026}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Pin() [2/2]

BufferHandle duckdb::StandardBufferManager::Pin ( const QueryContext & context,
shared_ptr< BlockHandle > & handle )
finalvirtual

Implements duckdb::BufferManager.

422028 {
422029 // we need to be careful not to return the BufferHandle to this block while holding the BlockHandle's lock
422030 // as exiting this function's scope may cause the destructor of the BufferHandle to be called while holding the lock
422031 // the destructor calls Unpin, which grabs the BlockHandle's lock again, causing a deadlock
422032 BufferHandle buf;
422033
422034 idx_t required_memory;
422035 auto &block_memory = handle->GetMemory();
422036 {
422037 // lock the block
422038 auto lock = block_memory.GetLock();
422039 // check if the block is already loaded
422040 if (block_memory.GetState() == BlockState::BLOCK_LOADED) {
422041 // the block is loaded, increment the reader count and set the BufferHandle
422042 buf = handle->Load(context);
422043 }
422044 required_memory = block_memory.GetMemoryUsage();
422045 }
422046
422047 if (buf.IsValid()) {
422048 return buf; // the block was already loaded, return it without holding the BlockHandle's lock
422049 }
422050
422051 // evict blocks until we have space for the current block
422052 unique_ptr<FileBuffer> reusable_buffer;
422053 auto reservation =
422054 EvictBlocksOrThrow(block_memory.GetMemoryTag(), required_memory, &reusable_buffer,
422055 "failed to pin block of size %s%s", StringUtil::BytesToHumanReadableString(required_memory));
422056
422057 // lock the handle again and repeat the check (in case anybody loaded in the meantime)
422058 auto lock = block_memory.GetLock();
422059 // check if the block is already loaded
422060 if (block_memory.GetState() == BlockState::BLOCK_LOADED) {
422061 // the block is loaded, increment the reader count and return a pointer to the handle
422062 reservation.Resize(0);
422063 buf = handle->Load(context);
422064 } else {
422065 // now we can actually load the current block
422066 D_ASSERT(block_memory.GetReaders() == 0);
422067 buf = handle->Load(context, std::move(reusable_buffer));
422068 if (!buf.IsValid()) {
422069 reservation.Resize(0);
422070 return buf; // Buffer was destroyed (e.g., due to DestroyBufferUpon::Eviction)
422071 }
422072 auto &memory_charge = block_memory.GetMemoryCharge(lock);
422073 memory_charge = std::move(reservation);
422074 // in the case of a variable sized block, the buffer may be smaller than a full block.
422075 int64_t delta = NumericCast<int64_t>(block_memory.GetBuffer(lock)->AllocSize()) -
422076 NumericCast<int64_t>(block_memory.GetMemoryUsage());
422077 if (delta) {
422078 block_memory.ChangeMemoryUsage(lock, delta);
422079 }
422080 D_ASSERT(block_memory.GetMemoryUsage() == block_memory.GetBuffer(lock)->AllocSize());
422081 }
422082
422083 // we should have a valid BufferHandle by now, either because the block was already loaded, or because we loaded it
422084 // return it without holding the BlockHandle's lock
422085 D_ASSERT(buf.IsValid());
422086 return buf;
422087}
::int64_t int64_t

◆ Prefetch()

void duckdb::StandardBufferManager::Prefetch ( vector< shared_ptr< BlockHandle > > & handles)
finalvirtual

Pre-fetch a series of blocks. Using this function is a performance suggestion.

Implements duckdb::BufferManager.

421985 {
421986 // figure out which set of blocks we should load
421987 map<block_id_t, idx_t> to_be_loaded;
421988 for (idx_t block_idx = 0; block_idx < handles.size(); block_idx++) {
421989 auto &handle = handles[block_idx];
421990 if (handle->GetMemory().GetState() != BlockState::BLOCK_LOADED) {
421991 // need to load this block - add it to the map
421992 to_be_loaded.insert(make_pair(handle->BlockId(), block_idx));
421993 }
421994 }
421995 if (to_be_loaded.empty()) {
421996 // nothing to fetch
421997 return;
421998 }
421999 // iterate over the blocks and perform bulk reads
422000 block_id_t first_block = -1;
422001 block_id_t previous_block_id = -1;
422002 for (auto &entry : to_be_loaded) {
422003 if (previous_block_id < 0) {
422004 // this the first block we are seeing
422005 first_block = entry.first;
422006 previous_block_id = first_block;
422007 } else if (previous_block_id + 1 == entry.first) {
422008 // this block is adjacent to the previous block - add it to the batch read
422009 previous_block_id = entry.first;
422010 } else {
422011 // this block is not adjacent to the previous block
422012 // perform the batch read for the previous batch
422013 BatchRead(handles, to_be_loaded, first_block, previous_block_id);
422014
422015 // set the first_block and previous_block_id to the current block
422016 first_block = entry.first;
422017 previous_block_id = entry.first;
422018 }
422019 }
422020 // batch read the final batch
422021 BatchRead(handles, to_be_loaded, first_block, previous_block_id);
422022}

◆ Unpin()

void duckdb::StandardBufferManager::Unpin ( shared_ptr< BlockHandle > & handle)
finalvirtual

Unpin a block handle.

Implements duckdb::BufferManager.

422117 {
422118 bool purge = false;
422119 auto &block_memory = handle->GetMemory();
422120 {
422121 auto lock = block_memory.GetLock();
422122 if (!block_memory.GetBuffer(lock) || block_memory.GetBufferType() == FileBufferType::TINY_BUFFER) {
422123 return;
422124 }
422125 D_ASSERT(block_memory.GetReaders() > 0);
422126 auto new_readers = block_memory.DecrementReaders();
422127 if (new_readers == 0) {
422128 VerifyZeroReaders(lock, handle);
422129 if (block_memory.MustAddToEvictionQueue()) {
422130 purge = buffer_pool.AddToEvictionQueue(handle);
422131 } else {
422132 block_memory.Unload(lock);
422133 }
422134 }
422135 }
422136
422137 // We do not have to keep the handle locked while purging.
422138 if (purge) {
422139 PurgeQueue(*handle);
422140 }
422141}
void VerifyZeroReaders(BlockLock &l, shared_ptr< BlockHandle > &handle)
Definition duckdb.cpp:422097
void PurgeQueue(const BlockHandle &handle) final
Garbage collect eviction queue.
Definition duckdb.cpp:422089
Here is the call graph for this function:

◆ SetMemoryLimit()

void duckdb::StandardBufferManager::SetMemoryLimit ( idx_t limit = (idx_t)-1)
finalvirtual

Set a new memory limit to the buffer manager, throws an exception if the new limit is too low and not enough blocks can be evicted

Reimplemented from duckdb::BufferManager.

422143 {
422144 buffer_pool.SetLimit(limit, InMemoryWarning());
422145}

◆ SetSwapLimit()

void duckdb::StandardBufferManager::SetSwapLimit ( optional_idx limit = optional_idx())
finalvirtual

Set a new swap limit.

Reimplemented from duckdb::BufferManager.

422147 {
422148 lock_guard<mutex> guard(temporary_directory.lock);
422149 if (temporary_directory.handle) {
422150 temporary_directory.handle->GetTempFile().SetMaxSwapSpace(limit);
422151 } else {
422152 temporary_directory.maximum_swap_space = limit;
422153 }
422154}

◆ GetMemoryUsageInfo()

vector< MemoryInformation > duckdb::StandardBufferManager::GetMemoryUsageInfo ( ) const
overridevirtual

Returns information about memory usage.

Implements duckdb::BufferManager.

422156 {
422157 vector<MemoryInformation> result;
422158 for (idx_t k = 0; k < MEMORY_TAG_COUNT; k++) {
422159 MemoryInformation info;
422160 info.tag = MemoryTag(k);
422161 info.size = buffer_pool.memory_usage.GetUsedMemory(MemoryTag(k), BufferPool::MemoryUsageCaches::FLUSH);
422162 info.evicted_data = evicted_data_per_tag[k].load();
422163 result.push_back(info);
422164 }
422165 return result;
422166}

◆ GetTemporaryBlockManager()

BlockManager & duckdb::StandardBufferManager::GetTemporaryBlockManager ( )
finalvirtual

Get the block manager used for in-memory data.

Implements duckdb::BufferManager.

422354 {
422355 return *temp_block_manager;
422356}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTemporaryFiles()

vector< TemporaryFileInformation > duckdb::StandardBufferManager::GetTemporaryFiles ( )
finalvirtual

Returns a list of all temporary files.

Reimplemented from duckdb::BufferManager.

422358 {
422359 vector<TemporaryFileInformation> result;
422360 if (temporary_directory.path.empty()) {
422361 return result;
422362 }
422363 {
422364 lock_guard<mutex> temp_handle_guard(temporary_directory.lock);
422365 if (temporary_directory.handle) {
422366 result = temporary_directory.handle->GetTempFile().GetTemporaryFiles();
422367 }
422368 }
422369 auto &fs = FileSystem::GetFileSystem(db);
422370 fs.ListFiles(temporary_directory.path, [&](const string &name, bool is_dir) {
422371 if (is_dir) {
422372 return;
422373 }
422374 if (!StringUtil::EndsWith(name, ".block")) {
422375 return;
422376 }
422377
422378 // Another process or thread can delete the file before we can get its file size.
422379 auto handle = fs.OpenFile(name, FileFlags::FILE_FLAGS_READ | FileFlags::FILE_FLAGS_NULL_IF_NOT_EXISTS);
422380 if (!handle) {
422381 return;
422382 }
422383
422384 TemporaryFileInformation info;
422385 info.path = name;
422386 info.size = NumericCast<idx_t>(fs.GetFileSize(*handle));
422387 handle.reset();
422388 result.push_back(info);
422389 });
422390 return result;
422391}
static constexpr FileOpenFlags FILE_FLAGS_NULL_IF_NOT_EXISTS
Return NULL if the file does not exist instead of throwing an error.
Definition duckdb.hpp:7865
static constexpr FileOpenFlags FILE_FLAGS_READ
Open file with read access.
Definition duckdb.hpp:7850
static DUCKDB_API bool EndsWith(const string &str, const string &suffix)
Returns true if the target string ends with the given suffix.
Definition duckdb.cpp:109384
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTemporaryDirectory()

const string & duckdb::StandardBufferManager::GetTemporaryDirectory ( ) const
inlinefinalvirtual

Get the path to the temporary file directory.

Reimplemented from duckdb::BufferManager.

276342 {
276343 return temporary_directory.path;
276344 }

◆ SetTemporaryDirectory()

void duckdb::StandardBufferManager::SetTemporaryDirectory ( const string & new_dir)
finalvirtual

Set the path to the temporary file directory.

Reimplemented from duckdb::BufferManager.

421778 {
421779 lock_guard<mutex> guard(temporary_directory.lock);
421780 if (temporary_directory.handle) {
421781 throw NotImplementedException("Cannot switch temporary directory after the current one has been used");
421782 }
421783 temporary_directory.path = new_dir;
421784}

◆ GetBufferAllocator()

Allocator & duckdb::StandardBufferManager::GetBufferAllocator ( )
finalvirtual

Get the buffer allocator.

Reimplemented from duckdb::BufferManager.

422467 {
422468 return buffer_allocator;
422469}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDatabase() [1/2]

const DatabaseInstance & duckdb::StandardBufferManager::GetDatabase ( ) const
inlineoverridevirtual

Get the const database.

Implements duckdb::BufferManager.

276350 {
276351 return db;
276352 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDatabase() [2/2]

DatabaseInstance & duckdb::StandardBufferManager::GetDatabase ( )
inlineoverridevirtual

Get the database.

Implements duckdb::BufferManager.

276353 {
276354 return db;
276355 }

◆ ConstructManagedBuffer()

unique_ptr< FileBuffer > duckdb::StandardBufferManager::ConstructManagedBuffer ( idx_t size,
idx_t block_header_size,
unique_ptr< FileBuffer > && source,
FileBufferType type = FileBufferType::MANAGED_BUFFER )
overridevirtual

Construct a managed buffer.

Reimplemented from duckdb::BufferManager.

421761 {
421762 unique_ptr<FileBuffer> result;
421763 if (type == FileBufferType::BLOCK) {
421764 throw InternalException("ConstructManagedBuffer cannot be used to construct blocks");
421765 }
421766 if (source) {
421767 auto tmp = std::move(source);
421768 D_ASSERT(tmp->AllocSize() == BufferManager::GetAllocSize(size + block_header_size));
421769 result = make_uniq<FileBuffer>(*tmp, type, block_header_size);
421770 } else {
421771 // non re-usable buffer: allocate a new buffer
421772 result = make_uniq<FileBuffer>(BlockAllocator::Get(db), type, size, block_header_size);
421773 }
421774 result->Initialize(DBConfig::GetConfig(db).options.debug_initialize);
421775 return result;
421776}
Here is the caller graph for this function:

◆ ReserveMemory()

void duckdb::StandardBufferManager::ReserveMemory ( idx_t size)
finalvirtual

Reserve memory.

Reimplemented from duckdb::BufferManager.

422403 {
422404 if (size == 0) {
422405 return;
422406 }
422407 auto reservation =
422408 EvictBlocksOrThrow(MemoryTag::EXTENSION, size, nullptr, "failed to reserve memory data of size %s%s",
422410 reservation.size = 0;
422411}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FreeReservedMemory()

void duckdb::StandardBufferManager::FreeReservedMemory ( idx_t size)
finalvirtual

Free reserved memory.

Reimplemented from duckdb::BufferManager.

422413 {
422414 if (size == 0) {
422415 return;
422416 }
422417 buffer_pool.memory_usage.UpdateUsedMemory(MemoryTag::EXTENSION, -(int64_t)size);
422418}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HasTemporaryDirectory()

bool duckdb::StandardBufferManager::HasTemporaryDirectory ( ) const
finalvirtual

Returns true, if the path to the temporary file directory is not empty.

Reimplemented from duckdb::BufferManager.

422332 {
422333 return !temporary_directory.path.empty();
422334}

◆ HasFilesInTemporaryDirectory()

bool duckdb::StandardBufferManager::HasFilesInTemporaryDirectory ( ) const
finalvirtual

Returns true if there are files found in the temporary directory.

Reimplemented from duckdb::BufferManager.

422336 {
422337 if (temporary_directory.path.empty()) {
422338 return false;
422339 }
422340
422341 auto &fs = FileSystem::GetFileSystem(db);
422342 bool found = false;
422343 fs.ListFiles(temporary_directory.path, [&](const string &name, bool is_dir) {
422344 if (is_dir) {
422345 return;
422346 }
422347 if (StringUtil::EndsWith(name, ".block") || StringUtil::EndsWith(name, ".tmp")) {
422348 found = true;
422349 }
422350 });
422351 return found;
422352}
Here is the call graph for this function:

◆ EvictBlocksOrThrow()

template<typename... ARGS>
TempBufferPoolReservation duckdb::StandardBufferManager::EvictBlocksOrThrow ( MemoryTag tag,
idx_t memory_delta,
unique_ptr< FileBuffer > * buffer,
ARGS... args )
protected

Helper.

421842 {
421843 auto r = buffer_pool.EvictBlocks(tag, memory_delta, buffer_pool.maximum_memory, buffer);
421844 if (!r.success) {
421845 string extra_text = StringUtil::Format(" (%s/%s used)", StringUtil::BytesToHumanReadableString(GetUsedMemory()),
421847 extra_text += InMemoryWarning();
421848 throw OutOfMemoryException(args..., extra_text);
421849 }
421850 return std::move(r.reservation);
421851}
idx_t GetUsedMemory() const final
Returns the currently allocated memory.
Definition duckdb.cpp:421809
idx_t GetMaxMemory() const final
Returns the maximum available memory.
Definition duckdb.cpp:421812
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterMemory()

shared_ptr< BlockHandle > duckdb::StandardBufferManager::RegisterMemory ( MemoryTag tag,
idx_t block_size,
idx_t block_header_size,
bool can_destroy )
protected

Register an in-memory buffer of arbitrary size, as long as it is >= BLOCK_SIZE. can_destroy signifies whether or not the buffer can be destroyed instead of evicted, if true, it will be destroyed, if false, it will be written to a temporary file so it can be reloaded If we want to change this, e.g., to immediately destroy the buffer upon unpinning, we can call BlockHandle::SetDestroyBufferUpon The resulting buffer will already be allocated, but needs to be pinned in order to be used. This needs to be private to prevent creating blocks without ever pinning them: blocks that are never pinned are never added to the eviction queue

421885 {
421886 auto alloc_size = GetAllocSize(block_size + block_header_size);
421887
421888 // Evict blocks until there is enough memory to store the buffer.
421889 unique_ptr<FileBuffer> reusable_buffer;
421890 auto res = EvictBlocksOrThrow(tag, alloc_size, &reusable_buffer, "could not allocate block of size %s%s",
421892
421893 // Create a new buffer and a block to hold the buffer.
421894 const auto file_buffer_type =
421895 tag == MemoryTag::EXTERNAL_FILE_CACHE ? FileBufferType::EXTERNAL_FILE : FileBufferType::MANAGED_BUFFER;
421896 auto buffer = ConstructManagedBuffer(block_size, block_header_size, std::move(reusable_buffer), file_buffer_type);
421897 const auto destroy_buffer_upon = can_destroy ? DestroyBufferUpon::EVICTION : DestroyBufferUpon::BLOCK;
421898 return make_shared_ptr<BlockHandle>(*temp_block_manager, ++temporary_id, tag, std::move(buffer),
421899 destroy_buffer_upon, alloc_size, std::move(res));
421900}
@ EVICTION
Destroy the data buffer upon destroying the associated BlockHandle (block can be evicted).
Definition duckdb.hpp:45580
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PurgeQueue()

void duckdb::StandardBufferManager::PurgeQueue ( const BlockHandle & handle)
finalprotectedvirtual

Garbage collect eviction queue.

Implements duckdb::BufferManager.

422089 {
422090 buffer_pool.PurgeQueue(handle);
422091}
Here is the caller graph for this function:

◆ GetBufferPool()

BufferPool & duckdb::StandardBufferManager::GetBufferPool ( ) const
finalprotectedvirtual

Get the buffer pool.

Reimplemented from duckdb::BufferManager.

421801 {
421802 return buffer_pool;
421803}
Here is the caller graph for this function:

◆ GetTemporaryMemoryManager()

TemporaryMemoryManager & duckdb::StandardBufferManager::GetTemporaryMemoryManager ( )
finalprotectedvirtual

Get the manager assigning reservations for temporary memory, e.g., for query intermediates.

Reimplemented from duckdb::BufferManager.

421805 {
421806 return buffer_pool.GetTemporaryMemoryManager();
421807}
Here is the caller graph for this function:

◆ WriteTemporaryBuffer()

void duckdb::StandardBufferManager::WriteTemporaryBuffer ( MemoryTag tag,
block_id_t block_id,
FileBuffer & buffer )
finalprotectedvirtual

Write a temporary buffer to disk.

for very large buffers, we store the size of the buffer in plaintext.

Write the nonce (and tag for GCM).

Reimplemented from duckdb::BufferManager.

422191 {
422192 // WriteTemporaryBuffer assumes that we never write a buffer below DEFAULT_BLOCK_ALLOC_SIZE.
422193 RequireTemporaryDirectory();
422194
422195 // Append to a few grouped files.
422196 if (buffer.AllocSize() == GetBlockAllocSize()) {
422197 idx_t eviction_size = temporary_directory.handle->GetTempFile().WriteTemporaryBuffer(block_id, buffer);
422198 evicted_data_per_tag[uint8_t(tag)] += eviction_size;
422199 return;
422200 }
422201
422202 // Get the path to write to.
422203 // These files are .block, and variable sized (larger then 256kb), as opposed to .tmp
422204 auto path = GetTemporaryPath(block_id);
422205
422206 idx_t header_size = sizeof(idx_t) * 2;
422207 if (EncryptTemporaryFiles()) {
422208 header_size += DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE;
422209 }
422210
422211 evicted_data_per_tag[uint8_t(tag)] += buffer.AllocSize();
422212
422213 // Create the file and write the size followed by the buffer contents.
422214 auto &fs = FileSystem::GetFileSystem(db);
422215 auto handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE);
422216 temporary_directory.handle->GetTempFile().IncreaseSizeOnDisk(buffer.AllocSize() + sizeof(idx_t) * 2 + header_size);
422218 idx_t block_header_size = buffer.GetHeaderSize();
422219 handle->Write(QueryContext(), &buffer.size, sizeof(idx_t), 0);
422220 handle->Write(QueryContext(), &block_header_size, sizeof(idx_t), sizeof(idx_t));
422221
422222 idx_t offset = sizeof(idx_t) * 2;
422223
422224 if (EncryptTemporaryFiles()) {
422225 uint8_t encryption_metadata[DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE];
422226 EncryptionEngine::EncryptTemporaryBuffer(db, buffer.InternalBuffer(), buffer.AllocSize(), encryption_metadata);
422228 handle->Write(QueryContext(), encryption_metadata, DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE, offset);
422229 offset += DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE;
422230 }
422231
422232 buffer.Write(QueryContext(), *handle, offset);
422233}
static void EncryptTemporaryBuffer(DatabaseInstance &db, data_ptr_t buffer, idx_t buffer_size, data_ptr_t metadata)
Definition duckdb.cpp:52005
static constexpr FileOpenFlags FILE_FLAGS_FILE_CREATE
Create file if not exists, can only be used together with WRITE.
Definition duckdb.hpp:7856
static constexpr FileOpenFlags FILE_FLAGS_WRITE
Open file with write access.
Definition duckdb.hpp:7852
string GetTemporaryPath(block_id_t id)
Get the path of the temporary buffer.
Definition duckdb.cpp:422168
idx_t GetBlockAllocSize() const final
Returns the block allocation size for buffer-managed blocks.
Definition duckdb.cpp:421828
::uint8_t uint8_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadTemporaryBuffer()

unique_ptr< FileBuffer > duckdb::StandardBufferManager::ReadTemporaryBuffer ( QueryContext context,
MemoryTag tag,
BlockHandle & block,
unique_ptr< FileBuffer > buffer = nullptr )
finalprotectedvirtual

Read a temporary buffer from disk.

Read nonce and tag from file.

Read and decrypt the buffer.

Reimplemented from duckdb::BufferManager.

422237 {
422238 D_ASSERT(!temporary_directory.path.empty());
422239 auto id = block.BlockId();
422240 if (!temporary_directory.handle) {
422241 throw InternalException("ReadTemporaryBuffer called but temporary directory has not been instantiated yet");
422242 }
422243 if (temporary_directory.handle->GetTempFile().HasTemporaryBuffer(id)) {
422244 // This is a block that was offloaded to a regular .tmp file, the file contains blocks of a fixed size
422245
422246 auto buffer =
422247 temporary_directory.handle->GetTempFile().ReadTemporaryBuffer(context, id, std::move(reusable_buffer));
422248
422249 // Decrement evicted size.
422250 evicted_data_per_tag[uint8_t(tag)] -= buffer->AllocSize();
422251
422252 return buffer;
422253 }
422254
422255 // This block contains data of variable size so we need to open it and read it to get its size.
422256 idx_t block_size;
422257 idx_t block_header_size;
422258 auto path = GetTemporaryPath(id);
422259 auto &fs = FileSystem::GetFileSystem(db);
422260 auto handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_READ);
422261 handle->Read(context, &block_size, sizeof(idx_t), 0);
422262 handle->Read(context, &block_header_size, sizeof(idx_t), sizeof(idx_t));
422263
422264 idx_t offset = sizeof(idx_t) * 2;
422265
422266 // Allocate a buffer of the file's size and read the data into that buffer.
422267 auto buffer = ConstructManagedBuffer(block_size, block_header_size, std::move(reusable_buffer));
422268
422269 if (EncryptTemporaryFiles()) {
422270 // encrypted
422272 uint8_t encryption_metadata[DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE];
422273 handle->Read(context, encryption_metadata, DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE, offset);
422274
422276 buffer->Read(context, *handle, offset + DEFAULT_ENCRYPTED_BUFFER_HEADER_SIZE);
422277 EncryptionEngine::DecryptTemporaryBuffer(GetDatabase(), buffer->InternalBuffer(), buffer->AllocSize(),
422278 encryption_metadata);
422279 } else {
422280 // unencrypted: read the data directly
422281 buffer->Read(context, *handle, offset);
422282 }
422283
422284 handle.reset();
422285
422286 // Delete the file and return the buffer.
422287 DeleteTemporaryFile(block.GetMemory());
422288
422289 // Decrement evicted size.
422290 evicted_data_per_tag[uint8_t(tag)] -= buffer->AllocSize();
422291
422292 return buffer;
422293}
static void DecryptTemporaryBuffer(DatabaseInstance &db, data_ptr_t buffer, idx_t buffer_size, data_ptr_t metadata)
Definition duckdb.cpp:52069
void DeleteTemporaryFile(BlockMemory &memory) final
Delete the temporary file containing the block memory.
Definition duckdb.cpp:422295
const DatabaseInstance & GetDatabase() const override
Get the const database.
Definition duckdb.cpp:276350
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTemporaryPath()

string duckdb::StandardBufferManager::GetTemporaryPath ( block_id_t id)
protected

Get the path of the temporary buffer.

422168 {
422169 auto &fs = FileSystem::GetFileSystem(db);
422170 return fs.JoinPath(temporary_directory.path, "duckdb_temp_block-" + to_string(id) + ".block");
422171}
std::string to_string(const T &value)
Definition duckdb.cpp:19425
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeleteTemporaryFile()

void duckdb::StandardBufferManager::DeleteTemporaryFile ( BlockMemory & memory)
finalprotectedvirtual

Delete the temporary file containing the block memory.

Reimplemented from duckdb::BufferManager.

422295 {
422296 if (!memory.IsUnloaded()) {
422297 return;
422298 }
422299 auto id = memory.BlockId();
422300 if (temporary_directory.path.empty()) {
422301 // no temporary directory specified: nothing to delete
422302 return;
422303 }
422304 {
422305 lock_guard<mutex> guard(temporary_directory.lock);
422306 if (!temporary_directory.handle) {
422307 // temporary directory was not initialized yet: nothing to delete
422308 return;
422309 }
422310 }
422311
422312 // check if we should delete the file from the shared pool of files, or from the general file system
422313 if (temporary_directory.handle->GetTempFile().HasTemporaryBuffer(id)) {
422314 idx_t eviction_size = temporary_directory.handle->GetTempFile().DeleteTemporaryBuffer(id);
422315 evicted_data_per_tag[uint8_t(memory.GetMemoryTag())] -= eviction_size;
422316 return;
422317 }
422318
422319 // The file is not in the shared pool of files.
422320 auto &fs = FileSystem::GetFileSystem(db);
422321 auto path = GetTemporaryPath(id);
422322 if (fs.FileExists(path)) {
422323 evicted_data_per_tag[uint8_t(memory.GetMemoryTag())] -= memory.GetMemoryUsage();
422324 auto handle = fs.OpenFile(path, FileFlags::FILE_FLAGS_READ);
422325 auto content_size = handle->GetFileSize();
422326 handle.reset();
422327 fs.RemoveFile(path);
422328 temporary_directory.handle->GetTempFile().DecreaseSizeOnDisk(content_size);
422329 }
422330}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RequireTemporaryDirectory()

void duckdb::StandardBufferManager::RequireTemporaryDirectory ( )
protected
422173 {
422174 if (temporary_directory.path.empty()) {
422175 throw InvalidInputException(
422176 "Out-of-memory: cannot write buffer because no temporary directory is specified!\nTo enable "
422177 "temporary buffer eviction set a temporary directory using PRAGMA temp_directory='/path/to/tmp.tmp'");
422178 }
422179 lock_guard<mutex> guard(temporary_directory.lock);
422180 if (!temporary_directory.handle) {
422181 // temp directory has not been created yet: initialize it
422182 temporary_directory.handle = make_uniq<TemporaryDirectoryHandle>(
422183 db, temporary_directory.path, temporary_directory.size_on_disk, temporary_directory.maximum_swap_space);
422184 }
422185}

◆ AddToEvictionQueue()

void duckdb::StandardBufferManager::AddToEvictionQueue ( shared_ptr< BlockHandle > & handle)
finalprotectedvirtual

Add the block handle to the eviction queue.

Reimplemented from duckdb::BufferManager.

422093 {
422094 buffer_pool.AddToEvictionQueue(handle);
422095}
Here is the caller graph for this function:

◆ InMemoryWarning()

const char * duckdb::StandardBufferManager::InMemoryWarning ( )
protected
422393 {
422394 if (!temporary_directory.path.empty()) {
422395 return "";
422396 }
422397 return "\nDatabase is launched in in-memory mode and no temporary directory is specified."
422398 "\nUnused blocks cannot be offloaded to disk."
422399 "\n\nLaunch the database with a persistent storage back-end"
422400 "\nOr set SET temp_directory='/path/to/tmp.tmp'";
422401}

◆ BufferAllocatorAllocate()

data_ptr_t duckdb::StandardBufferManager::BufferAllocatorAllocate ( PrivateAllocatorData * private_data,
idx_t size )
staticprotected
422423 {
422424 auto &data = private_data->Cast<BufferAllocatorData>();
422425 auto reservation =
422426 data.manager.EvictBlocksOrThrow(MemoryTag::ALLOCATOR, size, nullptr, "failed to allocate data of size %s%s",
422428 // We rely on manual tracking of this one. :(
422429 reservation.size = 0;
422430 return Allocator::Get(data.manager.db).AllocateData(size);
422431}

◆ BufferAllocatorFree()

void duckdb::StandardBufferManager::BufferAllocatorFree ( PrivateAllocatorData * private_data,
data_ptr_t pointer,
idx_t size )
staticprotected
422433 {
422434 auto &data = private_data->Cast<BufferAllocatorData>();
422435 BufferPoolReservation r(MemoryTag::ALLOCATOR, data.manager.GetBufferPool());
422436 r.size = size;
422437 r.Resize(0);
422438 return Allocator::Get(data.manager.db).FreeData(pointer, size);
422439}
GOpaque< Size > size(const GMat &src)

◆ BufferAllocatorRealloc()

data_ptr_t duckdb::StandardBufferManager::BufferAllocatorRealloc ( PrivateAllocatorData * private_data,
data_ptr_t pointer,
idx_t old_size,
idx_t size )
staticprotected
422442 {
422443 if (old_size == size) {
422444 return pointer;
422445 }
422446 auto &data = private_data->Cast<BufferAllocatorData>();
422447 BufferPoolReservation r(MemoryTag::ALLOCATOR, data.manager.GetBufferPool());
422448 r.size = old_size;
422449 r.Resize(size);
422450 r.size = 0;
422451 return Allocator::Get(data.manager.db).ReallocateData(pointer, old_size, size);
422452}

◆ VerifyZeroReaders()

void duckdb::StandardBufferManager::VerifyZeroReaders ( BlockLock & l,
shared_ptr< BlockHandle > & handle )
protected

When the BlockHandle reaches 0 readers, this creates a new FileBuffer for this BlockHandle and overwrites the data within with garbage. Any readers that do not hold the pin will notice

422097 {
422098#ifdef DUCKDB_DEBUG_DESTROY_BLOCKS
422099 unique_ptr<FileBuffer> replacement_buffer;
422100 auto &block_allocator = BlockAllocator::Get(db);
422101 auto &buffer = handle->GetMemory().GetBuffer(lock);
422102 auto block_header_size = buffer->GetHeaderSize();
422103 auto alloc_size = buffer->AllocSize() - block_header_size;
422104 if (handle->GetMemory().GetBufferType() == FileBufferType::BLOCK) {
422105 auto block = reinterpret_cast<Block *>(buffer.get());
422106 replacement_buffer = make_uniq<Block>(block_allocator, block->id, alloc_size, block_header_size);
422107 } else {
422108 replacement_buffer =
422109 make_uniq<FileBuffer>(block_allocator, buffer->GetBufferType(), alloc_size, block_header_size);
422110 }
422111 memcpy(replacement_buffer->buffer, buffer->buffer, buffer->size);
422112 WriteGarbageIntoBuffer(lock, *handle);
422113 buffer = std::move(replacement_buffer);
422114#endif
422115}
Here is the caller graph for this function:

◆ BatchRead()

void duckdb::StandardBufferManager::BatchRead ( vector< shared_ptr< BlockHandle > > & handles,
const map< block_id_t, idx_t > & load_map,
block_id_t first_block,
block_id_t last_block )
protected
421933 {
421934 idx_t block_count = NumericCast<idx_t>(last_block - first_block + 1);
421935 if (block_count == 1) {
421936 if (Settings::Get<StorageBlockPrefetchSetting>(db) != StorageBlockPrefetch::DEBUG_FORCE_ALWAYS) {
421937 // prefetching with block_count == 1 has no performance impact since we can't batch reads
421938 // skip the prefetch in this case
421939 // we do it anyway if alternative_verify is on for extra testing
421940 return;
421941 }
421942 }
421943
421944 // Allocate a buffer to hold the data of all blocks.
421945 auto block_alloc_size = handles[0]->GetBlockAllocSize();
421946 auto total_block_size = block_count * block_alloc_size;
421947 auto batch_memory = RegisterMemory(MemoryTag::BASE_TABLE, total_block_size, 0, true);
421948 auto intermediate_buffer = Pin(batch_memory);
421949
421950 // perform a batch read of the blocks into the buffer
421951 auto &block_manager = handles[0]->GetBlockManager();
421952 block_manager.ReadBlocks(intermediate_buffer.GetFileBuffer(), first_block, block_count);
421953
421954 // the blocks are read - now we need to assign them to the individual blocks
421955 for (idx_t block_idx = 0; block_idx < block_count; block_idx++) {
421956 block_id_t block_id = first_block + NumericCast<block_id_t>(block_idx);
421957 auto entry = load_map.find(block_id);
421958 D_ASSERT(entry != load_map.end()); // if we allow gaps we might not return true here
421959 auto &handle = handles[entry->second];
421960
421961 // reserve memory for the block
421962 auto &block_memory = handle->GetMemory();
421963 idx_t required_memory = block_memory.GetMemoryUsage();
421964 unique_ptr<FileBuffer> reusable_buffer;
421965 auto reservation = EvictBlocksOrThrow(block_memory.GetMemoryTag(), required_memory, &reusable_buffer,
421966 "failed to pin block of size %s%s",
421967 StringUtil::BytesToHumanReadableString(required_memory));
421968 // now load the block from the buffer
421969 // note that we discard the buffer handle - we do not keep it around
421970 // the prefetching relies on the block handle being pinned again during the actual read before it is evicted
421971 BufferHandle buf;
421972 {
421973 auto lock = block_memory.GetLock();
421974 if (block_memory.GetState() == BlockState::BLOCK_LOADED) {
421975 // the block is loaded already by another thread - free up the reservation and continue
421976 reservation.Resize(0);
421977 continue;
421978 }
421979 auto block_ptr = intermediate_buffer.GetFileBuffer().InternalBuffer() + block_idx * block_alloc_size;
421980 buf = handle->LoadFromBuffer(lock, block_ptr, std::move(reusable_buffer), std::move(reservation));
421981 }
421982 }
421983}

◆ EncryptTemporaryFiles()

bool duckdb::StandardBufferManager::EncryptTemporaryFiles ( )
protected
422187 {
422188 return Settings::Get<TempFileEncryptionSetting>(db);
422189}

The documentation for this class was generated from the following file: