Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions columnar/src/backend/columnar/columnar_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,12 @@ CreateEmptyChunkData(uint32 columnCount, bool *columnMask, uint32 chunkGroupRowC
ChunkData *chunkData = palloc0(sizeof(ChunkData));
chunkData->existsArray = palloc0(columnCount * sizeof(bool *));
chunkData->valueArray = palloc0(columnCount * sizeof(Datum *));
#if PG_VERSION_NUM >= PG_VERSION_16
/* 'cache' member will be 'false' because of 'palloc0' */
chunkData->valueBufferArray = palloc0(columnCount * sizeof(cached_StringInfo));
#else
chunkData->valueBufferArray = palloc0(columnCount * sizeof(StringInfo));
#endif
chunkData->columnCount = columnCount;
chunkData->rowCount = chunkGroupRowCount;

Expand Down Expand Up @@ -1195,31 +1200,17 @@ FreeChunkData(ChunkData *chunkData)
pfree(chunkData);
}

#if PG_VERSION_NUM >= PG_VERSION_16
/* Copied from postgres 15 source, since it was removed from 16. */
static bool
MemoryContextContains(MemoryContext context, void *pointer)
#if PG_VERSION_NUM < PG_VERSION_16
static inline bool
CachedStringInfo(StringInfo *info)
{
return MemoryContextContains(ColumnarCacheMemoryContext(), (void *)info);
}
#else
static inline bool
CachedStringInfo(StringInfo info)
{
MemoryContext ptr_context;

/*
* NB: Can't use GetMemoryChunkContext() here - that performs assertions
* that aren't acceptable here since we might be passed memory not
* allocated by any memory context.
*
* Try to detect bogus pointers handed to us, poorly though we can.
* Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
* allocated chunk.
*/
if (pointer == NULL || pointer != (void *) MAXALIGN(pointer))
return false;

/*
* OK, it's probably safe to look at the context.
*/
ptr_context = *(MemoryContext *) (((char *) pointer) - sizeof(void *));

return ptr_context == context;
return ((cached_StringInfo *)info)->cached;
}
#endif

Expand All @@ -1235,7 +1226,7 @@ void FreeChunkBufferValueArray(ChunkData *chunkData)

for (columnIndex = 0; columnIndex < chunkData->columnCount; columnIndex++)
{
if (chunkData->valueBufferArray[columnIndex] != NULL && !MemoryContextContains(ColumnarCacheMemoryContext(), chunkData->valueBufferArray[columnIndex]))
if (chunkData->valueBufferArray[columnIndex] != NULL && !CachedStringInfo(chunkData->valueBufferArray[columnIndex]))
{
pfree(chunkData->valueBufferArray[columnIndex]->data);
pfree(chunkData->valueBufferArray[columnIndex]);
Expand Down Expand Up @@ -1940,8 +1931,18 @@ DeserializeChunkData(StripeBuffers *stripeBuffers, uint64 chunkIndex,
chunkBuffers->valueCompressionType,
chunkBuffers->decompressedValueSize);

#if PG_VERSION_NUM >= PG_VERSION_16
valueBuffer = repalloc(valueBuffer, sizeof(cached_StringInfo));
((cached_StringInfo *)valueBuffer)->cached = false;
#endif

if (shouldCache)
{

#if PG_VERSION_NUM >= PG_VERSION_16
((cached_StringInfo *)valueBuffer)->cached = true;
#endif

ColumnarAddCacheEntry(state->relation->rd_id, stripeId, chunkIndex, columnIndex, valueBuffer);
MemoryContextSwitchTo(oldMemoryContext);
}
Expand Down
7 changes: 7 additions & 0 deletions columnar/src/backend/columnar/columnar_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,14 @@ ColumnarWriteRow(ColumnarWriteState *writeState, Datum *columnValues, bool *colu
*/
for (columnIndex = 0; columnIndex < columnCount; columnIndex++)
{
#if PG_VERSION_NUM >= PG_VERSION_16
cached_StringInfo *value = palloc(sizeof(cached_StringInfo));
initStringInfo(&value->data);
value->cached = false;
chunkData->valueBufferArray[columnIndex] = (StringInfo)value;
#else
chunkData->valueBufferArray[columnIndex] = makeStringInfo();
#endif
}
}

Expand Down
8 changes: 8 additions & 0 deletions columnar/src/include/columnar/columnar.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ typedef struct StripeSkipList
} StripeSkipList;


#if PG_VERSION_NUM >= PG_VERSION_16
typedef struct cached_StringInfo
{
StringInfoData data;
bool cached;
} cached_StringInfo;
#endif

/*
* ChunkData represents a chunk of data for multiple columns. valueArray stores
* the values of data, and existsArray stores whether a value is present.
Expand Down
Loading