pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Thread-safe stringToNode() / pg_strtok()
14+ messages / 5 participants
[nested] [flat]

Commits from this thread:
  2026-08-24 9673a0aa92  Make stringToNode() infrastructure thread-safe
  2026-08-24 89d091f602  Move some node-read support functions from nodes.h to readfuncs.h
* Thread-safe stringToNode() / pg_strtok()
@ 2026-08-14 14:08 Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 14+ messages in thread

From: Matthias van de Meent @ 2026-08-14 14:08 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>

Hi,

In line with various other efforts making things more re-entrant and
thread-safe, here's a patch that makes stringToNode (and pg_strtok
with it) thread safe.

There are other things we could do to improve this code (e.g.
de/serializaing based on node descriptors instead of generated
functions), but I consider such improvements to be out of scope for
this patch; the attached is just a simple move from global variables
to a context struct passed down the call stack.


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)

Attachments:

v1-0001-Make-stringToNode-infrastructure-thread-safe.patch (application/octet-stream, 23.2K) download From 2e8e0658aa1cbe4ab7f0a79757806da262bfe133 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Fri, 14 Aug 2026 16:01:54 +0200 Subject: [PATCH v1] Make stringToNode infrastructure thread-safe Whilst stringToNode was _technically_ reentrant-safe, this was only for single-threaded workloads; the string we're decoding was still stored in a global variable. This patch moves the global variables into a struct that's passed around on the stack, making this part of the code completely thread-safe. --- src/backend/nodes/gen_node_support.pl | 8 +- src/backend/nodes/read.c | 65 ++++-------- src/backend/nodes/readfuncs.c | 144 +++++++++++++------------- src/include/nodes/nodes.h | 15 +-- src/include/nodes/readfuncs.h | 17 +-- 5 files changed, 116 insertions(+), 133 deletions(-) diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl index f4b1317e99f..0b766272018 100644 --- a/src/backend/nodes/gen_node_support.pl +++ b/src/backend/nodes/gen_node_support.pl @@ -909,7 +909,7 @@ foreach my $n (@node_types) print $rfs "\tif (MATCH(\"$N\", " . length($N) . "))\n" - . "\t\treturn (Node *) _read${n}();\n" + . "\t\treturn (Node *) _read${n}(ctx);\n" unless $no_read; next if elem $n, @custom_read_write; @@ -930,7 +930,7 @@ _out${n}(StringInfo str, const $n *node) : 'READ_LOCALS_NO_FIELDS'; print $rff " static $n * -_read${n}(void) +_read${n}(ReadNodeContext *ctx) { \t$macro($n); @@ -1192,8 +1192,8 @@ _read${n}(void) /* Lookup CustomScanMethods by CustomName */ char *custom_name; const CustomScanMethods *methods; - token = pg_strtok(&length); /* skip methods: */ - token = pg_strtok(&length); /* CustomName */ + token = pg_strtok(ctx, &length); /* skip methods: */ + token = pg_strtok(ctx, &length); /* CustomName */ custom_name = nullable_string(token, length); methods = GetCustomScanMethods(custom_name, false); local_node->methods = methods; diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index f85cf65ea48..bacfc9b4640 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -28,15 +28,6 @@ #include "nodes/value.h" -/* Static state for pg_strtok */ -static const char *pg_strtok_ptr = NULL; - -/* State flag that determines how readfuncs.c should treat location fields */ -#ifdef DEBUG_NODE_TESTS_ENABLED -bool restore_location_fields = false; -#endif - - /* * stringToNode - * builds a Node tree from its string representation (assumed valid) @@ -49,36 +40,22 @@ static void * stringToNodeInternal(const char *str, bool restore_loc_fields) { void *retval; - const char *save_strtok; -#ifdef DEBUG_NODE_TESTS_ENABLED - bool save_restore_location_fields; -#endif + ReadNodeContext ctx; /* - * We save and restore the pre-existing state of pg_strtok. This makes the - * world safe for re-entrant invocation of stringToNode, without incurring - * a lot of notational overhead by having to pass the next-character - * pointer around through all the readfuncs.c code. + * We don't have a separate type for const StringInfos, unconstify + + * careful coding will have to suffice. */ - save_strtok = pg_strtok_ptr; - - pg_strtok_ptr = str; /* point pg_strtok at the string to read */ + ctx.str = str; /* * If enabled, likewise save/restore the location field handling flag. */ #ifdef DEBUG_NODE_TESTS_ENABLED - save_restore_location_fields = restore_location_fields; - restore_location_fields = restore_loc_fields; + ctx.restore_location_fields = restore_loc_fields; #endif - retval = nodeRead(NULL, 0); /* do the reading */ - - pg_strtok_ptr = save_strtok; - -#ifdef DEBUG_NODE_TESTS_ENABLED - restore_location_fields = save_restore_location_fields; -#endif + retval = nodeRead(&ctx, NULL, 0); /* do the reading */ return retval; } @@ -150,12 +127,12 @@ stringToNodeWithLocations(const char *str) * as a single token. */ const char * -pg_strtok(int *length) +pg_strtok(ReadNodeContext *ctx, int *length) { const char *local_str; /* working pointer to string */ const char *ret_str; /* start of token to return */ - local_str = pg_strtok_ptr; + local_str = ctx->str; while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t') local_str++; @@ -163,7 +140,7 @@ pg_strtok(int *length) if (*local_str == '\0') { *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return NULL; /* no more tokens */ } @@ -200,7 +177,7 @@ pg_strtok(int *length) if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>') *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return ret_str; } @@ -317,14 +294,14 @@ nodeTokenType(const char *token, int length) * this should only be invoked from within a stringToNode operation). */ void * -nodeRead(const char *token, int tok_len) +nodeRead(ReadNodeContext *ctx, const char *token, int tok_len) { Node *result; NodeTag type; if (token == NULL) /* need to read a token? */ { - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) /* end of input */ return NULL; @@ -335,8 +312,8 @@ nodeRead(const char *token, int tok_len) switch ((int) type) { case LEFT_BRACE: - result = parseNodeString(); - token = pg_strtok(&tok_len); + result = parseNodeString(ctx); + token = pg_strtok(ctx, &tok_len); if (token == NULL || token[0] != '}') elog(ERROR, "did not find '}' at end of input node"); break; @@ -352,7 +329,7 @@ nodeRead(const char *token, int tok_len) * or a list of nodes/values: (node node ...) *---------- */ - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (tok_len == 1 && token[0] == 'i') @@ -363,7 +340,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -384,7 +361,7 @@ nodeRead(const char *token, int tok_len) Oid val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -405,7 +382,7 @@ nodeRead(const char *token, int tok_len) TransactionId val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -428,7 +405,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (tok_len == 1 && token[0] == ')') @@ -449,8 +426,8 @@ nodeRead(const char *token, int tok_len) /* We have already scanned next token... */ if (token[0] == ')') break; - l = lappend(l, nodeRead(token, tok_len)); - token = pg_strtok(&tok_len); + l = lappend(l, nodeRead(ctx, token, tok_len)); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); } diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index b6b2ce6c792..6679e99fce0 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -56,77 +56,77 @@ /* Read an integer field (anything written as ":fldname %d") */ #define READ_INT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoi(token) /* Read an unsigned integer field (anything written as ":fldname %u") */ #define READ_UINT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoui(token) /* Read a signed integer field (anything written using INT64_FORMAT) */ #define READ_INT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtoi64(token, NULL, 10) /* Read an unsigned integer field (anything written using UINT64_FORMAT) */ #define READ_UINT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtou64(token, NULL, 10) /* Read a long integer field (anything written as ":fldname %ld") */ #define READ_LONG_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atol(token) /* Read an OID field (don't hard-wire assumption that OID is same as uint) */ #define READ_OID_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atooid(token) /* Read a char field (ie, one ascii character) */ #define READ_CHAR_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ /* avoid overhead of calling debackslash() for one char */ \ local_node->fldname = (length == 0) ? '\0' : (token[0] == '\\' ? token[1] : token[0]) /* Read an enumerated-type field that was written as an integer code */ #define READ_ENUM_FIELD(fldname, enumtype) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = (enumtype) atoi(token) /* Read a float field */ #define READ_FLOAT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atof(token) /* Read a boolean field */ #define READ_BOOL_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtobool(token) /* Read a character-string field */ #define READ_STRING_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = nullable_string(token, length) /* Read a parse location field (and possibly throw away the value) */ #ifdef DEBUG_NODE_TESTS_ENABLED #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ - local_node->fldname = restore_location_fields ? atoi(token) : -1 + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ + local_node->fldname = ctx->restore_location_fields ? atoi(token) : -1 #else #define READ_LOCATION_FIELD(fldname) \ token = pg_strtok(&length); /* skip :fldname */ \ @@ -137,35 +137,35 @@ /* Read a Node field */ #define READ_NODE_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = nodeRead(NULL, 0) + local_node->fldname = nodeRead(ctx, NULL, 0) /* Read a bitmapset field */ #define READ_BITMAPSET_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = _readBitmapset() + local_node->fldname = _readBitmapset(ctx) /* Read an attribute number array */ #define READ_ATTRNUMBER_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readAttrNumberCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readAttrNumberCols(ctx, len) /* Read an oid array */ #define READ_OID_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readOidCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readOidCols(ctx, len) /* Read an int array */ #define READ_INT_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readIntCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readIntCols(ctx, len) /* Read a bool array */ #define READ_BOOL_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readBoolCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readBoolCols(ctx, len) /* Routine exit */ #define READ_DONE() \ @@ -204,19 +204,19 @@ nullable_string(const char *token, int length) * Bitmapset when we come across one in other contexts. */ static Bitmapset * -_readBitmapset(void) +_readBitmapset(ReadNodeContext *ctx) { Bitmapset *result = NULL; READ_TEMP_LOCALS(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != '(') elog(ERROR, "unrecognized token: \"%.*s\"", length, token); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != 'b') @@ -227,7 +227,7 @@ _readBitmapset(void) int val; char *endptr; - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (length == 1 && token[0] == ')') @@ -246,9 +246,9 @@ _readBitmapset(void) * That's somewhat historical, though, because calling nodeRead() will work. */ Bitmapset * -readBitmapset(void) +readBitmapset(ReadNodeContext *ctx) { - return _readBitmapset(); + return _readBitmapset(ctx); } #include "readfuncs.funcs.c" @@ -260,7 +260,7 @@ readBitmapset(void) */ static Const * -_readConst(void) +_readConst(ReadNodeContext *ctx) { READ_LOCALS(Const); @@ -272,23 +272,23 @@ _readConst(void) READ_BOOL_FIELD(constisnull); READ_LOCATION_FIELD(location); - token = pg_strtok(&length); /* skip :constvalue */ + token = pg_strtok(ctx, &length); /* skip :constvalue */ if (local_node->constisnull) - token = pg_strtok(&length); /* skip "<>" */ + token = pg_strtok(ctx, &length); /* skip "<>" */ else - local_node->constvalue = readDatum(local_node->constbyval); + local_node->constvalue = readDatum(ctx, local_node->constbyval); READ_DONE(); } static BoolExpr * -_readBoolExpr(void) +_readBoolExpr(ReadNodeContext *ctx) { READ_LOCALS(BoolExpr); /* do-it-yourself enum representation */ - token = pg_strtok(&length); /* skip :boolop */ - token = pg_strtok(&length); /* get field value */ + token = pg_strtok(ctx, &length); /* skip :boolop */ + token = pg_strtok(ctx, &length); /* get field value */ if (length == 3 && strncmp(token, "and", 3) == 0) local_node->boolop = AND_EXPR; else if (length == 2 && strncmp(token, "or", 2) == 0) @@ -305,17 +305,17 @@ _readBoolExpr(void) } static A_Const * -_readA_Const(void) +_readA_Const(ReadNodeContext *ctx) { READ_LOCALS(A_Const); /* We expect either NULL or :val here */ - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 4 && strncmp(token, "NULL", 4) == 0) local_node->isnull = true; else { - union ValUnion *tmp = nodeRead(NULL, 0); + union ValUnion *tmp = nodeRead(ctx, NULL, 0); /* To forestall valgrind complaints, copy only the valid data */ switch (nodeTag(tmp)) @@ -348,7 +348,7 @@ _readA_Const(void) } static RangeTblEntry * -_readRangeTblEntry(void) +_readRangeTblEntry(ReadNodeContext *ctx) { READ_LOCALS(RangeTblEntry); @@ -452,11 +452,11 @@ _readRangeTblEntry(void) } static A_Expr * -_readA_Expr(void) +_readA_Expr(ReadNodeContext *ctx) { READ_LOCALS(A_Expr); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 3 && strncmp(token, "ANY", 3) == 0) { @@ -526,7 +526,7 @@ _readA_Expr(void) else if (length == 5 && strncmp(token, ":name", 5) == 0) { local_node->kind = AEXPR_OP; - local_node->name = nodeRead(NULL, 0); + local_node->name = nodeRead(ctx, NULL, 0); } else elog(ERROR, "unrecognized A_Expr kind: \"%.*s\"", length, token); @@ -541,7 +541,7 @@ _readA_Expr(void) } static ExtensibleNode * -_readExtensibleNode(void) +_readExtensibleNode(ReadNodeContext *ctx) { const ExtensibleNodeMethods *methods; ExtensibleNode *local_node; @@ -549,8 +549,8 @@ _readExtensibleNode(void) READ_TEMP_LOCALS(); - token = pg_strtok(&length); /* skip :extnodename */ - token = pg_strtok(&length); /* get extnodename */ + token = pg_strtok(ctx, &length); /* skip :extnodename */ + token = pg_strtok(ctx, &length); /* get extnodename */ extnodename = nullable_string(token, length); if (!extnodename) @@ -577,14 +577,14 @@ _readExtensibleNode(void) * The string to be read must already have been loaded into pg_strtok(). */ Node * -parseNodeString(void) +parseNodeString(ReadNodeContext *ctx) { READ_TEMP_LOCALS(); /* Guard against stack overflow due to overly complex expressions */ check_stack_depth(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); #define MATCH(tokname, namelen) \ (length == namelen && memcmp(token, tokname, namelen) == 0) @@ -604,7 +604,7 @@ parseNodeString(void) * so we must be told that. */ Datum -readDatum(bool typbyval) +readDatum(ReadNodeContext *ctx, bool typbyval) { Size length; int tokenLength; @@ -615,10 +615,10 @@ readDatum(bool typbyval) /* * read the actual length of the value */ - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); length = atoui(token); - token = pg_strtok(&tokenLength); /* read the '[' */ + token = pg_strtok(ctx, &tokenLength); /* read the '[' */ if (token == NULL || token[0] != '[') elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -631,7 +631,7 @@ readDatum(bool typbyval) s = (char *) (&res); for (Size i = 0; i < (Size) sizeof(Datum); i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } } @@ -642,13 +642,13 @@ readDatum(bool typbyval) s = (char *) palloc(length); for (Size i = 0; i < length; i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } res = PointerGetDatum(s); } - token = pg_strtok(&tokenLength); /* read the ']' */ + token = pg_strtok(ctx, &tokenLength); /* read the ']' */ if (token == NULL || token[0] != ']') elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -666,11 +666,11 @@ readDatum(bool typbyval) */ #define READ_SCALAR_ARRAY(fnname, datatype, convfunc) \ datatype * \ -fnname(int numCols) \ +fnname(ReadNodeContext *ctx, int numCols) \ { \ datatype *vals; \ READ_TEMP_LOCALS(); \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL) \ elog(ERROR, "incomplete scalar array"); \ if (length == 0) \ @@ -680,12 +680,12 @@ fnname(int numCols) \ vals = (datatype *) palloc(numCols * sizeof(datatype)); \ for (int i = 0; i < numCols; i++) \ { \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || token[0] == ')') \ elog(ERROR, "incomplete scalar array"); \ vals[i] = convfunc(token); \ } \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || length != 1 || token[0] != ')') \ elog(ERROR, "incomplete scalar array"); \ return vals; \ diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 372eee20680..be29644b431 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -207,16 +207,19 @@ extern char *bmsToString(const struct Bitmapset *bms); /* * nodes/{readfuncs.c,read.c} */ + +typedef struct ReadNodeContext ReadNodeContext; + extern void *stringToNode(const char *str); #ifdef DEBUG_NODE_TESTS_ENABLED extern void *stringToNodeWithLocations(const char *str); #endif -extern struct Bitmapset *readBitmapset(void); -extern Datum readDatum(bool typbyval); -extern bool *readBoolCols(int numCols); -extern int *readIntCols(int numCols); -extern Oid *readOidCols(int numCols); -extern int16 *readAttrNumberCols(int numCols); +extern struct Bitmapset *readBitmapset(ReadNodeContext *ctx); +extern Datum readDatum(ReadNodeContext *ctx, bool typbyval); +extern bool *readBoolCols(ReadNodeContext *ctx, int numCols); +extern int *readIntCols(ReadNodeContext *ctx, int numCols); +extern Oid *readOidCols(ReadNodeContext *ctx, int numCols); +extern int16 *readAttrNumberCols(ReadNodeContext *ctx, int numCols); /* * nodes/copyfuncs.c diff --git a/src/include/nodes/readfuncs.h b/src/include/nodes/readfuncs.h index 0a5abdd9691..92ca3a800e6 100644 --- a/src/include/nodes/readfuncs.h +++ b/src/include/nodes/readfuncs.h @@ -16,23 +16,26 @@ #include "nodes/nodes.h" -/* - * variable in read.c that needs to be accessible to readfuncs.c - */ +typedef struct ReadNodeContext +{ + /* the string that's being parsed */ + const char *str; #ifdef DEBUG_NODE_TESTS_ENABLED -extern PGDLLIMPORT bool restore_location_fields; + /* state flag determining how readfuncs.c should treat location fields */ + bool restore_location_fields; #endif +} ReadNodeContext; /* * prototypes for functions in read.c (the lisp token parser) */ -extern const char *pg_strtok(int *length); +extern const char *pg_strtok(ReadNodeContext *ctx, int *length); extern char *debackslash(const char *token, int length); -extern void *nodeRead(const char *token, int tok_len); +extern void *nodeRead(ReadNodeContext *ctx, const char *token, int tok_len); /* * prototypes for functions in readfuncs.c */ -extern Node *parseNodeString(void); +extern Node *parseNodeString(ReadNodeContext *ctx); #endif /* READFUNCS_H */ -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-08-16 07:13 ` Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 13:19   ` Re: Thread-safe stringToNode() / pg_strtok() Andres Freund <andres@anarazel.de>
  0 siblings, 2 replies; 14+ messages in thread

