diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index d71c0a4322..270d34684e 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -336,6 +336,13 @@ ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot, CmdType cmdtype val = ExecEvalExpr(resultRelInfo->ri_GeneratedExprs[i], econtext, &isnull); + /* + * We must make a copy of val as we have no guarantees about where + * memory for pass-by-ref Dataums is located. + */ + if (!isnull) + val = datumCopy(val, attr->attbyval, attr->attlen); + values[i] = val; nulls[i] = isnull; }