Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d20d8fb

Browse files
committed
Do not output actual value of location fields in node serialization by default
This changes nodeToString() to not output the actual value of location fields in nodes, but instead it writes -1. This mirrors the fact that stringToNode() also does not read location field values but always stores -1. For most uses of nodeToString(), which is to store nodes in catalog fields, this is more useful. We don't store original query texts in catalogs, so any lingering query location values are not meaningful. For debugging purposes, there is a new nodeToStringWithLocations(), which mirrors the existing stringToNodeWithLocations(). This is used for WRITE_READ_PARSE_PLAN_TREES and nodes/print.c functions, which covers all the debugging uses. Reviewed-by: Matthias van de Meent <boekewurm+postgres@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/CAEze2WgrCiR3JZmWyB0YTc8HV7ewRdx13j0CqD6mVkYAW+SFGQ@mail.gmail.com
1 parent 6ae701b commit d20d8fb

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

src/backend/nodes/outfuncs.c

+34-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "nodes/pg_list.h"
2626
#include "utils/datum.h"
2727

28+
/* State flag that determines how nodeToStringInternal() should treat location fields */
29+
static bool write_location_fields = false;
30+
2831
static void outChar(StringInfo str, char c);
2932
static void outDouble(StringInfo str, double d);
3033

@@ -88,7 +91,7 @@ static void outDouble(StringInfo str, double d);
8891

8992
/* Write a parse location field (actually same as INT case) */
9093
#define WRITE_LOCATION_FIELD(fldname) \
91-
appendStringInfo(str, " :" CppAsString(fldname) " %d", node->fldname)
94+
appendStringInfo(str, " :" CppAsString(fldname) " %d", write_location_fields ? node->fldname : -1)
9295

9396
/* Write a Node field */
9497
#define WRITE_NODE_FIELD(fldname) \
@@ -757,18 +760,46 @@ outNode(StringInfo str, const void *obj)
757760
/*
758761
* nodeToString -
759762
* returns the ascii representation of the Node as a palloc'd string
763+
*
764+
* write_loc_fields determines whether location fields are output with their
765+
* actual value rather than -1. The actual value can be useful for debugging,
766+
* but for most uses, the actual value is not useful, since the original query
767+
* string is no longer available.
760768
*/
761-
char *
762-
nodeToString(const void *obj)
769+
static char *
770+
nodeToStringInternal(const void *obj, bool write_loc_fields)
763771
{
764772
StringInfoData str;
773+
bool save_write_location_fields;
774+
775+
save_write_location_fields = write_location_fields;
776+
write_location_fields = write_loc_fields;
765777

766778
/* see stringinfo.h for an explanation of this maneuver */
767779
initStringInfo(&str);
768780
outNode(&str, obj);
781+
782+
write_location_fields = save_write_location_fields;
783+
769784
return str.data;
770785
}
771786

787+
/*
788+
* Externally visible entry points
789+
*/
790+
char *
791+
nodeToString(const void *obj)
792+
{
793+
return nodeToStringInternal(obj, false);
794+
}
795+
796+
char *
797+
nodeToStringWithLocations(const void *obj)
798+
{
799+
return nodeToStringInternal(obj, true);
800+
}
801+
802+
772803
/*
773804
* bmsToString -
774805
* returns the ascii representation of the Bitmapset as a palloc'd string

src/backend/nodes/print.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ print(const void *obj)
3838
char *s;
3939
char *f;
4040

41-
s = nodeToString(obj);
41+
s = nodeToStringWithLocations(obj);
4242
f = format_node_dump(s);
4343
pfree(s);
4444
printf("%s\n", f);
@@ -56,7 +56,7 @@ pprint(const void *obj)
5656
char *s;
5757
char *f;
5858

59-
s = nodeToString(obj);
59+
s = nodeToStringWithLocations(obj);
6060
f = pretty_format_node_dump(s);
6161
pfree(s);
6262
printf("%s\n", f);
@@ -74,7 +74,7 @@ elog_node_display(int lev, const char *title, const void *obj, bool pretty)
7474
char *s;
7575
char *f;
7676

77-
s = nodeToString(obj);
77+
s = nodeToStringWithLocations(obj);
7878
if (pretty)
7979
f = pretty_format_node_dump(s);
8080
else

src/backend/tcop/postgres.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ pg_parse_query(const char *query_string)
641641
*/
642642
#ifdef WRITE_READ_PARSE_PLAN_TREES
643643
{
644-
char *str = nodeToString(raw_parsetree_list);
644+
char *str = nodeToStringWithLocations(raw_parsetree_list);
645645
List *new_list = stringToNodeWithLocations(str);
646646

647647
pfree(str);
@@ -849,7 +849,7 @@ pg_rewrite_query(Query *query)
849849
foreach(lc, querytree_list)
850850
{
851851
Query *curr_query = lfirst_node(Query, lc);
852-
char *str = nodeToString(curr_query);
852+
char *str = nodeToStringWithLocations(curr_query);
853853
Query *new_query = stringToNodeWithLocations(str);
854854

855855
/*
@@ -931,7 +931,7 @@ pg_plan_query(Query *querytree, const char *query_string, int cursorOptions,
931931
char *str;
932932
PlannedStmt *new_plan;
933933

934-
str = nodeToString(plan);
934+
str = nodeToStringWithLocations(plan);
935935
new_plan = stringToNodeWithLocations(str);
936936
pfree(str);
937937

src/include/nodes/nodes.h

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ extern void outBitmapset(struct StringInfoData *str,
195195
extern void outDatum(struct StringInfoData *str, uintptr_t value,
196196
int typlen, bool typbyval);
197197
extern char *nodeToString(const void *obj);
198+
extern char *nodeToStringWithLocations(const void *obj);
198199
extern char *bmsToString(const struct Bitmapset *bms);
199200

200201
/*

0 commit comments

Comments
 (0)