From: Michael Paquier @ 2026-08-16 07:13 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>

On Fri, Aug 14, 2026 at 04:08:24PM +0200, Matthias van de Meent wrote:
> In line with various other efforts making things more re-entrant and
> thread-safe, here's a patch that makes stringToNode (and pg_strtok
> with it) thread safe.

I have been reading the patch (not tested, just looking at the diffs),
and using a context as you are doing sounds like an acceptable thing
to do to remove the static pointer used by pg_strtok().  At least the
change makes sense here when taken in isolation.

> There are other things we could do to improve this code (e.g.
> de/serializaing based on node descriptors instead of generated
> functions), but I consider such improvements to be out of scope for
> this patch; the attached is just a simple move from global variables
> to a context struct passed down the call stack.

That sounds fair.

Any thoughts or comments from others?
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
@ 2026-08-17 11:36   ` Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-18 03:38     ` Re: Thread-safe stringToNode() / pg_strtok() 邱宇航 <iamqyh@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  1 sibling, 2 replies; 14+ messages in thread

From: Matthias van de Meent @ 2026-08-17 11:36 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>

On Sun, 16 Aug 2026 at 09:13, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Fri, Aug 14, 2026 at 04:08:24PM +0200, Matthias van de Meent wrote:
> > In line with various other efforts making things more re-entrant and
> > thread-safe, here's a patch that makes stringToNode (and pg_strtok
> > with it) thread safe.
>
> I have been reading the patch (not tested, just looking at the diffs),
> and using a context as you are doing sounds like an acceptable thing
> to do to remove the static pointer used by pg_strtok().  At least the
> change makes sense here when taken in isolation.

Thanks for looking!

> Any thoughts or comments from others?

One update for the patch. This fixes non-DEBUG_NODE_TESTS_ENABLED
builds; I forgot to update the READ_LOCATION_FIELD definition in those
non-debug builds, CI picked this up quite quickly but I didn't get to
fixing it until today.

-Matthias

Attachments:

