Change some Datum to void * for opaque pass-through pointer

Started by Peter Eisentraut20 days ago3 messages
#1Peter Eisentraut
peter@eisentraut.org
1 attachment(s)

I developed this patch as part of the Datum-as-struct semi-proposal [0]/messages/by-id/8246d7ff-f4b7-4363-913e-827dadfeb145@eisentraut.org.
I think it can be useful independent of that.

Here, Datum was used to pass around an opaque pointer between a group of
functions. But one might as well use void * for that; the use of Datum
doesn't achieve anything here and is just distracting.

[0]: /messages/by-id/8246d7ff-f4b7-4363-913e-827dadfeb145@eisentraut.org
/messages/by-id/8246d7ff-f4b7-4363-913e-827dadfeb145@eisentraut.org

Attachments:

0001-Change-some-Datum-to-void-for-opaque-pass-through-po.patchtext/plain; charset=UTF-8; name=0001-Change-some-Datum-to-void-for-opaque-pass-through-po.patchDownload
From ab1ac0e4b1f9948faf56dd09d570d17cc2fd8275 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Fri, 5 Sep 2025 23:21:53 +0200
Subject: [PATCH] Change some Datum to void * for opaque pass-through pointer

---
 src/backend/tsearch/to_tsany.c  | 12 ++++++------
 src/backend/utils/adt/tsquery.c |  8 ++++----
 src/include/tsearch/ts_utils.h  |  4 ++--
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/backend/tsearch/to_tsany.c b/src/backend/tsearch/to_tsany.c
index 4dfcc2cd3bd..678095cb29f 100644
--- a/src/backend/tsearch/to_tsany.c
+++ b/src/backend/tsearch/to_tsany.c
@@ -489,7 +489,7 @@ add_to_tsvector(void *_state, char *elem_value, int elem_len)
  * and different variants are ORed together.
  */
 static void
-pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
+pushval_morph(void *opaque, TSQueryParserState state, char *strval, int lenval, int16 weight, bool prefix)
 {
 	int32		count = 0;
 	ParsedText	prs;
@@ -498,7 +498,7 @@ pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval,
 				cntvar = 0,
 				cntpos = 0,
 				cnt = 0;
-	MorphOpaque *data = (MorphOpaque *) DatumGetPointer(opaque);
+	MorphOpaque *data = opaque;
 
 	prs.lenwords = 4;
 	prs.curwords = 0;
@@ -594,7 +594,7 @@ to_tsquery_byid(PG_FUNCTION_ARGS)
 
 	query = parse_tsquery(text_to_cstring(in),
 						  pushval_morph,
-						  PointerGetDatum(&data),
+						  &data,
 						  0,
 						  NULL);
 
@@ -631,7 +631,7 @@ plainto_tsquery_byid(PG_FUNCTION_ARGS)
 
 	query = parse_tsquery(text_to_cstring(in),
 						  pushval_morph,
-						  PointerGetDatum(&data),
+						  &data,
 						  P_TSQ_PLAIN,
 						  NULL);
 
@@ -669,7 +669,7 @@ phraseto_tsquery_byid(PG_FUNCTION_ARGS)
 
 	query = parse_tsquery(text_to_cstring(in),
 						  pushval_morph,
-						  PointerGetDatum(&data),
+						  &data,
 						  P_TSQ_PLAIN,
 						  NULL);
 
@@ -707,7 +707,7 @@ websearch_to_tsquery_byid(PG_FUNCTION_ARGS)
 
 	query = parse_tsquery(text_to_cstring(in),
 						  pushval_morph,
-						  PointerGetDatum(&data),
+						  &data,
 						  P_TSQ_WEB,
 						  NULL);
 
diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c
index 717de8073d5..6a021c61afb 100644
--- a/src/backend/utils/adt/tsquery.c
+++ b/src/backend/utils/adt/tsquery.c
@@ -671,7 +671,7 @@ cleanOpStack(TSQueryParserState state,
 static void
 makepol(TSQueryParserState state,
 		PushFunction pushval,
-		Datum opaque)
+		void *opaque)
 {
 	int8		operator = 0;
 	ts_tokentype type;
@@ -816,7 +816,7 @@ findoprnd(QueryItem *ptr, int size, bool *needcleanup)
 TSQuery
 parse_tsquery(char *buf,
 			  PushFunction pushval,
-			  Datum opaque,
+			  void *opaque,
 			  int flags,
 			  Node *escontext)
 {
@@ -939,7 +939,7 @@ parse_tsquery(char *buf,
 }
 
 static void
-pushval_asis(Datum opaque, TSQueryParserState state, char *strval, int lenval,
+pushval_asis(void *opaque, TSQueryParserState state, char *strval, int lenval,
 			 int16 weight, bool prefix)
 {
 	pushValue(state, strval, lenval, weight, prefix);
@@ -956,7 +956,7 @@ tsqueryin(PG_FUNCTION_ARGS)
 
 	PG_RETURN_TSQUERY(parse_tsquery(in,
 									pushval_asis,
-									PointerGetDatum(NULL),
+									NULL,
 									0,
 									escontext));
 }
diff --git a/src/include/tsearch/ts_utils.h b/src/include/tsearch/ts_utils.h
index 7debc85ed80..78175929088 100644
--- a/src/include/tsearch/ts_utils.h
+++ b/src/include/tsearch/ts_utils.h
@@ -54,7 +54,7 @@ extern void close_tsvector_parser(TSVectorParseState state);
 struct TSQueryParserStateData;	/* private in backend/utils/adt/tsquery.c */
 typedef struct TSQueryParserStateData *TSQueryParserState;
 
-typedef void (*PushFunction) (Datum opaque, TSQueryParserState state,
+typedef void (*PushFunction) (void *opaque, TSQueryParserState state,
 							  char *token, int tokenlen,
 							  int16 tokenweights,	/* bitmap as described in
 													 * QueryOperand struct */
@@ -66,7 +66,7 @@ typedef void (*PushFunction) (Datum opaque, TSQueryParserState state,
 
 extern TSQuery parse_tsquery(char *buf,
 							 PushFunction pushval,
-							 Datum opaque,
+							 void *opaque,
 							 int flags,
 							 Node *escontext);
 
-- 
2.52.0

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#1)
Re: Change some Datum to void * for opaque pass-through pointer

Peter Eisentraut <peter@eisentraut.org> writes:

Here, Datum was used to pass around an opaque pointer between a group of
functions. But one might as well use void * for that; the use of Datum
doesn't achieve anything here and is just distracting.

We have some APIs where Datum is used to be agnostic about whether
a pointer or an integer is being passed through. on_shmem_exit()
callers make use of both cases, for example.

These tsearch functions don't need that, and they aren't adjacent
to places using such an API, so I'm fine with this patch as written.
But I don't mean that as carte blanche to make such changes
everywhere.

regards, tom lane

#3Peter Eisentraut
peter@eisentraut.org
In reply to: Tom Lane (#2)
Re: Change some Datum to void * for opaque pass-through pointer

On 23.12.25 16:07, Tom Lane wrote:

Peter Eisentraut <peter@eisentraut.org> writes:

Here, Datum was used to pass around an opaque pointer between a group of
functions. But one might as well use void * for that; the use of Datum
doesn't achieve anything here and is just distracting.

We have some APIs where Datum is used to be agnostic about whether
a pointer or an integer is being passed through. on_shmem_exit()
callers make use of both cases, for example.

Agreed, those are different situations.

These tsearch functions don't need that, and they aren't adjacent
to places using such an API, so I'm fine with this patch as written.

Ok, committed.