From c16114bf17cee5b5649d3dfa462c4a8b594fb2f0 Mon Sep 17 00:00:00 2001
From: Nikita Glukhov <n.gluhov@postgrespro.ru>
Date: Tue, 22 Feb 2022 20:52:45 +0300
Subject: [PATCH 1/2] Fix toast_tuple_externalize()

---
 src/backend/access/table/toast_helper.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/backend/access/table/toast_helper.c b/src/backend/access/table/toast_helper.c
index bfbe941d6ac..11d870535e1 100644
--- a/src/backend/access/table/toast_helper.c
+++ b/src/backend/access/table/toast_helper.c
@@ -345,6 +345,9 @@ toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int maxDataLen,
 										 maxDataLen, options)
 			);
 
+	if (*value == old_value)
+		return;
+
 	if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
 		pfree(DatumGetPointer(old_value));
 	attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
-- 
2.25.1