v2-0001-Make-stringToNode-infrastructure-thread-safe.patch (application/octet-stream, 23.4K) download From 12662a55ce5e940941f197ba2d39bfd87facc772 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Fri, 14 Aug 2026 16:01:54 +0200 Subject: [PATCH v2] Make stringToNode infrastructure thread-safe Whilst stringToNode was _technically_ reentrant-safe, this was only for single-threaded workloads; the string we're decoding was still stored in a global variable. This patch moves the global variables into a struct that's passed around on the stack, making this part of the code completely thread-safe. --- src/backend/nodes/gen_node_support.pl | 8 +- src/backend/nodes/read.c | 65 ++++------- src/backend/nodes/readfuncs.c | 148 +++++++++++++------------- src/include/nodes/nodes.h | 15 +-- src/include/nodes/readfuncs.h | 17 +-- 5 files changed, 118 insertions(+), 135 deletions(-) diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl index f4b1317e99f..0b766272018 100644 --- a/src/backend/nodes/gen_node_support.pl +++ b/src/backend/nodes/gen_node_support.pl @@ -909,7 +909,7 @@ foreach my $n (@node_types) print $rfs "\tif (MATCH(\"$N\", " . length($N) . "))\n" - . "\t\treturn (Node *) _read${n}();\n" + . "\t\treturn (Node *) _read${n}(ctx);\n" unless $no_read; next if elem $n, @custom_read_write; @@ -930,7 +930,7 @@ _out${n}(StringInfo str, const $n *node) : 'READ_LOCALS_NO_FIELDS'; print $rff " static $n * -_read${n}(void) +_read${n}(ReadNodeContext *ctx) { \t$macro($n); @@ -1192,8 +1192,8 @@ _read${n}(void) /* Lookup CustomScanMethods by CustomName */ char *custom_name; const CustomScanMethods *methods; - token = pg_strtok(&length); /* skip methods: */ - token = pg_strtok(&length); /* CustomName */ + token = pg_strtok(ctx, &length); /* skip methods: */ + token = pg_strtok(ctx, &length); /* CustomName */ custom_name = nullable_string(token, length); methods = GetCustomScanMethods(custom_name, false); local_node->methods = methods; diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index f85cf65ea48..bacfc9b4640 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -28,15 +28,6 @@ #include "nodes/value.h" -/* Static state for pg_strtok */ -static const char *pg_strtok_ptr = NULL; - -/* State flag that determines how readfuncs.c should treat location fields */ -#ifdef DEBUG_NODE_TESTS_ENABLED -bool restore_location_fields = false; -#endif - - /* * stringToNode - * builds a Node tree from its string representation (assumed valid) @@ -49,36 +40,22 @@ static void * stringToNodeInternal(const char *str, bool restore_loc_fields) { void *retval; - const char *save_strtok; -#ifdef DEBUG_NODE_TESTS_ENABLED - bool save_restore_location_fields; -#endif + ReadNodeContext ctx; /* - * We save and restore the pre-existing state of pg_strtok. This makes the - * world safe for re-entrant invocation of stringToNode, without incurring - * a lot of notational overhead by having to pass the next-character - * pointer around through all the readfuncs.c code. + * We don't have a separate type for const StringInfos, unconstify + + * careful coding will have to suffice. */ - save_strtok = pg_strtok_ptr; - - pg_strtok_ptr = str; /* point pg_strtok at the string to read */ + ctx.str = str; /* * If enabled, likewise save/restore the location field handling flag. */ #ifdef DEBUG_NODE_TESTS_ENABLED - save_restore_location_fields = restore_location_fields; - restore_location_fields = restore_loc_fields; + ctx.restore_location_fields = restore_loc_fields; #endif - retval = nodeRead(NULL, 0); /* do the reading */ - - pg_strtok_ptr = save_strtok; - -#ifdef DEBUG_NODE_TESTS_ENABLED - restore_location_fields = save_restore_location_fields; -#endif + retval = nodeRead(&ctx, NULL, 0); /* do the reading */ return retval; } @@ -150,12 +127,12 @@ stringToNodeWithLocations(const char *str) * as a single token. */ const char * -pg_strtok(int *length) +pg_strtok(ReadNodeContext *ctx, int *length) { const char *local_str; /* working pointer to string */ const char *ret_str; /* start of token to return */ - local_str = pg_strtok_ptr; + local_str = ctx->str; while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t') local_str++; @@ -163,7 +140,7 @@ pg_strtok(int *length) if (*local_str == '\0') { *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return NULL; /* no more tokens */ } @@ -200,7 +177,7 @@ pg_strtok(int *length) if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>') *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return ret_str; } @@ -317,14 +294,14 @@ nodeTokenType(const char *token, int length) * this should only be invoked from within a stringToNode operation). */ void * -nodeRead(const char *token, int tok_len) +nodeRead(ReadNodeContext *ctx, const char *token, int tok_len) { Node *result; NodeTag type; if (token == NULL) /* need to read a token? */ { - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) /* end of input */ return NULL; @@ -335,8 +312,8 @@ nodeRead(const char *token, int tok_len) switch ((int) type) { case LEFT_BRACE: - result = parseNodeString(); - token = pg_strtok(&tok_len); + result = parseNodeString(ctx); + token = pg_strtok(ctx, &tok_len); if (token == NULL || token[0] != '}') elog(ERROR, "did not find '}' at end of input node"); break; @@ -352,7 +329,7 @@ nodeRead(const char *token, int tok_len) * or a list of nodes/values: (node node ...) *---------- */ - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (tok_len == 1 && token[0] == 'i') @@ -363,7 +340,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -384,7 +361,7 @@ nodeRead(const char *token, int tok_len) Oid val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -405,7 +382,7 @@ nodeRead(const char *token, int tok_len) TransactionId val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -428,7 +405,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (tok_len == 1 && token[0] == ')') @@ -449,8 +426,8 @@ nodeRead(const char *token, int tok_len) /* We have already scanned next token... */ if (token[0] == ')') break; - l = lappend(l, nodeRead(token, tok_len)); - token = pg_strtok(&tok_len); + l = lappend(l, nodeRead(ctx, token, tok_len)); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); } diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index b6b2ce6c792..89d70e07349 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -56,116 +56,116 @@ /* Read an integer field (anything written as ":fldname %d") */ #define READ_INT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoi(token) /* Read an unsigned integer field (anything written as ":fldname %u") */ #define READ_UINT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoui(token) /* Read a signed integer field (anything written using INT64_FORMAT) */ #define READ_INT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtoi64(token, NULL, 10) /* Read an unsigned integer field (anything written using UINT64_FORMAT) */ #define READ_UINT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtou64(token, NULL, 10) /* Read a long integer field (anything written as ":fldname %ld") */ #define READ_LONG_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atol(token) /* Read an OID field (don't hard-wire assumption that OID is same as uint) */ #define READ_OID_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atooid(token) /* Read a char field (ie, one ascii character) */ #define READ_CHAR_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ /* avoid overhead of calling debackslash() for one char */ \ local_node->fldname = (length == 0) ? '\0' : (token[0] == '\\' ? token[1] : token[0]) /* Read an enumerated-type field that was written as an integer code */ #define READ_ENUM_FIELD(fldname, enumtype) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = (enumtype) atoi(token) /* Read a float field */ #define READ_FLOAT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atof(token) /* Read a boolean field */ #define READ_BOOL_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtobool(token) /* Read a character-string field */ #define READ_STRING_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = nullable_string(token, length) /* Read a parse location field (and possibly throw away the value) */ #ifdef DEBUG_NODE_TESTS_ENABLED #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ - local_node->fldname = restore_location_fields ? atoi(token) : -1 + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ + local_node->fldname = ctx->restore_location_fields ? atoi(token) : -1 #else #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ (void) token; /* in case not used elsewhere */ \ local_node->fldname = -1 /* set field to "unknown" */ #endif /* Read a Node field */ #define READ_NODE_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = nodeRead(NULL, 0) + local_node->fldname = nodeRead(ctx, NULL, 0) /* Read a bitmapset field */ #define READ_BITMAPSET_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = _readBitmapset() + local_node->fldname = _readBitmapset(ctx) /* Read an attribute number array */ #define READ_ATTRNUMBER_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readAttrNumberCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readAttrNumberCols(ctx, len) /* Read an oid array */ #define READ_OID_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readOidCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readOidCols(ctx, len) /* Read an int array */ #define READ_INT_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readIntCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readIntCols(ctx, len) /* Read a bool array */ #define READ_BOOL_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readBoolCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readBoolCols(ctx, len) /* Routine exit */ #define READ_DONE() \ @@ -204,19 +204,19 @@ nullable_string(const char *token, int length) * Bitmapset when we come across one in other contexts. */ static Bitmapset * -_readBitmapset(void) +_readBitmapset(ReadNodeContext *ctx) { Bitmapset *result = NULL; READ_TEMP_LOCALS(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != '(') elog(ERROR, "unrecognized token: \"%.*s\"", length, token); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != 'b') @@ -227,7 +227,7 @@ _readBitmapset(void) int val; char *endptr; - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (length == 1 && token[0] == ')') @@ -246,9 +246,9 @@ _readBitmapset(void) * That's somewhat historical, though, because calling nodeRead() will work. */ Bitmapset * -readBitmapset(void) +readBitmapset(ReadNodeContext *ctx) { - return _readBitmapset(); + return _readBitmapset(ctx); } #include "readfuncs.funcs.c" @@ -260,7 +260,7 @@ readBitmapset(void) */ static Const * -_readConst(void) +_readConst(ReadNodeContext *ctx) { READ_LOCALS(Const); @@ -272,23 +272,23 @@ _readConst(void) READ_BOOL_FIELD(constisnull); READ_LOCATION_FIELD(location); - token = pg_strtok(&length); /* skip :constvalue */ + token = pg_strtok(ctx, &length); /* skip :constvalue */ if (local_node->constisnull) - token = pg_strtok(&length); /* skip "<>" */ + token = pg_strtok(ctx, &length); /* skip "<>" */ else - local_node->constvalue = readDatum(local_node->constbyval); + local_node->constvalue = readDatum(ctx, local_node->constbyval); READ_DONE(); } static BoolExpr * -_readBoolExpr(void) +_readBoolExpr(ReadNodeContext *ctx) { READ_LOCALS(BoolExpr); /* do-it-yourself enum representation */ - token = pg_strtok(&length); /* skip :boolop */ - token = pg_strtok(&length); /* get field value */ + token = pg_strtok(ctx, &length); /* skip :boolop */ + token = pg_strtok(ctx, &length); /* get field value */ if (length == 3 && strncmp(token, "and", 3) == 0) local_node->boolop = AND_EXPR; else if (length == 2 && strncmp(token, "or", 2) == 0) @@ -305,17 +305,17 @@ _readBoolExpr(void) } static A_Const * -_readA_Const(void) +_readA_Const(ReadNodeContext *ctx) { READ_LOCALS(A_Const); /* We expect either NULL or :val here */ - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 4 && strncmp(token, "NULL", 4) == 0) local_node->isnull = true; else { - union ValUnion *tmp = nodeRead(NULL, 0); + union ValUnion *tmp = nodeRead(ctx, NULL, 0); /* To forestall valgrind complaints, copy only the valid data */ switch (nodeTag(tmp)) @@ -348,7 +348,7 @@ _readA_Const(void) } static RangeTblEntry * -_readRangeTblEntry(void) +_readRangeTblEntry(ReadNodeContext *ctx) { READ_LOCALS(RangeTblEntry); @@ -452,11 +452,11 @@ _readRangeTblEntry(void) } static A_Expr * -_readA_Expr(void) +_readA_Expr(ReadNodeContext *ctx) { READ_LOCALS(A_Expr); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 3 && strncmp(token, "ANY", 3) == 0) { @@ -526,7 +526,7 @@ _readA_Expr(void) else if (length == 5 && strncmp(token, ":name", 5) == 0) { local_node->kind = AEXPR_OP; - local_node->name = nodeRead(NULL, 0); + local_node->name = nodeRead(ctx, NULL, 0); } else elog(ERROR, "unrecognized A_Expr kind: \"%.*s\"", length, token); @@ -541,7 +541,7 @@ _readA_Expr(void) } static ExtensibleNode * -_readExtensibleNode(void) +_readExtensibleNode(ReadNodeContext *ctx) { const ExtensibleNodeMethods *methods; ExtensibleNode *local_node; @@ -549,8 +549,8 @@ _readExtensibleNode(void) READ_TEMP_LOCALS(); - token = pg_strtok(&length); /* skip :extnodename */ - token = pg_strtok(&length); /* get extnodename */ + token = pg_strtok(ctx, &length); /* skip :extnodename */ + token = pg_strtok(ctx, &length); /* get extnodename */ extnodename = nullable_string(token, length); if (!extnodename) @@ -577,14 +577,14 @@ _readExtensibleNode(void) * The string to be read must already have been loaded into pg_strtok(). */ Node * -parseNodeString(void) +parseNodeString(ReadNodeContext *ctx) { READ_TEMP_LOCALS(); /* Guard against stack overflow due to overly complex expressions */ check_stack_depth(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); #define MATCH(tokname, namelen) \ (length == namelen && memcmp(token, tokname, namelen) == 0) @@ -604,7 +604,7 @@ parseNodeString(void) * so we must be told that. */ Datum -readDatum(bool typbyval) +readDatum(ReadNodeContext *ctx, bool typbyval) { Size length; int tokenLength; @@ -615,10 +615,10 @@ readDatum(bool typbyval) /* * read the actual length of the value */ - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); length = atoui(token); - token = pg_strtok(&tokenLength); /* read the '[' */ + token = pg_strtok(ctx, &tokenLength); /* read the '[' */ if (token == NULL || token[0] != '[') elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -631,7 +631,7 @@ readDatum(bool typbyval) s = (char *) (&res); for (Size i = 0; i < (Size) sizeof(Datum); i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } } @@ -642,13 +642,13 @@ readDatum(bool typbyval) s = (char *) palloc(length); for (Size i = 0; i < length; i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } res = PointerGetDatum(s); } - token = pg_strtok(&tokenLength); /* read the ']' */ + token = pg_strtok(ctx, &tokenLength); /* read the ']' */ if (token == NULL || token[0] != ']') elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -666,11 +666,11 @@ readDatum(bool typbyval) */ #define READ_SCALAR_ARRAY(fnname, datatype, convfunc) \ datatype * \ -fnname(int numCols) \ +fnname(ReadNodeContext *ctx, int numCols) \ { \ datatype *vals; \ READ_TEMP_LOCALS(); \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL) \ elog(ERROR, "incomplete scalar array"); \ if (length == 0) \ @@ -680,12 +680,12 @@ fnname(int numCols) \ vals = (datatype *) palloc(numCols * sizeof(datatype)); \ for (int i = 0; i < numCols; i++) \ { \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || token[0] == ')') \ elog(ERROR, "incomplete scalar array"); \ vals[i] = convfunc(token); \ } \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || length != 1 || token[0] != ')') \ elog(ERROR, "incomplete scalar array"); \ return vals; \ diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 372eee20680..be29644b431 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -207,16 +207,19 @@ extern char *bmsToString(const struct Bitmapset *bms); /* * nodes/{readfuncs.c,read.c} */ + +typedef struct ReadNodeContext ReadNodeContext; + extern void *stringToNode(const char *str); #ifdef DEBUG_NODE_TESTS_ENABLED extern void *stringToNodeWithLocations(const char *str); #endif -extern struct Bitmapset *readBitmapset(void); -extern Datum readDatum(bool typbyval); -extern bool *readBoolCols(int numCols); -extern int *readIntCols(int numCols); -extern Oid *readOidCols(int numCols); -extern int16 *readAttrNumberCols(int numCols); +extern struct Bitmapset *readBitmapset(ReadNodeContext *ctx); +extern Datum readDatum(ReadNodeContext *ctx, bool typbyval); +extern bool *readBoolCols(ReadNodeContext *ctx, int numCols); +extern int *readIntCols(ReadNodeContext *ctx, int numCols); +extern Oid *readOidCols(ReadNodeContext *ctx, int numCols); +extern int16 *readAttrNumberCols(ReadNodeContext *ctx, int numCols); /* * nodes/copyfuncs.c diff --git a/src/include/nodes/readfuncs.h b/src/include/nodes/readfuncs.h index 0a5abdd9691..92ca3a800e6 100644 --- a/src/include/nodes/readfuncs.h +++ b/src/include/nodes/readfuncs.h @@ -16,23 +16,26 @@ #include "nodes/nodes.h" -/* - * variable in read.c that needs to be accessible to readfuncs.c - */ +typedef struct ReadNodeContext +{ + /* the string that's being parsed */ + const char *str; #ifdef DEBUG_NODE_TESTS_ENABLED -extern PGDLLIMPORT bool restore_location_fields; + /* state flag determining how readfuncs.c should treat location fields */ + bool restore_location_fields; #endif +} ReadNodeContext; /* * prototypes for functions in read.c (the lisp token parser) */ -extern const char *pg_strtok(int *length); +extern const char *pg_strtok(ReadNodeContext *ctx, int *length); extern char *debackslash(const char *token, int length); -extern void *nodeRead(const char *token, int tok_len); +extern void *nodeRead(ReadNodeContext *ctx, const char *token, int tok_len); /* * prototypes for functions in readfuncs.c */ -extern Node *parseNodeString(void); +extern Node *parseNodeString(ReadNodeContext *ctx); #endif /* READFUNCS_H */ -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-08-18 03:38     ` 邱宇航 <iamqyh@gmail.com>
  1 sibling, 0 replies; 14+ messages in thread

From: 邱宇航 @ 2026-08-18 03:38 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>

Hi Matthias,

Thanks for v2. I think the ExtensibleNode path still needs an update.
_readExtensibleNode() receives ReadNodeContext *ctx, but still calls:
    methods->nodeRead(local_node);

Since pg_strtok(), readBitmapset(), and the other reader helpers now
require ctx, an extension's nodeRead callback has no way to read its
private fields. External extensions such as Apache AGE, DocumentDB and
AQO implement ExtensibleNode nodeRead callbacks using pg_strtok() and
related reader helpers.

Should ExtensibleNodeMethods.nodeRead also receive ReadNodeContext, for
example:
    void (*nodeRead)(ReadNodeContext *ctx, ExtensibleNode *node);
and be called as:
    methods->nodeRead(ctx, local_node);

Best Regards,
Yuhang Qiu.







