diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-07-20 13:04:33 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-07-20 13:04:33 -0400 |
commit | 2d04277121f2f8e18b65190f0dfa8ca97e9c29cb (patch) | |
tree | 7e71eab88b213e02699d836ece9adadbcbb81896 /src/backend/nodes/outfuncs.c | |
parent | f77ff083350eb5a2625a4dbfca61d15b66c4918b (diff) | |
download | postgresql-2d04277121f2f8e18b65190f0dfa8ca97e9c29cb.tar.gz postgresql-2d04277121f2f8e18b65190f0dfa8ca97e9c29cb.zip |
Make serialization of Nodes' scalar-array fields more robust.
When the ability to print variable-length-array fields was first
added to outfuncs.c, there was no corresponding read capability,
as it was used only for debug dumps of planner-internal Nodes.
Not a lot of thought seems to have been put into the output format:
it's just the space-separated array elements and nothing else.
Later such fields appeared in Plan nodes, and still later we grew
read support so that Plans could be transferred to parallel workers,
but the original text format wasn't rethought. It seems inadequate
to me because (a) no cross-check is possible that we got the right
number of array entries, (b) we can't tell the difference between
a NULL pointer and a zero-length array, and (c) except for
WRITE_INDEX_ARRAY, we'd crash if a non-zero length is specified
when the pointer is NULL, a situation that can arise in some fields
that we currently conveniently avoid printing.
Since we're currently in a campaign to make the Node infrastructure
generally more it-just-works-without-thinking-about-it, now seems
like a good time to improve this.
Let's adopt a format similar to that used for Lists, that is "<>"
for a NULL pointer or "(item item item)" otherwise. Also retool
the code to not have so many copies of the identical logic.
I bumped catversion out of an abundance of caution, although I think
that we don't use any such array fields in Nodes that can get into
the catalogs.
Discussion: https://postgr.es/m/1528424.1658272135@sss.pgh.pa.us
Diffstat (limited to 'src/backend/nodes/outfuncs.c')
-rw-r--r-- | src/backend/nodes/outfuncs.c | 81 |
1 files changed, 48 insertions, 33 deletions
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 2b85f97f398..4077fc4498d 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -16,6 +16,7 @@ #include <ctype.h> +#include "access/attnum.h" #include "lib/stringinfo.h" #include "miscadmin.h" #include "nodes/bitmapset.h" @@ -96,48 +97,30 @@ static void outChar(StringInfo str, char c); (appendStringInfoString(str, " :" CppAsString(fldname) " "), \ outBitmapset(str, node->fldname)) +/* Write a variable-length array of AttrNumber */ #define WRITE_ATTRNUMBER_ARRAY(fldname, len) \ - do { \ - appendStringInfoString(str, " :" CppAsString(fldname) " "); \ - for (int i = 0; i < len; i++) \ - appendStringInfo(str, " %d", node->fldname[i]); \ - } while(0) + (appendStringInfoString(str, " :" CppAsString(fldname) " "), \ + writeAttrNumberCols(str, node->fldname, len)) +/* Write a variable-length array of Oid */ #define WRITE_OID_ARRAY(fldname, len) \ - do { \ - appendStringInfoString(str, " :" CppAsString(fldname) " "); \ - for (int i = 0; i < len; i++) \ - appendStringInfo(str, " %u", node->fldname[i]); \ - } while(0) + (appendStringInfoString(str, " :" CppAsString(fldname) " "), \ + writeOidCols(str, node->fldname, len)) -/* - * This macro supports the case that the field is NULL. For the other array - * macros, that is currently not needed. - */ +/* Write a variable-length array of Index */ #define WRITE_INDEX_ARRAY(fldname, len) \ - do { \ - appendStringInfoString(str, " :" CppAsString(fldname) " "); \ - if (node->fldname) \ - for (int i = 0; i < len; i++) \ - appendStringInfo(str, " %u", node->fldname[i]); \ - else \ - appendStringInfoString(str, "<>"); \ - } while(0) + (appendStringInfoString(str, " :" CppAsString(fldname) " "), \ + writeIndexCols(str, node->fldname, len)) +/* Write a variable-length array of int */ #define WRITE_INT_ARRAY(fldname, len) \ - do { \ - appendStringInfoString(str, " :" CppAsString(fldname) " "); \ - for (int i = 0; i < len; i++) \ - appendStringInfo(str, " %d", node->fldname[i]); \ - } while(0) + (appendStringInfoString(str, " :" CppAsString(fldname) " "), \ + writeIntCols(str, node->fldname, len)) +/* Write a variable-length array of bool */ #define WRITE_BOOL_ARRAY(fldname, len) \ - do { \ - appendStringInfoString(str, " :" CppAsString(fldname) " "); \ - for (int i = 0; i < len; i++) \ - appendStringInfo(str, " %s", booltostr(node->fldname[i])); \ - } while(0) - + (appendStringInfoString(str, " :" CppAsString(fldname) " "), \ + writeBoolCols(str, node->fldname, len)) #define booltostr(x) ((x) ? "true" : "false") @@ -196,6 +179,38 @@ outChar(StringInfo str, char c) outToken(str, in); } +/* + * common implementation for scalar-array-writing functions + * + * The data format is either "<>" for a NULL pointer or "(item item item)". + * fmtstr must include a leading space, and the rest of it must produce + * something that will be seen as a single simple token by pg_strtok(). + * convfunc can be empty, or the name of a conversion macro or function. + */ +#define WRITE_SCALAR_ARRAY(fnname, datatype, fmtstr, convfunc) \ +static void \ +fnname(StringInfo str, const datatype *arr, int len) \ +{ \ + if (arr != NULL) \ + { \ + appendStringInfoChar(str, '('); \ + for (int i = 0; i < len; i++) \ + appendStringInfo(str, fmtstr, convfunc(arr[i])); \ + appendStringInfoChar(str, ')'); \ + } \ + else \ + appendStringInfoString(str, "<>"); \ +} + +WRITE_SCALAR_ARRAY(writeAttrNumberCols, AttrNumber, " %d",) +WRITE_SCALAR_ARRAY(writeOidCols, Oid, " %u",) +WRITE_SCALAR_ARRAY(writeIndexCols, Index, " %u",) +WRITE_SCALAR_ARRAY(writeIntCols, int, " %d",) +WRITE_SCALAR_ARRAY(writeBoolCols, bool, " %s", booltostr) + +/* + * Print a List. + */ static void _outList(StringInfo str, const List *node) { |