Skip to content

Commit

Permalink
Fix memory context when performing version check (#38)
Browse files Browse the repository at this point in the history
String buffer returned by TextDatumGetCString was in
SPI memory context, and it is invalidated upon SPI_finish()

We set active memory context here so that data will continue
to be available after SPI_finish(). It would be reclaimed
by next memory context reset to occur after completion of the call.
  • Loading branch information
mtuncer committed Jul 4, 2019
1 parent fe00ba6 commit 568fc2a
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/monitor/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ checkPgAutoFailoverVersion()
const int argCount = 1;
Oid argTypes[] = { TEXTOID };
Datum argValues[] = { CStringGetTextDatum(AUTO_FAILOVER_EXTENSION_NAME) };
MemoryContext callerContext = CurrentMemoryContext;

char *selectQuery =
"SELECT default_version, installed_version "
Expand Down Expand Up @@ -220,6 +221,7 @@ checkPgAutoFailoverVersion()
TupleDesc tupleDescriptor = SPI_tuptable->tupdesc;
HeapTuple heapTuple = SPI_tuptable->vals[0];
bool defaultIsNull = false, installedIsNull = false;
MemoryContext spiContext = MemoryContextSwitchTo(callerContext);

Datum defaultVersionDatum =
heap_getattr(heapTuple, 1, tupleDescriptor, &defaultIsNull);
Expand All @@ -236,6 +238,8 @@ checkPgAutoFailoverVersion()
{
installedVersion = TextDatumGetCString(installedVersionDatum);
}

MemoryContextSwitchTo(spiContext);
}

SPI_finish();
Expand Down

0 comments on commit 568fc2a

Please sign in to comment.