^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-08-19 08:46     ` Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  1 sibling, 1 reply; 14+ messages in thread

From: Peter Eisentraut @ 2026-08-19 08:46 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; Michael Paquier <michael@paquier.xyz>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>

On 17.08.26 13:36, Matthias van de Meent wrote:
> On Sun, 16 Aug 2026 at 09:13, Michael Paquier <michael@paquier.xyz> wrote:
>>
>> On Fri, Aug 14, 2026 at 04:08:24PM +0200, Matthias van de Meent wrote:
>>> In line with various other efforts making things more re-entrant and
>>> thread-safe, here's a patch that makes stringToNode (and pg_strtok
>>> with it) thread safe.
>>
>> I have been reading the patch (not tested, just looking at the diffs),
>> and using a context as you are doing sounds like an acceptable thing
>> to do to remove the static pointer used by pg_strtok().  At least the
>> change makes sense here when taken in isolation.
> 
> Thanks for looking!
> 
>> Any thoughts or comments from others?
> 
> One update for the patch. This fixes non-DEBUG_NODE_TESTS_ENABLED
> builds; I forgot to update the READ_LOCATION_FIELD definition in those
> non-debug builds, CI picked this up quite quickly but I didn't get to
> fixing it until today.

The patch contains a comment:

+        * We don't have a separate type for const StringInfos, unconstify +
+        * careful coding will have to suffice.

But the patch doesn't contain any unconstify() calls.







^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
@ 2026-08-19 13:13       ` Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 14+ messages in thread

From: Matthias van de Meent @ 2026-08-19 13:13 UTC (permalink / raw)
  To: Peter Eisentraut <peter@eisentraut.org>; +Cc: Michael Paquier <michael@paquier.xyz>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Wed, 19 Aug 2026 at 10:46, Peter Eisentraut <peter@eisentraut.org> wrote:
>
> On 17.08.26 13:36, Matthias van de Meent wrote:
> > One update for the patch. This fixes non-DEBUG_NODE_TESTS_ENABLED
> > builds; I forgot to update the READ_LOCATION_FIELD definition in those
> > non-debug builds, CI picked this up quite quickly but I didn't get to
> > fixing it until today.
>
> The patch contains a comment:
>
> +        * We don't have a separate type for const StringInfos, unconstify +
> +        * careful coding will have to suffice.
>
> But the patch doesn't contain any unconstify() calls.

Thanks for noticing. An (unposted) earlier version stored a
StringInfoData, using its cursor field for decoding in pg_strtok(),
instead of changing the base string pointer.
The comment references the unconstify() needed to populate this
StringInfoData's non-const char* field.


Attached is patch v3, with new changes:
- Further simplified stringToNodeInternal;
- Another round of comments cleanup, including the one Peter pointed out;
- Added a small comment describing the ReadNodeContext struct;
- Fixed _readExtensibleNode()/ExtensibleNodeMethods->nodeRead(), per
Yuhang Qiu's review.


Kind regards,

Matthias van de Meent

Attachments:

v3-0001-Make-stringToNode-infrastructure-thread-safe.patch (application/octet-stream, 25.6K) download From ffb6de0022f2a67c9687405824045f31a9f102d9 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Fri, 14 Aug 2026 16:01:54 +0200 Subject: [PATCH v3] Make stringToNode infrastructure thread-safe Whilst stringToNode was technically reentrant-safe, this was only for single-threaded workloads; the string we're decoding was still stored in a global variable and wouldn't be safe to use in a coroutines execution model, let alone a multithreaded execution environment. This patch moves the global variables into a struct that's passed around on the stack, making this part of the code completely thread-safe. Reviewed-by: Yuhang Qiu <iamqyh@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> --- src/backend/nodes/gen_node_support.pl | 8 +- src/backend/nodes/read.c | 76 ++++--------- src/backend/nodes/readfuncs.c | 150 +++++++++++++------------- src/include/nodes/extensible.h | 5 +- src/include/nodes/nodes.h | 15 +-- src/include/nodes/readfuncs.h | 18 +++- 6 files changed, 125 insertions(+), 147 deletions(-) diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl index f4b1317e99f..0b766272018 100644 --- a/src/backend/nodes/gen_node_support.pl +++ b/src/backend/nodes/gen_node_support.pl @@ -909,7 +909,7 @@ foreach my $n (@node_types) print $rfs "\tif (MATCH(\"$N\", " . length($N) . "))\n" - . "\t\treturn (Node *) _read${n}();\n" + . "\t\treturn (Node *) _read${n}(ctx);\n" unless $no_read; next if elem $n, @custom_read_write; @@ -930,7 +930,7 @@ _out${n}(StringInfo str, const $n *node) : 'READ_LOCALS_NO_FIELDS'; print $rff " static $n * -_read${n}(void) +_read${n}(ReadNodeContext *ctx) { \t$macro($n); @@ -1192,8 +1192,8 @@ _read${n}(void) /* Lookup CustomScanMethods by CustomName */ char *custom_name; const CustomScanMethods *methods; - token = pg_strtok(&length); /* skip methods: */ - token = pg_strtok(&length); /* CustomName */ + token = pg_strtok(ctx, &length); /* skip methods: */ + token = pg_strtok(ctx, &length); /* CustomName */ custom_name = nullable_string(token, length); methods = GetCustomScanMethods(custom_name, false); local_node->methods = methods; diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index f85cf65ea48..c644cb2cd50 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -28,15 +28,6 @@ #include "nodes/value.h" -/* Static state for pg_strtok */ -static const char *pg_strtok_ptr = NULL; - -/* State flag that determines how readfuncs.c should treat location fields */ -#ifdef DEBUG_NODE_TESTS_ENABLED -bool restore_location_fields = false; -#endif - - /* * stringToNode - * builds a Node tree from its string representation (assumed valid) @@ -48,39 +39,16 @@ bool restore_location_fields = false; static void * stringToNodeInternal(const char *str, bool restore_loc_fields) { - void *retval; - const char *save_strtok; -#ifdef DEBUG_NODE_TESTS_ENABLED - bool save_restore_location_fields; -#endif - - /* - * We save and restore the pre-existing state of pg_strtok. This makes the - * world safe for re-entrant invocation of stringToNode, without incurring - * a lot of notational overhead by having to pass the next-character - * pointer around through all the readfuncs.c code. - */ - save_strtok = pg_strtok_ptr; - - pg_strtok_ptr = str; /* point pg_strtok at the string to read */ - - /* - * If enabled, likewise save/restore the location field handling flag. - */ -#ifdef DEBUG_NODE_TESTS_ENABLED - save_restore_location_fields = restore_location_fields; - restore_location_fields = restore_loc_fields; -#endif - - retval = nodeRead(NULL, 0); /* do the reading */ - - pg_strtok_ptr = save_strtok; + ReadNodeContext ctx; + /* initialize the context */ + ctx.str = str; #ifdef DEBUG_NODE_TESTS_ENABLED - restore_location_fields = save_restore_location_fields; + ctx.restore_location_fields = restore_loc_fields; #endif - return retval; + /* do the reading, and return */ + return nodeRead(&ctx, NULL, 0); } /* @@ -118,8 +86,6 @@ stringToNodeWithLocations(const char *str) * Also, the rules about what is a token are hard-wired rather than being * configured by passing a set of terminating characters. * - * The string is assumed to have been initialized already by stringToNode. - * * The rules for tokens are: * * Whitespace (space, tab, newline) always separates tokens. * * The characters '(', ')', '{', '}' form individual tokens even @@ -150,12 +116,12 @@ stringToNodeWithLocations(const char *str) * as a single token. */ const char * -pg_strtok(int *length) +pg_strtok(ReadNodeContext *ctx, int *length) { const char *local_str; /* working pointer to string */ const char *ret_str; /* start of token to return */ - local_str = pg_strtok_ptr; + local_str = ctx->str; while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t') local_str++; @@ -163,7 +129,7 @@ pg_strtok(int *length) if (*local_str == '\0') { *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return NULL; /* no more tokens */ } @@ -200,7 +166,7 @@ pg_strtok(int *length) if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>') *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return ret_str; } @@ -317,14 +283,14 @@ nodeTokenType(const char *token, int length) * this should only be invoked from within a stringToNode operation). */ void * -nodeRead(const char *token, int tok_len) +nodeRead(ReadNodeContext *ctx, const char *token, int tok_len) { Node *result; NodeTag type; if (token == NULL) /* need to read a token? */ { - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) /* end of input */ return NULL; @@ -335,8 +301,8 @@ nodeRead(const char *token, int tok_len) switch ((int) type) { case LEFT_BRACE: - result = parseNodeString(); - token = pg_strtok(&tok_len); + result = parseNodeString(ctx); + token = pg_strtok(ctx, &tok_len); if (token == NULL || token[0] != '}') elog(ERROR, "did not find '}' at end of input node"); break; @@ -352,7 +318,7 @@ nodeRead(const char *token, int tok_len) * or a list of nodes/values: (node node ...) *---------- */ - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (tok_len == 1 && token[0] == 'i') @@ -363,7 +329,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -384,7 +350,7 @@ nodeRead(const char *token, int tok_len) Oid val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -405,7 +371,7 @@ nodeRead(const char *token, int tok_len) TransactionId val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -428,7 +394,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (tok_len == 1 && token[0] == ')') @@ -449,8 +415,8 @@ nodeRead(const char *token, int tok_len) /* We have already scanned next token... */ if (token[0] == ')') break; - l = lappend(l, nodeRead(token, tok_len)); - token = pg_strtok(&tok_len); + l = lappend(l, nodeRead(ctx, token, tok_len)); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); } diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index b6b2ce6c792..6f93f5a1157 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -56,116 +56,116 @@ /* Read an integer field (anything written as ":fldname %d") */ #define READ_INT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoi(token) /* Read an unsigned integer field (anything written as ":fldname %u") */ #define READ_UINT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoui(token) /* Read a signed integer field (anything written using INT64_FORMAT) */ #define READ_INT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtoi64(token, NULL, 10) /* Read an unsigned integer field (anything written using UINT64_FORMAT) */ #define READ_UINT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtou64(token, NULL, 10) /* Read a long integer field (anything written as ":fldname %ld") */ #define READ_LONG_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atol(token) /* Read an OID field (don't hard-wire assumption that OID is same as uint) */ #define READ_OID_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atooid(token) /* Read a char field (ie, one ascii character) */ #define READ_CHAR_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ /* avoid overhead of calling debackslash() for one char */ \ local_node->fldname = (length == 0) ? '\0' : (token[0] == '\\' ? token[1] : token[0]) /* Read an enumerated-type field that was written as an integer code */ #define READ_ENUM_FIELD(fldname, enumtype) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = (enumtype) atoi(token) /* Read a float field */ #define READ_FLOAT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atof(token) /* Read a boolean field */ #define READ_BOOL_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtobool(token) /* Read a character-string field */ #define READ_STRING_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = nullable_string(token, length) /* Read a parse location field (and possibly throw away the value) */ #ifdef DEBUG_NODE_TESTS_ENABLED #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ - local_node->fldname = restore_location_fields ? atoi(token) : -1 + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ + local_node->fldname = ctx->restore_location_fields ? atoi(token) : -1 #else #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ (void) token; /* in case not used elsewhere */ \ local_node->fldname = -1 /* set field to "unknown" */ #endif /* Read a Node field */ #define READ_NODE_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = nodeRead(NULL, 0) + local_node->fldname = nodeRead(ctx, NULL, 0) /* Read a bitmapset field */ #define READ_BITMAPSET_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = _readBitmapset() + local_node->fldname = _readBitmapset(ctx) /* Read an attribute number array */ #define READ_ATTRNUMBER_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readAttrNumberCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readAttrNumberCols(ctx, len) /* Read an oid array */ #define READ_OID_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readOidCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readOidCols(ctx, len) /* Read an int array */ #define READ_INT_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readIntCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readIntCols(ctx, len) /* Read a bool array */ #define READ_BOOL_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readBoolCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readBoolCols(ctx, len) /* Routine exit */ #define READ_DONE() \ @@ -204,19 +204,19 @@ nullable_string(const char *token, int length) * Bitmapset when we come across one in other contexts. */ static Bitmapset * -_readBitmapset(void) +_readBitmapset(ReadNodeContext *ctx) { Bitmapset *result = NULL; READ_TEMP_LOCALS(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != '(') elog(ERROR, "unrecognized token: \"%.*s\"", length, token); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != 'b') @@ -227,7 +227,7 @@ _readBitmapset(void) int val; char *endptr; - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (length == 1 && token[0] == ')') @@ -246,9 +246,9 @@ _readBitmapset(void) * That's somewhat historical, though, because calling nodeRead() will work. */ Bitmapset * -readBitmapset(void) +readBitmapset(ReadNodeContext *ctx) { - return _readBitmapset(); + return _readBitmapset(ctx); } #include "readfuncs.funcs.c" @@ -260,7 +260,7 @@ readBitmapset(void) */ static Const * -_readConst(void) +_readConst(ReadNodeContext *ctx) { READ_LOCALS(Const); @@ -272,23 +272,23 @@ _readConst(void) READ_BOOL_FIELD(constisnull); READ_LOCATION_FIELD(location); - token = pg_strtok(&length); /* skip :constvalue */ + token = pg_strtok(ctx, &length); /* skip :constvalue */ if (local_node->constisnull) - token = pg_strtok(&length); /* skip "<>" */ + token = pg_strtok(ctx, &length); /* skip "<>" */ else - local_node->constvalue = readDatum(local_node->constbyval); + local_node->constvalue = readDatum(ctx, local_node->constbyval); READ_DONE(); } static BoolExpr * -_readBoolExpr(void) +_readBoolExpr(ReadNodeContext *ctx) { READ_LOCALS(BoolExpr); /* do-it-yourself enum representation */ - token = pg_strtok(&length); /* skip :boolop */ - token = pg_strtok(&length); /* get field value */ + token = pg_strtok(ctx, &length); /* skip :boolop */ + token = pg_strtok(ctx, &length); /* get field value */ if (length == 3 && strncmp(token, "and", 3) == 0) local_node->boolop = AND_EXPR; else if (length == 2 && strncmp(token, "or", 2) == 0) @@ -305,17 +305,17 @@ _readBoolExpr(void) } static A_Const * -_readA_Const(void) +_readA_Const(ReadNodeContext *ctx) { READ_LOCALS(A_Const); /* We expect either NULL or :val here */ - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 4 && strncmp(token, "NULL", 4) == 0) local_node->isnull = true; else { - union ValUnion *tmp = nodeRead(NULL, 0); + union ValUnion *tmp = nodeRead(ctx, NULL, 0); /* To forestall valgrind complaints, copy only the valid data */ switch (nodeTag(tmp)) @@ -348,7 +348,7 @@ _readA_Const(void) } static RangeTblEntry * -_readRangeTblEntry(void) +_readRangeTblEntry(ReadNodeContext *ctx) { READ_LOCALS(RangeTblEntry); @@ -452,11 +452,11 @@ _readRangeTblEntry(void) } static A_Expr * -_readA_Expr(void) +_readA_Expr(ReadNodeContext *ctx) { READ_LOCALS(A_Expr); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 3 && strncmp(token, "ANY", 3) == 0) { @@ -526,7 +526,7 @@ _readA_Expr(void) else if (length == 5 && strncmp(token, ":name", 5) == 0) { local_node->kind = AEXPR_OP; - local_node->name = nodeRead(NULL, 0); + local_node->name = nodeRead(ctx, NULL, 0); } else elog(ERROR, "unrecognized A_Expr kind: \"%.*s\"", length, token); @@ -541,7 +541,7 @@ _readA_Expr(void) } static ExtensibleNode * -_readExtensibleNode(void) +_readExtensibleNode(ReadNodeContext *ctx) { const ExtensibleNodeMethods *methods; ExtensibleNode *local_node; @@ -549,8 +549,8 @@ _readExtensibleNode(void) READ_TEMP_LOCALS(); - token = pg_strtok(&length); /* skip :extnodename */ - token = pg_strtok(&length); /* get extnodename */ + token = pg_strtok(ctx, &length); /* skip :extnodename */ + token = pg_strtok(ctx, &length); /* get extnodename */ extnodename = nullable_string(token, length); if (!extnodename) @@ -562,7 +562,7 @@ _readExtensibleNode(void) local_node->extnodename = extnodename; /* deserialize the private fields */ - methods->nodeRead(local_node); + methods->nodeRead(ctx, local_node); READ_DONE(); } @@ -577,14 +577,14 @@ _readExtensibleNode(void) * The string to be read must already have been loaded into pg_strtok(). */ Node * -parseNodeString(void) +parseNodeString(ReadNodeContext *ctx) { READ_TEMP_LOCALS(); /* Guard against stack overflow due to overly complex expressions */ check_stack_depth(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); #define MATCH(tokname, namelen) \ (length == namelen && memcmp(token, tokname, namelen) == 0) @@ -604,7 +604,7 @@ parseNodeString(void) * so we must be told that. */ Datum -readDatum(bool typbyval) +readDatum(ReadNodeContext *ctx, bool typbyval) { Size length; int tokenLength; @@ -615,10 +615,10 @@ readDatum(bool typbyval) /* * read the actual length of the value */ - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); length = atoui(token); - token = pg_strtok(&tokenLength); /* read the '[' */ + token = pg_strtok(ctx, &tokenLength); /* read the '[' */ if (token == NULL || token[0] != '[') elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -631,7 +631,7 @@ readDatum(bool typbyval) s = (char *) (&res); for (Size i = 0; i < (Size) sizeof(Datum); i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } } @@ -642,13 +642,13 @@ readDatum(bool typbyval) s = (char *) palloc(length); for (Size i = 0; i < length; i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } res = PointerGetDatum(s); } - token = pg_strtok(&tokenLength); /* read the ']' */ + token = pg_strtok(ctx, &tokenLength); /* read the ']' */ if (token == NULL || token[0] != ']') elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -666,11 +666,11 @@ readDatum(bool typbyval) */ #define READ_SCALAR_ARRAY(fnname, datatype, convfunc) \ datatype * \ -fnname(int numCols) \ +fnname(ReadNodeContext *ctx, int numCols) \ { \ datatype *vals; \ READ_TEMP_LOCALS(); \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL) \ elog(ERROR, "incomplete scalar array"); \ if (length == 0) \ @@ -680,12 +680,12 @@ fnname(int numCols) \ vals = (datatype *) palloc(numCols * sizeof(datatype)); \ for (int i = 0; i < numCols; i++) \ { \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || token[0] == ')') \ elog(ERROR, "incomplete scalar array"); \ vals[i] = convfunc(token); \ } \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || length != 1 || token[0] != ')') \ elog(ERROR, "incomplete scalar array"); \ return vals; \ diff --git a/src/include/nodes/extensible.h b/src/include/nodes/extensible.h index 517db95c4a3..564d9a41f2e 100644 --- a/src/include/nodes/extensible.h +++ b/src/include/nodes/extensible.h @@ -54,7 +54,7 @@ typedef struct ExtensibleNode * * nodeRead is a deserialization function for the node type. It does not need * to read type or extnodename; the core system handles those. It should fetch - * the next token using pg_strtok() from the current input stream, and then + * the next token from the ReadNodeContext using pg_strtok(ctx), and then * reconstruct the private fields according to the manner in readfuncs.c. * * All callbacks are mandatory. @@ -69,7 +69,8 @@ typedef struct ExtensibleNodeMethods const struct ExtensibleNode *b); void (*nodeOut) (struct StringInfoData *str, const struct ExtensibleNode *node); - void (*nodeRead) (struct ExtensibleNode *node); + void (*nodeRead) (struct ReadNodeContext *ctx, + struct ExtensibleNode *node); } ExtensibleNodeMethods; extern void RegisterExtensibleNodeMethods(const ExtensibleNodeMethods *methods); diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 372eee20680..be29644b431 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -207,16 +207,19 @@ extern char *bmsToString(const struct Bitmapset *bms); /* * nodes/{readfuncs.c,read.c} */ + +typedef struct ReadNodeContext ReadNodeContext; + extern void *stringToNode(const char *str); #ifdef DEBUG_NODE_TESTS_ENABLED extern void *stringToNodeWithLocations(const char *str); #endif -extern struct Bitmapset *readBitmapset(void); -extern Datum readDatum(bool typbyval); -extern bool *readBoolCols(int numCols); -extern int *readIntCols(int numCols); -extern Oid *readOidCols(int numCols); -extern int16 *readAttrNumberCols(int numCols); +extern struct Bitmapset *readBitmapset(ReadNodeContext *ctx); +extern Datum readDatum(ReadNodeContext *ctx, bool typbyval); +extern bool *readBoolCols(ReadNodeContext *ctx, int numCols); +extern int *readIntCols(ReadNodeContext *ctx, int numCols); +extern Oid *readOidCols(ReadNodeContext *ctx, int numCols); +extern int16 *readAttrNumberCols(ReadNodeContext *ctx, int numCols); /* * nodes/copyfuncs.c diff --git a/src/include/nodes/readfuncs.h b/src/include/nodes/readfuncs.h index 0a5abdd9691..7547f447038 100644 --- a/src/include/nodes/readfuncs.h +++ b/src/include/nodes/readfuncs.h @@ -17,22 +17,30 @@ #include "nodes/nodes.h" /* - * variable in read.c that needs to be accessible to readfuncs.c + * ReadNodeContext - Context data for node deserialization + * + * This struct holds the state and some configuration of the deserializer. */ +typedef struct ReadNodeContext +{ + /* the string that's being parsed */ + const char *str; #ifdef DEBUG_NODE_TESTS_ENABLED -extern PGDLLIMPORT bool restore_location_fields; + /* state flag determining how readfuncs.c should treat location fields */ + bool restore_location_fields; #endif +} ReadNodeContext; /* * prototypes for functions in read.c (the lisp token parser) */ -extern const char *pg_strtok(int *length); +extern const char *pg_strtok(ReadNodeContext *ctx, int *length); extern char *debackslash(const char *token, int length); -extern void *nodeRead(const char *token, int tok_len); +extern void *nodeRead(ReadNodeContext *ctx, const char *token, int tok_len); /* * prototypes for functions in readfuncs.c */ -extern Node *parseNodeString(void); +extern Node *parseNodeString(ReadNodeContext *ctx); #endif /* READFUNCS_H */ -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-08-21 04:41         ` Michael Paquier <michael@paquier.xyz>
  2026-08-21 15:36           ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Michael Paquier @ 2026-08-21 04:41 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Wed, Aug 19, 2026 at 03:13:42PM +0200, Matthias van de Meent wrote:
