v22-0001-Teach-datum_image_eq-about-cstring-datums.patch

application/octet-stream

Filename: v22-0001-Teach-datum_image_eq-about-cstring-datums.patch
Type: application/octet-stream
Part: 0
Message: Re: [HACKERS] [WIP] Effective storage of duplicates in B-tree index.

Patch

Format: format-patch
Series: patch v22-0001
Subject: Teach datum_image_eq() about cstring datums.
File+
src/backend/utils/adt/datum.c 16 3
From 2697341e50a43ee544f496189a6180eff9713a78 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Mon, 4 Nov 2019 09:07:13 -0800
Subject: [PATCH v22 1/3] Teach datum_image_eq() about cstring datums.

An upcoming patch to add deduplication to nbtree indexes needs to be
able to use datum_image_eq() as a drop-in replacement for opclass
equality in certain contexts.  This includes comparisons of TOASTable
datatypes such as text (at least when deterministic collations are in
use), and cstring datums in system catalog indexes.  cstring is used as
the storage type of "name" columns when indexed by nbtree, despite the
fact that cstring is a pseudo-type.

Discussion: https://postgr.es/m/CAH2-Wzn3Ee49Gmxb7V1VJ3-AC8fWn-Fr8pfWQebHe8rYRxt5OQ@mail.gmail.com
---
 src/backend/utils/adt/datum.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/adt/datum.c b/src/backend/utils/adt/datum.c
index 73703efe05..b20d0640ea 100644
--- a/src/backend/utils/adt/datum.c
+++ b/src/backend/utils/adt/datum.c
@@ -263,6 +263,8 @@ datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
 bool
 datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
 {
+	Size		len1,
+				len2;
 	bool		result = true;
 
 	if (typByVal)
@@ -277,9 +279,6 @@ datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
 	}
 	else if (typLen == -1)
 	{
-		Size		len1,
-					len2;
-
 		len1 = toast_raw_datum_size(value1);
 		len2 = toast_raw_datum_size(value2);
 		/* No need to de-toast if lengths don't match. */
@@ -304,6 +303,20 @@ datum_image_eq(Datum value1, Datum value2, bool typByVal, int typLen)
 				pfree(arg2val);
 		}
 	}
+	else if (typLen == -2)
+	{
+		char	   *s1,
+				   *s2;
+
+		/* Compare cstring datums */
+		s1 = DatumGetCString(value1);
+		s2 = DatumGetCString(value2);
+		len1 = strlen(s1) + 1;
+		len2 = strlen(s2) + 1;
+		if (len1 != len2)
+			return false;
+		result = (memcmp(s1, s2, len1) == 0);
+	}
 	else
 		elog(ERROR, "unexpected typLen: %d", typLen);
 
-- 
2.17.1