From 5d6307a2a08110bfc15293ea569238f6d25f0fe4 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Thu, 8 Feb 2024 20:24:22 +0100
Subject: [PATCH v3 5/7] nodeToString: omit serializing NULL datums in Const
 nodes

This saves some bytes in certain cases, and aligns its serialization conditions
with other field's serialization conditions.
---
 src/backend/nodes/outfuncs.c  |  8 ++++----
 src/backend/nodes/readfuncs.c | 14 ++++++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 3973c0e489..1a17eafd57 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -499,11 +499,11 @@ _outConst(StringInfo str, const Const *node, bool omitLocation)
 	WRITE_BOOL_FIELD(constisnull);
 	WRITE_LOCATION_FIELD(location);
 
-	appendStringInfoString(str, " :constvalue ");
-	if (node->constisnull)
-		appendStringInfoString(str, "<>");
-	else
+	if (!node->constisnull)
+	{
+		appendStringInfoString(str, " :constvalue ");
 		outDatum(str, node->constvalue, node->constlen, node->constbyval);
+	}
 }
 
 static void
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 44ab140799..4cdbad9e7e 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -363,11 +363,17 @@ _readConst(void)
 	READ_BOOL_FIELD(constisnull);
 	READ_LOCATION_FIELD(location);
 
-	token = pg_strtok(&length); /* skip :constvalue */
-	if (local_node->constisnull)
-		token = pg_strtok(&length); /* skip "<>" */
-	else
+	if (pg_strtoken_next(":constvalue"))
+	{
+		token = pg_strtok(&length); /* skip :constvalue */
+		Assert(strncmp(token, ":constvalue", sizeof(":constvalue") - 1) == 0);
 		local_node->constvalue = readDatum(local_node->constbyval);
+	}
+	else
+	{
+		/* value was omitted */
+		Assert(local_node->constisnull);
+	}
 
 	READ_DONE();
 }
-- 
2.40.1