> Thanks for noticing. An (unposted) earlier version stored a
> StringInfoData, using its cursor field for decoding in pg_strtok(),
> instead of changing the base string pointer.
> The comment references the unconstify() needed to populate this
> StringInfoData's non-const char* field.
> 
> Attached is patch v3, with new changes:
> - Further simplified stringToNodeInternal;
> - Another round of comments cleanup, including the one Peter pointed out;
> - Added a small comment describing the ReadNodeContext struct;

I have looked at this patch in depth, and I like a lot what you are
doing here.  Some comments available below.

A comment at the top of parseNodeString() says the following:
 * The string to be read must already have been loaded into pg_strtok().

I believe that's not true anymore?

> - Fixed _readExtensibleNode()/ExtensibleNodeMethods->nodeRead(), per
> Yuhang Qiu's review.

I have never paid much attention to this file, but I think that this
points to another problem: RegisterExtensibleNodeMethods() is defined,
but we have zero caller of it in core.  So there is a test gap for me
here.  It's not the fault of your patch, and as far as I can see
yourchange looks fine, but it would be nice to close that coverage
hole.

Luckily enough, Aleksander Alekseev has posted a patch to add a test
module that would cover this API, and we could have node-level reads 
and compares to even cover what you are doing here (some more work
still required): 
https://www.postgresql.org/message-id/CAJ7c6TNfn9Fv_Je1etA6rrgq1onVvXbjwBTkbkd4kVQhcu11gg@mail.gmail...

Not something that we have to do here, but something I think you
should be made aware of.  I am particularly interested in opinions
regarding possible gaps in what's proposed on the other thread.

-extern struct Bitmapset *readBitmapset(void);
-extern Datum readDatum(bool typbyval);
-extern bool *readBoolCols(int numCols);
-extern int *readIntCols(int numCols);
-extern Oid *readOidCols(int numCols);
-extern int16 *readAttrNumberCols(int numCols);
+extern struct Bitmapset *readBitmapset(ReadNodeContext *ctx);
+extern Datum readDatum(ReadNodeContext *ctx, bool typbyval);
+extern bool *readBoolCols(ReadNodeContext *ctx, int numCols);
+extern int *readIntCols(ReadNodeContext *ctx, int numCols);
+extern Oid *readOidCols(ReadNodeContext *ctx, int numCols);
+extern int16 *readAttrNumberCols(ReadNodeContext *ctx, int numCols);

Hmm.  There is something that smells structurally wrong to me here.
Historically, readBitmapset() exists for out-of-core code, but I think
that based on what we are dealing with we should remove it and
encourage the use of readNode() instead.  The bottom of readfuncs.c
lists four more of these functions, but readNode() would lead to a
similar result.  My point is to get rid of the forward declaration of
ReadNodeContext in nodes.h, and do that as a preliminary patch.  I
have quickly tested a move of these functions to readfuncs.c, making
them static, and that works.  Let's also remove readBitmapset() and
recommend folks to modernize and move to readNode().  I'd suggest to
do that as a small refactoring piece done before the introduction of
the thread-safe pg_strtok().

And just to not sound suspicious, I have looked at
codesearch.debian.org to look at uses of these routines, and found
zero hits.  So making them local to readfuncs.c to make the result of
this thread more elegant does not stress me much.  :)

