Copy data from heap tuples instead of using references

The general rule is:
If the data is used within the bounds of table_open ... table_close > no need to copy
If the data is required for use even after the table is closed > copy

(cherry picked from commit dc9da7630f)
release-11.0-gledis
gledis69 2022-05-26 12:00:38 +03:00
parent b34b1ce06b
commit 50e8638ede
7 changed files with 14 additions and 6 deletions

View File

@ -3000,7 +3000,7 @@ InstalledExtensionVersionColumnar(void)
}
installedExtensionVersion = text_to_cstring(DatumGetTextPP(installedVersion));
installedExtensionVersion = text_to_cstring(DatumGetTextPCopy(installedVersion));
}
else
{

View File

@ -175,7 +175,7 @@ RecreateDomainStmt(Oid domainOid)
*
* There is no supported way to go from a cooked expression to a raw expression.
*/
constraint->cooked_expr = TextDatumGetCString(conbin);
constraint->cooked_expr = text_to_cstring(DatumGetTextPCopy(conbin));
stmt->constraints = lappend(stmt->constraints, constraint);
}

View File

@ -337,6 +337,7 @@ ExtractEncryptedPassword(Oid roleOid)
Datum passwordDatum = heap_getattr(tuple, Anum_pg_authid_rolpassword,
pgAuthIdDescription, &isNull);
char *passwordCstring = text_to_cstring(DatumGetTextPCopy(passwordDatum));
table_close(pgAuthId, AccessShareLock);
ReleaseSysCache(tuple);
@ -346,7 +347,7 @@ ExtractEncryptedPassword(Oid roleOid)
return NULL;
}
return pstrdup(TextDatumGetCString(passwordDatum));
return pstrdup(passwordCstring);
}
@ -398,7 +399,7 @@ GenerateAlterRoleSetIfExistsCommandList(HeapTuple tuple,
*/
for (i = 0; i < nconfigs; i++)
{
char *config = TextDatumGetCString(configs[i]);
char *config = text_to_cstring(DatumGetTextPCopy(configs[i]));
stmt->setstmt = MakeVariableSetStmt(config);
commandList = lappend(commandList,
(void *) CreateAlterRoleSetIfExistsCommand(stmt));

View File

@ -207,6 +207,7 @@ GetTextSearchDictionaryInitOptions(HeapTuple tup, Form_pg_ts_dict dict)
List *initOptionDefElemList = NIL;
if (!isnull)
{
/* TODO: might need copying */
initOptionDefElemList = deserialize_deflist(dictinitoption);
}

View File

@ -152,7 +152,7 @@ get_extension_version(Oid extensionId)
RelationGetDescr(relation), &isNull);
if (!isNull)
{
versionName = text_to_cstring(DatumGetTextPP(versionDatum));
versionName = text_to_cstring(DatumGetTextPCopy(versionDatum));
}
}

View File

@ -67,6 +67,7 @@
#include "utils/datum.h"
#include "utils/elog.h"
#include "utils/hsearch.h"
#include "utils/jsonb.h"
#if PG_VERSION_NUM >= PG_VERSION_13
#include "common/hashfn.h"
#endif
@ -2249,7 +2250,7 @@ InstalledExtensionVersion(void)
MemoryContext oldMemoryContext = MemoryContextSwitchTo(
MetadataCacheMemoryContext);
installedExtensionVersion = text_to_cstring(DatumGetTextPP(installedVersion));
installedExtensionVersion = text_to_cstring(DatumGetTextPCopy(installedVersion));
MemoryContextSwitchTo(oldMemoryContext);
}
@ -4883,6 +4884,10 @@ DistNodeMetadata(void)
"could not find any entries in pg_dist_metadata")));
}
/* copy the jsonb result before closing the table */
/* since that memory can be freed */
metadata = JsonbPGetDatum(DatumGetJsonbPCopy(metadata));
systable_endscan(scanDescriptor);
table_close(pgDistNodeMetadata, AccessShareLock);

View File

@ -1286,6 +1286,7 @@ ExplainAnalyzeDestPutTuple(TupleDestination *self, Task *task,
return;
}
/* TODO: might need copying */
char *fetchedExplainAnalyzePlan = TextDatumGetCString(explainAnalyze);
double fetchedExplainAnalyzeExecutionDuration = DatumGetFloat8(executionDuration);