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
pull/5967/head
gledis69 2022-05-26 12:00:38 +03:00
parent 27ddb4fc8e
commit dc9da7630f
7 changed files with 14 additions and 6 deletions

View File

@ -2874,7 +2874,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

@ -70,6 +70,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
@ -2276,7 +2277,7 @@ InstalledExtensionVersion(void)
MemoryContext oldMemoryContext = MemoryContextSwitchTo(
MetadataCacheMemoryContext);
installedExtensionVersion = text_to_cstring(DatumGetTextPP(installedVersion));
installedExtensionVersion = text_to_cstring(DatumGetTextPCopy(installedVersion));
MemoryContextSwitchTo(oldMemoryContext);
}
@ -4910,6 +4911,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);