Similarly, it would be cleaner to have extensible.h include
nodes/readfuncs.h to grab the definition of ReadNodeContext.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
@ 2026-08-21 15:36           ` Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 23:47             ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 14+ messages in thread

From: Matthias van de Meent @ 2026-08-21 15:36 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Fri, 21 Aug 2026 at 06:41, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Wed, Aug 19, 2026 at 03:13:42PM +0200, Matthias van de Meent wrote:
> > Thanks for noticing. An (unposted) earlier version stored a
> > StringInfoData, using its cursor field for decoding in pg_strtok(),
> > instead of changing the base string pointer.
> > The comment references the unconstify() needed to populate this
> > StringInfoData's non-const char* field.
> >
> > Attached is patch v3, with new changes:
> > - Further simplified stringToNodeInternal;
> > - Another round of comments cleanup, including the one Peter pointed out;
> > - Added a small comment describing the ReadNodeContext struct;
>
> I have looked at this patch in depth, and I like a lot what you are
> doing here.  Some comments available below.
>
> A comment at the top of parseNodeString() says the following:
>  * The string to be read must already have been loaded into pg_strtok().
>
> I believe that's not true anymore?

Correct. I hadn't found this because "loaded into pg_strtok()" isn't exactly

> -extern struct Bitmapset *readBitmapset(void);
> -extern Datum readDatum(bool typbyval);
> -extern bool *readBoolCols(int numCols);
> -extern int *readIntCols(int numCols);
> -extern Oid *readOidCols(int numCols);
> -extern int16 *readAttrNumberCols(int numCols);
> +extern struct Bitmapset *readBitmapset(ReadNodeContext *ctx);
> +extern Datum readDatum(ReadNodeContext *ctx, bool typbyval);
> +extern bool *readBoolCols(ReadNodeContext *ctx, int numCols);
> +extern int *readIntCols(ReadNodeContext *ctx, int numCols);
> +extern Oid *readOidCols(ReadNodeContext *ctx, int numCols);
> +extern int16 *readAttrNumberCols(ReadNodeContext *ctx, int numCols);
>
> Hmm.  There is something that smells structurally wrong to me here.
> Historically, readBitmapset() exists for out-of-core code, but I think
> that based on what we are dealing with we should remove it and
> encourage the use of readNode() instead.

I'm ambivalent about that.  A direct call into the Bitmapset
(de)serializer avoids the overhead for dispatching to _readBitmapset()
in readNode().

Of all nodes, Bitmapset is the only special_read_write node type that
an ExtensibleNode implementation could reasonably want to serialize.
The other special_read_write node types (String, Integer, Float,
Boolean, Bitstring) are all parse node types, which I think are more
properly replaced with either an A_Const or the parsed primitive
values in custom planner/executor nodes.

>  My point is to get rid of the forward declaration of
> ReadNodeContext in nodes.h, and do that as a preliminary patch.

ReadNodeContext is invented in this patchset with just a single patch.
What preliminary patch could be added that gets rid of something
introduced in later patches?

>  The bottom of readfuncs.c
> lists four more of these functions, but readNode() would lead to a
> similar result. [..] I
> have quickly tested a move of these functions to readfuncs.c, making
> them static, and that works.

If you refer to the read*Cols functions and/or readDatum, then your
conclusion is not accurate: The read*Cols functions don't operate on
Nodes, but on dense arrays of their respective non-node types.  They
exist to help deserialize the various arrays or Datum values in a
node's fields.

A user could implement these functions by themselves, but exposing
these procedures helps avoid developers having to re-invent the wheel.
It's a simple wheel, sure, but a wheel it is regardless.

> Let's also remove readBitmapset() and
> recommend folks to modernize and move to readNode().  I'd suggest to
> do that as a small refactoring piece done before the introduction of
> the thread-safe pg_strtok().
>
> And just to not sound suspicious, I have looked at
> codesearch.debian.org to look at uses of these routines, and found
> zero hits.  So making them local to readfuncs.c to make the result of
> this thread more elegant does not stress me much.  :)

I'm happy to move the read*Cols, readBitmapset, and readDatum
declarations to readfuncs.h instead, if that's OK with you?  I'd like
to keep them public for the reasons above - it's not like it costs us
a lot to maintain these.

> Similarly, it would be cleaner to have extensible.h include
> nodes/readfuncs.h to grab the definition of ReadNodeContext.

I don't think that everyone who needs to know the shape of
ExtensibleNode needs to know what a ReadNodeContext is. Using a
forward declaration for it makes more sense to me; especially given
that it currently also uses "struct ExtensibleNode" even though that
same header typedefs ExtensibleNode just a few lines above that.


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-21 15:36           ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-08-21 23:47             ` Michael Paquier <michael@paquier.xyz>
  2026-08-22 20:16               ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Michael Paquier @ 2026-08-21 23:47 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Fri, Aug 21, 2026 at 05:36:23PM +0200, Matthias van de Meent wrote:
> On Fri, 21 Aug 2026 at 06:41, Michael Paquier <michael@paquier.xyz> wrote:
>> Hmm.  There is something that smells structurally wrong to me here.
>> Historically, readBitmapset() exists for out-of-core code, but I think
>> that based on what we are dealing with we should remove it and
>> encourage the use of readNode() instead.
> 
> I'm ambivalent about that.  A direct call into the Bitmapset
> (de)serializer avoids the overhead for dispatching to _readBitmapset()
> in readNode().
> 
> Of all nodes, Bitmapset is the only special_read_write node type that
> an ExtensibleNode implementation could reasonably want to serialize.
> The other special_read_write node types (String, Integer, Float,
> Boolean, Bitstring) are all parse node types, which I think are more
> properly replaced with either an A_Const or the parsed primitive
> values in custom planner/executor nodes.

Are you aware of code that relies on these specific calls?  I have
poked as well at github, but could not find even one hit that refers
to non-forked code.  I have seen traces of copies of this code, for
backward-compatibility purposes only (citus is one, due to its
branching model).

>>  My point is to get rid of the forward declaration of
>> ReadNodeContext in nodes.h, and do that as a preliminary patch.
> 
> ReadNodeContext is invented in this patchset with just a single patch.
> What preliminary patch could be added that gets rid of something
> introduced in later patches?

I just mean to create a first patch to not publish anymore the
readBitmapset() & friends set of functions, make them static inside
readfuncs.c.  This results in a simplification of your patch by not
requiring the knowledge of ReadNodeContext anymore in nodes.h and this
forward declaration.  Sounds like a win to me based on my arguments of
the first paragraph, because it leads to a cleaner result.

> If you refer to the read*Cols functions and/or readDatum, then your
> conclusion is not accurate: The read*Cols functions don't operate on
> Nodes, but on dense arrays of their respective non-node types.  They
> exist to help deserialize the various arrays or Datum values in a
> node's fields.
> 
> A user could implement these functions by themselves, but exposing
> these procedures helps avoid developers having to re-invent the wheel.
> It's a simple wheel, sure, but a wheel it is regardless.

Argument that could also apply to a bunch of these.  I am OK if
someone can point me out to some code out there where these are
relevants.  In terms of in-core code and after searches in the open, I
just see no evidence that points to this direction..

> I'm happy to move the read*Cols, readBitmapset, and readDatum
> declarations to readfuncs.h instead, if that's OK with you?  I'd like
> to keep them public for the reasons above - it's not like it costs us
> a lot to maintain these.

