@@ -1735,9 +1735,14 @@ get_part_expression_info(Oid relid, const char *expr_string,
1735
1735
List * querytree_list ;
1736
1736
PlannedStmt * plan ;
1737
1737
TargetEntry * target_entry ;
1738
+ MemoryContext pathman_parse_context , oldcontext ;
1738
1739
1739
1740
expr_info = palloc (sizeof (PartExpressionInfo ));
1740
1741
1742
+ pathman_parse_context = AllocSetContextCreate (TopMemoryContext ,
1743
+ "pathman parse context" ,
1744
+ ALLOCSET_DEFAULT_SIZES );
1745
+
1741
1746
/* Keep raw expression */
1742
1747
expr_info -> raw_expr = get_raw_expression (relid , expr_string ,
1743
1748
& query_string , & parsetree );
@@ -1746,6 +1751,12 @@ get_part_expression_info(Oid relid, const char *expr_string,
1746
1751
/* We don't need pathman activity initialization for this relation yet */
1747
1752
pathman_hooks_enabled = false;
1748
1753
1754
+ /* We use separate memory context here, just to make sure we don't leave
1755
+ * anything behind after analyze and planning.
1756
+ * Parsed raw expression will stay in context of caller
1757
+ */
1758
+ oldcontext = MemoryContextSwitchTo (pathman_parse_context );
1759
+
1749
1760
/* This will fail with elog in case of wrong expression
1750
1761
* with more or less understable text */
1751
1762
querytree_list = pg_analyze_and_rewrite (parsetree ,
@@ -1776,11 +1787,13 @@ get_part_expression_info(Oid relid, const char *expr_string,
1776
1787
target_entry = lfirst (list_head (plan -> planTree -> targetlist ));
1777
1788
expr_node = (Node * ) target_entry -> expr ;
1778
1789
expr_node = eval_const_expressions (NULL , expr_node );
1790
+ out_string = nodeToString (expr_node );
1791
+
1792
+ MemoryContextSwitchTo (oldcontext );
1779
1793
1780
1794
/* Convert expression to string and return it as datum */
1781
- out_string = nodeToString (expr_node );
1782
1795
expr_info -> expr_datum = CStringGetTextDatum (out_string );
1783
- pfree ( out_string );
1796
+ MemoryContextReset ( pathman_parse_context );
1784
1797
1785
1798
end :
1786
1799
/* Enable pathman hooks */
0 commit comments