I'd still make the internals of node code leaner, I guess..  That
leads to a simpler result.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-21 15:36           ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 23:47             ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
@ 2026-08-22 20:16               ` Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-22 21:38                 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 14+ messages in thread

From: Matthias van de Meent @ 2026-08-22 20:16 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Sat, 22 Aug 2026 at 01:47, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Fri, Aug 21, 2026 at 05:36:23PM +0200, Matthias van de Meent wrote:
> > On Fri, 21 Aug 2026 at 06:41, Michael Paquier <michael@paquier.xyz> wrote:
> >> Hmm.  There is something that smells structurally wrong to me here.
> >> Historically, readBitmapset() exists for out-of-core code, but I think
> >> that based on what we are dealing with we should remove it and
> >> encourage the use of readNode() instead.
> >
> > I'm ambivalent about that.  A direct call into the Bitmapset
> > (de)serializer avoids the overhead for dispatching to _readBitmapset()
> > in readNode().
> >
> > Of all nodes, Bitmapset is the only special_read_write node type that
> > an ExtensibleNode implementation could reasonably want to serialize.
> > The other special_read_write node types (String, Integer, Float,
> > Boolean, Bitstring) are all parse node types, which I think are more
> > properly replaced with either an A_Const or the parsed primitive
> > values in custom planner/executor nodes.
>
> Are you aware of code that relies on these specific calls?  I have
> poked as well at github, but could not find even one hit that refers
> to non-forked code.  I have seen traces of copies of this code, for
> backward-compatibility purposes only (citus is one, due to its
> branching model).

Not for readBitmap, no. See below for others.

> > >  My point is to get rid of the forward declaration of
> > > ReadNodeContext in nodes.h, and do that as a preliminary patch.
> >
> > ReadNodeContext is invented in this patchset with just a single patch.
> > What preliminary patch could be added that gets rid of something
> > introduced in later patches?
>
> I just mean to create a first patch to not publish anymore the
> readBitmapset() & friends set of functions, make them static inside
> readfuncs.c.  This results in a simplification of your patch by not
> requiring the knowledge of ReadNodeContext anymore in nodes.h and this
> forward declaration.  Sounds like a win to me based on my arguments of
> the first paragraph, because it leads to a cleaner result.

I've moved them to readfuncs.h, which I think is a reasonable
compromise; avoiding the forward declaration whilst still exposing
these functions to extensions that might need them.

> > If you refer to the read*Cols functions and/or readDatum, then your
> > conclusion is not accurate: The read*Cols functions don't operate on
> > Nodes, but on dense arrays of their respective non-node types.  They
> > exist to help deserialize the various arrays or Datum values in a
> > node's fields.
> >
> > A user could implement these functions by themselves, but exposing
> > these procedures helps avoid developers having to re-invent the wheel.
> > It's a simple wheel, sure, but a wheel it is regardless.
>
> Argument that could also apply to a bunch of these.  I am OK if
> someone can point me out to some code out there where these are
> relevants.  In terms of in-core code and after searches in the open, I
> just see no evidence that points to this direction..

There's a "shardman" repo that seems to directly use readOidCols:
https://github.com/ZhaoDiankui/shardman/blob/merge/src/exchange.c#L162

Apache AGE seems to use (or at least, reference) our read*Cols
definitions in their backend/nodes/cypher_readfuncs.c; in copied
macros that don't currently seem to have any usage but might get used
in the future. Its READ_BITMAP_FIELD macro won't work as-is (it
references _readBitmap, which is the private/static version, whereas
readBitmap is the exposed version).

----------

Attached is version 4, which has these changes compared to v3:

 * new 0001 patch, which moves the read* functions from nodes.h to readfuncs.h.
    As was discussed upthread. 0002 is adjusted accordingly.
 * nodes/extensible.h now includes nodes/readfuncs.h
    I revised my opinion on this: I noticed my compiler complained
about the plain "struct ReadNodeContext *ctx" argument if I didn't put
a "struct ReadNodeContext;" declaration at the top level of the file,
which (when combined with relevant comments) would add more verbose
clutter than the alternative of including nodes/readfuncs.h.
 * adjusted comment on parseNodeString, as reported upthread.


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)

Attachments: download all (2 files, .tar.gz)

v4-0002-Make-stringToNode-infrastructure-thread-safe.patch (application/octet-stream, 25.4K) download From b5994bb02fef57e5245b00687908857080a22269 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Fri, 14 Aug 2026 16:01:54 +0200 Subject: [PATCH v4 2/2] Make stringToNode infrastructure thread-safe Whilst stringToNode was technically reentrant-safe, this was only for single-threaded workloads; the string we're decoding was still stored in a global variable and wouldn't be safe to use in a coroutines execution model, let alone a multithreaded execution environment. This patch moves the global variables into a struct that's passed around on the stack, making this part of the code completely thread-safe. Reviewed-by: Yuhang Qiu <iamqyh@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Peter Eisentraut <peter@eisentraut.org> --- src/backend/nodes/gen_node_support.pl | 8 +- src/backend/nodes/read.c | 76 ++++--------- src/backend/nodes/readfuncs.c | 152 +++++++++++++------------- src/include/nodes/extensible.h | 6 +- src/include/nodes/readfuncs.h | 30 +++-- 5 files changed, 123 insertions(+), 149 deletions(-) diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl index f4b1317e99f..0b766272018 100644 --- a/src/backend/nodes/gen_node_support.pl +++ b/src/backend/nodes/gen_node_support.pl @@ -909,7 +909,7 @@ foreach my $n (@node_types) print $rfs "\tif (MATCH(\"$N\", " . length($N) . "))\n" - . "\t\treturn (Node *) _read${n}();\n" + . "\t\treturn (Node *) _read${n}(ctx);\n" unless $no_read; next if elem $n, @custom_read_write; @@ -930,7 +930,7 @@ _out${n}(StringInfo str, const $n *node) : 'READ_LOCALS_NO_FIELDS'; print $rff " static $n * -_read${n}(void) +_read${n}(ReadNodeContext *ctx) { \t$macro($n); @@ -1192,8 +1192,8 @@ _read${n}(void) /* Lookup CustomScanMethods by CustomName */ char *custom_name; const CustomScanMethods *methods; - token = pg_strtok(&length); /* skip methods: */ - token = pg_strtok(&length); /* CustomName */ + token = pg_strtok(ctx, &length); /* skip methods: */ + token = pg_strtok(ctx, &length); /* CustomName */ custom_name = nullable_string(token, length); methods = GetCustomScanMethods(custom_name, false); local_node->methods = methods; diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index f85cf65ea48..c644cb2cd50 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -28,15 +28,6 @@ #include "nodes/value.h" -/* Static state for pg_strtok */ -static const char *pg_strtok_ptr = NULL; - -/* State flag that determines how readfuncs.c should treat location fields */ -#ifdef DEBUG_NODE_TESTS_ENABLED -bool restore_location_fields = false; -#endif - - /* * stringToNode - * builds a Node tree from its string representation (assumed valid) @@ -48,39 +39,16 @@ bool restore_location_fields = false; static void * stringToNodeInternal(const char *str, bool restore_loc_fields) { - void *retval; - const char *save_strtok; -#ifdef DEBUG_NODE_TESTS_ENABLED - bool save_restore_location_fields; -#endif - - /* - * We save and restore the pre-existing state of pg_strtok. This makes the - * world safe for re-entrant invocation of stringToNode, without incurring - * a lot of notational overhead by having to pass the next-character - * pointer around through all the readfuncs.c code. - */ - save_strtok = pg_strtok_ptr; - - pg_strtok_ptr = str; /* point pg_strtok at the string to read */ - - /* - * If enabled, likewise save/restore the location field handling flag. - */ -#ifdef DEBUG_NODE_TESTS_ENABLED - save_restore_location_fields = restore_location_fields; - restore_location_fields = restore_loc_fields; -#endif - - retval = nodeRead(NULL, 0); /* do the reading */ - - pg_strtok_ptr = save_strtok; + ReadNodeContext ctx; + /* initialize the context */ + ctx.str = str; #ifdef DEBUG_NODE_TESTS_ENABLED - restore_location_fields = save_restore_location_fields; + ctx.restore_location_fields = restore_loc_fields; #endif - return retval; + /* do the reading, and return */ + return nodeRead(&ctx, NULL, 0); } /* @@ -118,8 +86,6 @@ stringToNodeWithLocations(const char *str) * Also, the rules about what is a token are hard-wired rather than being * configured by passing a set of terminating characters. * - * The string is assumed to have been initialized already by stringToNode. - * * The rules for tokens are: * * Whitespace (space, tab, newline) always separates tokens. * * The characters '(', ')', '{', '}' form individual tokens even @@ -150,12 +116,12 @@ stringToNodeWithLocations(const char *str) * as a single token. */ const char * -pg_strtok(int *length) +pg_strtok(ReadNodeContext *ctx, int *length) { const char *local_str; /* working pointer to string */ const char *ret_str; /* start of token to return */ - local_str = pg_strtok_ptr; + local_str = ctx->str; while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t') local_str++; @@ -163,7 +129,7 @@ pg_strtok(int *length) if (*local_str == '\0') { *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return NULL; /* no more tokens */ } @@ -200,7 +166,7 @@ pg_strtok(int *length) if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>') *length = 0; - pg_strtok_ptr = local_str; + ctx->str = local_str; return ret_str; } @@ -317,14 +283,14 @@ nodeTokenType(const char *token, int length) * this should only be invoked from within a stringToNode operation). */ void * -nodeRead(const char *token, int tok_len) +nodeRead(ReadNodeContext *ctx, const char *token, int tok_len) { Node *result; NodeTag type; if (token == NULL) /* need to read a token? */ { - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) /* end of input */ return NULL; @@ -335,8 +301,8 @@ nodeRead(const char *token, int tok_len) switch ((int) type) { case LEFT_BRACE: - result = parseNodeString(); - token = pg_strtok(&tok_len); + result = parseNodeString(ctx); + token = pg_strtok(ctx, &tok_len); if (token == NULL || token[0] != '}') elog(ERROR, "did not find '}' at end of input node"); break; @@ -352,7 +318,7 @@ nodeRead(const char *token, int tok_len) * or a list of nodes/values: (node node ...) *---------- */ - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (tok_len == 1 && token[0] == 'i') @@ -363,7 +329,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -384,7 +350,7 @@ nodeRead(const char *token, int tok_len) Oid val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -405,7 +371,7 @@ nodeRead(const char *token, int tok_len) TransactionId val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); if (token[0] == ')') @@ -428,7 +394,7 @@ nodeRead(const char *token, int tok_len) int val; char *endptr; - token = pg_strtok(&tok_len); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (tok_len == 1 && token[0] == ')') @@ -449,8 +415,8 @@ nodeRead(const char *token, int tok_len) /* We have already scanned next token... */ if (token[0] == ')') break; - l = lappend(l, nodeRead(token, tok_len)); - token = pg_strtok(&tok_len); + l = lappend(l, nodeRead(ctx, token, tok_len)); + token = pg_strtok(ctx, &tok_len); if (token == NULL) elog(ERROR, "unterminated List structure"); } diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index b6b2ce6c792..29fd89f0c3c 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -56,116 +56,116 @@ /* Read an integer field (anything written as ":fldname %d") */ #define READ_INT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoi(token) /* Read an unsigned integer field (anything written as ":fldname %u") */ #define READ_UINT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atoui(token) /* Read a signed integer field (anything written using INT64_FORMAT) */ #define READ_INT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtoi64(token, NULL, 10) /* Read an unsigned integer field (anything written using UINT64_FORMAT) */ #define READ_UINT64_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtou64(token, NULL, 10) /* Read a long integer field (anything written as ":fldname %ld") */ #define READ_LONG_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atol(token) /* Read an OID field (don't hard-wire assumption that OID is same as uint) */ #define READ_OID_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atooid(token) /* Read a char field (ie, one ascii character) */ #define READ_CHAR_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ /* avoid overhead of calling debackslash() for one char */ \ local_node->fldname = (length == 0) ? '\0' : (token[0] == '\\' ? token[1] : token[0]) /* Read an enumerated-type field that was written as an integer code */ #define READ_ENUM_FIELD(fldname, enumtype) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = (enumtype) atoi(token) /* Read a float field */ #define READ_FLOAT_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = atof(token) /* Read a boolean field */ #define READ_BOOL_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = strtobool(token) /* Read a character-string field */ #define READ_STRING_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ local_node->fldname = nullable_string(token, length) /* Read a parse location field (and possibly throw away the value) */ #ifdef DEBUG_NODE_TESTS_ENABLED #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ - local_node->fldname = restore_location_fields ? atoi(token) : -1 + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ + local_node->fldname = ctx->restore_location_fields ? atoi(token) : -1 #else #define READ_LOCATION_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ - token = pg_strtok(&length); /* get field value */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* get field value */ \ (void) token; /* in case not used elsewhere */ \ local_node->fldname = -1 /* set field to "unknown" */ #endif /* Read a Node field */ #define READ_NODE_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = nodeRead(NULL, 0) + local_node->fldname = nodeRead(ctx, NULL, 0) /* Read a bitmapset field */ #define READ_BITMAPSET_FIELD(fldname) \ - token = pg_strtok(&length); /* skip :fldname */ \ + token = pg_strtok(ctx, &length); /* skip :fldname */ \ (void) token; /* in case not used elsewhere */ \ - local_node->fldname = _readBitmapset() + local_node->fldname = _readBitmapset(ctx) /* Read an attribute number array */ #define READ_ATTRNUMBER_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readAttrNumberCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readAttrNumberCols(ctx, len) /* Read an oid array */ #define READ_OID_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readOidCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readOidCols(ctx, len) /* Read an int array */ #define READ_INT_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readIntCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readIntCols(ctx, len) /* Read a bool array */ #define READ_BOOL_ARRAY(fldname, len) \ - token = pg_strtok(&length); /* skip :fldname */ \ - local_node->fldname = readBoolCols(len) + token = pg_strtok(ctx, &length); /* skip :fldname */ \ + local_node->fldname = readBoolCols(ctx, len) /* Routine exit */ #define READ_DONE() \ @@ -204,19 +204,19 @@ nullable_string(const char *token, int length) * Bitmapset when we come across one in other contexts. */ static Bitmapset * -_readBitmapset(void) +_readBitmapset(ReadNodeContext *ctx) { Bitmapset *result = NULL; READ_TEMP_LOCALS(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != '(') elog(ERROR, "unrecognized token: \"%.*s\"", length, token); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "incomplete Bitmapset structure"); if (length != 1 || token[0] != 'b') @@ -227,7 +227,7 @@ _readBitmapset(void) int val; char *endptr; - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (token == NULL) elog(ERROR, "unterminated Bitmapset structure"); if (length == 1 && token[0] == ')') @@ -246,9 +246,9 @@ _readBitmapset(void) * That's somewhat historical, though, because calling nodeRead() will work. */ Bitmapset * -readBitmapset(void) +readBitmapset(ReadNodeContext *ctx) { - return _readBitmapset(); + return _readBitmapset(ctx); } #include "readfuncs.funcs.c" @@ -260,7 +260,7 @@ readBitmapset(void) */ static Const * -_readConst(void) +_readConst(ReadNodeContext *ctx) { READ_LOCALS(Const); @@ -272,23 +272,23 @@ _readConst(void) READ_BOOL_FIELD(constisnull); READ_LOCATION_FIELD(location); - token = pg_strtok(&length); /* skip :constvalue */ + token = pg_strtok(ctx, &length); /* skip :constvalue */ if (local_node->constisnull) - token = pg_strtok(&length); /* skip "<>" */ + token = pg_strtok(ctx, &length); /* skip "<>" */ else - local_node->constvalue = readDatum(local_node->constbyval); + local_node->constvalue = readDatum(ctx, local_node->constbyval); READ_DONE(); } static BoolExpr * -_readBoolExpr(void) +_readBoolExpr(ReadNodeContext *ctx) { READ_LOCALS(BoolExpr); /* do-it-yourself enum representation */ - token = pg_strtok(&length); /* skip :boolop */ - token = pg_strtok(&length); /* get field value */ + token = pg_strtok(ctx, &length); /* skip :boolop */ + token = pg_strtok(ctx, &length); /* get field value */ if (length == 3 && strncmp(token, "and", 3) == 0) local_node->boolop = AND_EXPR; else if (length == 2 && strncmp(token, "or", 2) == 0) @@ -305,17 +305,17 @@ _readBoolExpr(void) } static A_Const * -_readA_Const(void) +_readA_Const(ReadNodeContext *ctx) { READ_LOCALS(A_Const); /* We expect either NULL or :val here */ - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 4 && strncmp(token, "NULL", 4) == 0) local_node->isnull = true; else { - union ValUnion *tmp = nodeRead(NULL, 0); + union ValUnion *tmp = nodeRead(ctx, NULL, 0); /* To forestall valgrind complaints, copy only the valid data */ switch (nodeTag(tmp)) @@ -348,7 +348,7 @@ _readA_Const(void) } static RangeTblEntry * -_readRangeTblEntry(void) +_readRangeTblEntry(ReadNodeContext *ctx) { READ_LOCALS(RangeTblEntry); @@ -452,11 +452,11 @@ _readRangeTblEntry(void) } static A_Expr * -_readA_Expr(void) +_readA_Expr(ReadNodeContext *ctx) { READ_LOCALS(A_Expr); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); if (length == 3 && strncmp(token, "ANY", 3) == 0) { @@ -526,7 +526,7 @@ _readA_Expr(void) else if (length == 5 && strncmp(token, ":name", 5) == 0) { local_node->kind = AEXPR_OP; - local_node->name = nodeRead(NULL, 0); + local_node->name = nodeRead(ctx, NULL, 0); } else elog(ERROR, "unrecognized A_Expr kind: \"%.*s\"", length, token); @@ -541,7 +541,7 @@ _readA_Expr(void) } static ExtensibleNode * -_readExtensibleNode(void) +_readExtensibleNode(ReadNodeContext *ctx) { const ExtensibleNodeMethods *methods; ExtensibleNode *local_node; @@ -549,8 +549,8 @@ _readExtensibleNode(void) READ_TEMP_LOCALS(); - token = pg_strtok(&length); /* skip :extnodename */ - token = pg_strtok(&length); /* get extnodename */ + token = pg_strtok(ctx, &length); /* skip :extnodename */ + token = pg_strtok(ctx, &length); /* get extnodename */ extnodename = nullable_string(token, length); if (!extnodename) @@ -562,7 +562,7 @@ _readExtensibleNode(void) local_node->extnodename = extnodename; /* deserialize the private fields */ - methods->nodeRead(local_node); + methods->nodeRead(ctx, local_node); READ_DONE(); } @@ -573,18 +573,16 @@ _readExtensibleNode(void) * * Given a character string representing a node tree, parseNodeString creates * the internal node structure. - * - * The string to be read must already have been loaded into pg_strtok(). */ Node * -parseNodeString(void) +parseNodeString(ReadNodeContext *ctx) { READ_TEMP_LOCALS(); /* Guard against stack overflow due to overly complex expressions */ check_stack_depth(); - token = pg_strtok(&length); + token = pg_strtok(ctx, &length); #define MATCH(tokname, namelen) \ (length == namelen && memcmp(token, tokname, namelen) == 0) @@ -604,7 +602,7 @@ parseNodeString(void) * so we must be told that. */ Datum -readDatum(bool typbyval) +readDatum(ReadNodeContext *ctx, bool typbyval) { Size length; int tokenLength; @@ -615,10 +613,10 @@ readDatum(bool typbyval) /* * read the actual length of the value */ - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); length = atoui(token); - token = pg_strtok(&tokenLength); /* read the '[' */ + token = pg_strtok(ctx, &tokenLength); /* read the '[' */ if (token == NULL || token[0] != '[') elog(ERROR, "expected \"[\" to start datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -631,7 +629,7 @@ readDatum(bool typbyval) s = (char *) (&res); for (Size i = 0; i < (Size) sizeof(Datum); i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } } @@ -642,13 +640,13 @@ readDatum(bool typbyval) s = (char *) palloc(length); for (Size i = 0; i < length; i++) { - token = pg_strtok(&tokenLength); + token = pg_strtok(ctx, &tokenLength); s[i] = (char) atoi(token); } res = PointerGetDatum(s); } - token = pg_strtok(&tokenLength); /* read the ']' */ + token = pg_strtok(ctx, &tokenLength); /* read the ']' */ if (token == NULL || token[0] != ']') elog(ERROR, "expected \"]\" to end datum, but got \"%s\"; length = %zu", token ? token : "[NULL]", length); @@ -666,11 +664,11 @@ readDatum(bool typbyval) */ #define READ_SCALAR_ARRAY(fnname, datatype, convfunc) \ datatype * \ -fnname(int numCols) \ +fnname(ReadNodeContext *ctx, int numCols) \ { \ datatype *vals; \ READ_TEMP_LOCALS(); \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL) \ elog(ERROR, "incomplete scalar array"); \ if (length == 0) \ @@ -680,12 +678,12 @@ fnname(int numCols) \ vals = (datatype *) palloc(numCols * sizeof(datatype)); \ for (int i = 0; i < numCols; i++) \ { \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || token[0] == ')') \ elog(ERROR, "incomplete scalar array"); \ vals[i] = convfunc(token); \ } \ - token = pg_strtok(&length); \ + token = pg_strtok(ctx, &length); \ if (token == NULL || length != 1 || token[0] != ')') \ elog(ERROR, "incomplete scalar array"); \ return vals; \ diff --git a/src/include/nodes/extensible.h b/src/include/nodes/extensible.h index 517db95c4a3..b4fc6067807 100644 --- a/src/include/nodes/extensible.h +++ b/src/include/nodes/extensible.h @@ -19,6 +19,7 @@ #include "nodes/execnodes.h" #include "nodes/pathnodes.h" #include "nodes/plannodes.h" +#include "nodes/readfuncs.h" /* maximum length of an extensible node identifier */ #define EXTNODENAME_MAX_LEN 64 @@ -54,7 +55,7 @@ typedef struct ExtensibleNode * * nodeRead is a deserialization function for the node type. It does not need * to read type or extnodename; the core system handles those. It should fetch - * the next token using pg_strtok() from the current input stream, and then + * the next token from the ReadNodeContext using pg_strtok(ctx), and then * reconstruct the private fields according to the manner in readfuncs.c. * * All callbacks are mandatory. @@ -69,7 +70,8 @@ typedef struct ExtensibleNodeMethods const struct ExtensibleNode *b); void (*nodeOut) (struct StringInfoData *str, const struct ExtensibleNode *node); - void (*nodeRead) (struct ExtensibleNode *node); + void (*nodeRead) (ReadNodeContext *ctx, + struct ExtensibleNode *node); } ExtensibleNodeMethods; extern void RegisterExtensibleNodeMethods(const ExtensibleNodeMethods *methods); diff --git a/src/include/nodes/readfuncs.h b/src/include/nodes/readfuncs.h index 8ee2ddcd5c8..402ea147374 100644 --- a/src/include/nodes/readfuncs.h +++ b/src/include/nodes/readfuncs.h @@ -17,28 +17,36 @@ #include "nodes/nodes.h" /* - * variable in read.c that needs to be accessible to readfuncs.c + * ReadNodeContext - Context data for node deserialization + * + * This struct holds the state and some configuration of the deserializer. */ +typedef struct ReadNodeContext +{ + /* the string that's being parsed */ + const char *str; #ifdef DEBUG_NODE_TESTS_ENABLED -extern PGDLLIMPORT bool restore_location_fields; + /* state flag determining how readfuncs.c should treat location fields */ + bool restore_location_fields; #endif +} ReadNodeContext; /* * prototypes for functions in read.c (the lisp token parser) */ -extern const char *pg_strtok(int *length); +extern const char *pg_strtok(ReadNodeContext *ctx, int *length); extern char *debackslash(const char *token, int length); -extern void *nodeRead(const char *token, int tok_len); +extern void *nodeRead(ReadNodeContext *ctx, const char *token, int tok_len); /* * prototypes for functions in readfuncs.c */ -extern Node *parseNodeString(void); -extern struct Bitmapset *readBitmapset(void); -extern Datum readDatum(bool typbyval); -extern bool *readBoolCols(int numCols); -extern int *readIntCols(int numCols); -extern Oid *readOidCols(int numCols); -extern int16 *readAttrNumberCols(int numCols); +extern Node *parseNodeString(ReadNodeContext *ctx); +extern struct Bitmapset *readBitmapset(ReadNodeContext *ctx); +extern Datum readDatum(ReadNodeContext *ctx, bool typbyval); +extern bool *readBoolCols(ReadNodeContext *ctx, int numCols); +extern int *readIntCols(ReadNodeContext *ctx, int numCols); +extern Oid *readOidCols(ReadNodeContext *ctx, int numCols); +extern int16 *readAttrNumberCols(ReadNodeContext *ctx, int numCols); #endif /* READFUNCS_H */ -- 2.50.1 (Apple Git-155)
v4-0001-Refactor-Move-read-functions-to-readfuncs.h.patch (application/octet-stream, 2.0K) download From ee0936741f93c5f33bacf5f48470bcbae2eb25f6 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Sat, 22 Aug 2026 20:48:39 +0200 Subject: [PATCH v4 1/2] Refactor: Move read* functions to readfuncs.h This removes the need to expose new Node read internals in the general node.h infrastructure in later commits; it'll be contained to readfuncs.h only. Suggested-by: Michael Paquier <michael@paquier.xyz> --- src/include/nodes/nodes.h | 8 +------- src/include/nodes/readfuncs.h | 6 ++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 372eee20680..e6fe8577997 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -205,18 +205,12 @@ extern char *nodeToStringWithLocations(const void *obj); extern char *bmsToString(const struct Bitmapset *bms); /* - * nodes/{readfuncs.c,read.c} + * nodes/read.c */ extern void *stringToNode(const char *str); #ifdef DEBUG_NODE_TESTS_ENABLED extern void *stringToNodeWithLocations(const char *str); #endif -extern struct Bitmapset *readBitmapset(void); -extern Datum readDatum(bool typbyval); -extern bool *readBoolCols(int numCols); -extern int *readIntCols(int numCols); -extern Oid *readOidCols(int numCols); -extern int16 *readAttrNumberCols(int numCols); /* * nodes/copyfuncs.c diff --git a/src/include/nodes/readfuncs.h b/src/include/nodes/readfuncs.h index 0a5abdd9691..8ee2ddcd5c8 100644 --- a/src/include/nodes/readfuncs.h +++ b/src/include/nodes/readfuncs.h @@ -34,5 +34,11 @@ extern void *nodeRead(const char *token, int tok_len); * prototypes for functions in readfuncs.c */ extern Node *parseNodeString(void); +extern struct Bitmapset *readBitmapset(void); +extern Datum readDatum(bool typbyval); +extern bool *readBoolCols(int numCols); +extern int *readIntCols(int numCols); +extern Oid *readOidCols(int numCols); +extern int16 *readAttrNumberCols(int numCols); #endif /* READFUNCS_H */ -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-21 15:36           ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 23:47             ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-22 20:16               ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-08-22 21:38                 ` Michael Paquier <michael@paquier.xyz>
  2026-08-24 03:22                   ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 14+ messages in thread

From: Michael Paquier @ 2026-08-22 21:38 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Sat, Aug 22, 2026 at 10:16:44PM +0200, Matthias van de Meent wrote:
> I've moved them to readfuncs.h, which I think is a reasonable
> compromise; avoiding the forward declaration whilst still exposing
> these functions to extensions that might need them.

The forward declaration feeling unnecessary was my main grip, so I am
fine with the suggestion of moving them to readfuncs.c.

> There's a "shardman" repo that seems to directly use readOidCols:
> https://github.com/ZhaoDiankui/shardman/blob/merge/src/exchange.c#L162

Noted.

> Apache AGE seems to use (or at least, reference) our read*Cols
> definitions in their backend/nodes/cypher_readfuncs.c; in copied
> macros that don't currently seem to have any usage but might get used
> in the future. Its READ_BITMAP_FIELD macro won't work as-is (it
> references _readBitmap, which is the private/static version, whereas
> readBitmap is the exposed version).

I saw this one, but it just looked like a copy-paste of the
readfuncs.c code to me, not a legit call.

>  * new 0001 patch, which moves the read* functions from nodes.h to readfuncs.h.
>     As was discussed upthread. 0002 is adjusted accordingly.

OK for this one here.

>  * nodes/extensible.h now includes nodes/readfuncs.h
>     I revised my opinion on this: I noticed my compiler complained
> about the plain "struct ReadNodeContext *ctx" argument if I didn't put
> a "struct ReadNodeContext;" declaration at the top level of the file,
> which (when combined with relevant comments) would add more verbose
> clutter than the alternative of including nodes/readfuncs.h.

Avoiding the struct markers when we can is a better practice to me.
Digression: We still have a few places in the code where we have these
markers, I think, perhaps they could be cleaned up..

>  * adjusted comment on parseNodeString, as reported upthread.

Thanks.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-21 15:36           ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 23:47             ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-22 20:16               ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-22 21:38                 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
@ 2026-08-24 03:22                   ` Michael Paquier <michael@paquier.xyz>
  2026-08-24 10:49                     ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Michael Paquier @ 2026-08-24 03:22 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Sun, Aug 23, 2026 at 06:38:58AM +0900, Michael Paquier wrote:
>>  * nodes/extensible.h now includes nodes/readfuncs.h
>>     I revised my opinion on this: I noticed my compiler complained
>> about the plain "struct ReadNodeContext *ctx" argument if I didn't put
>> a "struct ReadNodeContext;" declaration at the top level of the file,
>> which (when combined with relevant comments) would add more verbose
>> clutter than the alternative of including nodes/readfuncs.h.

89d091f602fa and 9673a0aa92f4 are now applied, for both changes.

Thanks for this work, Matthias.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-17 11:36   ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-19 08:46     ` Re: Thread-safe stringToNode() / pg_strtok() Peter Eisentraut <peter@eisentraut.org>
  2026-08-19 13:13       ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 04:41         ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-21 15:36           ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-21 23:47             ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-22 20:16               ` Re: Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-22 21:38                 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
  2026-08-24 03:22                   ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
@ 2026-08-24 10:49                     ` Matthias van de Meent <boekewurm+postgres@gmail.com>
  0 siblings, 0 replies; 14+ messages in thread

From: Matthias van de Meent @ 2026-08-24 10:49 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Peter Eisentraut <peter@eisentraut.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>; 邱宇航 <iamqyh@gmail.com>

On Mon, 24 Aug 2026 at 05:22, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Sun, Aug 23, 2026 at 06:38:58AM +0900, Michael Paquier wrote:
> >>  * nodes/extensible.h now includes nodes/readfuncs.h
> >>     I revised my opinion on this: I noticed my compiler complained
> >> about the plain "struct ReadNodeContext *ctx" argument if I didn't put
> >> a "struct ReadNodeContext;" declaration at the top level of the file,
> >> which (when combined with relevant comments) would add more verbose
> >> clutter than the alternative of including nodes/readfuncs.h.
>
> 89d091f602fa and 9673a0aa92f4 are now applied, for both changes.
>
> Thanks for this work, Matthias.

Thanks for all the feedback and committing, Michael!


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Thread-safe stringToNode() / pg_strtok()
  2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-08-16 07:13 ` Re: Thread-safe stringToNode() / pg_strtok() Michael Paquier <michael@paquier.xyz>
@ 2026-08-19 13:19   ` Andres Freund <andres@anarazel.de>
  1 sibling, 0 replies; 14+ messages in thread

From: Andres Freund @ 2026-08-19 13:19 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Heikki Linnakangas <hlinnaka@iki.fi>

Hi,

On 2026-08-16 16:13:20 +0900, Michael Paquier wrote:
> On Fri, Aug 14, 2026 at 04:08:24PM +0200, Matthias van de Meent wrote:
> > There are other things we could do to improve this code (e.g.
> > de/serializaing based on node descriptors instead of generated
> > functions), but I consider such improvements to be out of scope for
> > this patch; the attached is just a simple move from global variables
> > to a context struct passed down the call stack.
> 
> That sounds fair.
> 
> Any thoughts or comments from others?

Tackling those things separately makes a lot of sense to me. Not using static
variables for the node infrastructure also makes a lof of sense, I find APIs
like pg_strtok() quite terrible, even disregarding thread safety.

Greetings,

Andres Freund






^ permalink  raw  reply  [nested|flat] 14+ messages in thread


end of thread, other threads:[~2026-08-24 10:49 UTC | newest]

Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-14 14:08 Thread-safe stringToNode() / pg_strtok() Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-08-16 07:13 ` Michael Paquier <michael@paquier.xyz>
2026-08-17 11:36   ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-08-18 03:38     ` 邱宇航 <iamqyh@gmail.com>
2026-08-19 08:46     ` Peter Eisentraut <peter@eisentraut.org>
2026-08-19 13:13       ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-08-21 04:41         ` Michael Paquier <michael@paquier.xyz>
2026-08-21 15:36           ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-08-21 23:47             ` Michael Paquier <michael@paquier.xyz>
2026-08-22 20:16               ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-08-22 21:38                 ` Michael Paquier <michael@paquier.xyz>
2026-08-24 03:22                   ` Michael Paquier <michael@paquier.xyz>
2026-08-24 10:49                     ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-08-19 13:19   ` Andres Freund <andres@anarazel.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox