-
Notifications
You must be signed in to change notification settings - Fork 105
/
update_copied_funcs.pl
executable file
·332 lines (299 loc) · 8.92 KB
/
update_copied_funcs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#! /usr/bin/perl
use strict;
my $srcpath;
my @sources = (
'src/backend/optimizer/path/allpaths.c',
'src/backend/optimizer/path/joinrels.c');
my %defs =
('core.c'
=> {protos => [],
funcs => ['set_plain_rel_pathlist',
'standard_join_search',
'create_plain_partial_paths',
'join_search_one_level',
'make_rels_by_clause_joins',
'make_rels_by_clauseless_joins',
'join_is_legal',
'has_join_restriction',
'restriction_is_constant_false',
'build_child_join_sjinfo',
'get_matching_part_pairs',
'compute_partition_bounds',
'try_partitionwise_join',
'free_child_join_sjinfo'],
head => core_c_head()},
'make_join_rel.c'
=> {protos => [],
funcs => ['make_join_rel',
'populate_joinrel_with_paths'],
head => make_join_rel_head()});
open (my $in, '-|', "objdump -W `which postgres`") || die "failed to objdump";
while (<$in>)
{
if (/DW_AT_comp_dir .*: (.*\/)src\/backend\//)
{
$srcpath = $1;
last;
}
}
close($in);
die "source path not found" if (! defined $srcpath);
#printf("Source path = %s\n", $srcpath);
my %protos;
my %funcs;
my %func_is_static;
my %func_source;
for my $fname (@sources)
{
my $f = $srcpath.$fname;
my $source;
open ($in, '<', $f) || die "failed to open $f: $!";
while (<$in>)
{
$source .= $_;
}
## Collect static prototypes
while ($source =~ /\n(static [^\(\)\{\}]*?(\w+)(\([^\{\);]+?\);))/gsm)
{
# print "Prototype found: $2\n";
$protos{$2} = $1;
}
## Collect function bodies
while ($source =~ /(\n\/\*\n.+?\*\/\n(static )?(.+?)\n(.+?) *\(.*?\)\n\{.+?\n\}\n)/gsm)
{
$funcs{$4} = $1;
$func_is_static{$4} = (defined $2);
$func_source{$4} = $fname;
# printf("Function found: %s$4\n", $func_is_static{$4} ? "static " : "");
}
close($in);
}
# Generate files
for my $fname (keys %defs)
{
my %d = %{$defs{$fname}};
my @protonames = @{$d{'protos'}};
my @funcnames = @{$d{'funcs'}};
my $head = $d{'head'};
print "Generate $fname.\n";
open (my $out, '>', $fname) || die "could not open $fname: $!";
print $out $head;
for (@protonames)
{
print " Prototype: $_\n";
print $out "\n";
die "Prototype for $_ not found" if (! defined $protos{$_});
print $out $protos{$_};
}
for (@funcnames)
{
printf(" %s function: $_@%s\n",
$func_is_static{$_}?"static":"public", $func_source{$_});
print $out "\n";
die "Function body for $_ not found" if (! defined $funcs{$_});
print $out $funcs{$_};
}
close($out);
}
# modify make_join_rel.c
patch_make_join_rel();
sub core_c_head()
{
return << "EOS";
/*-------------------------------------------------------------------------
*
* core.c
* Routines copied from PostgreSQL core distribution.
*
* The main purpose of this files is having access to static functions in core.
* Another purpose is tweaking functions behavior by replacing part of them by
* macro definitions. See at the end of pg_hint_plan.c for details. Anyway,
* this file *must* contain required functions without making any change.
*
* This file contains the following functions from corresponding files.
*
* src/backend/optimizer/path/allpaths.c
*
* public functions:
* standard_join_search(): This funcion is not static. The reason for
* including this function is make_rels_by_clause_joins. In order to
* avoid generating apparently unwanted join combination, we decided to
* change the behavior of make_join_rel, which is called under this
* function.
*
* static functions:
* set_plain_rel_pathlist()
* create_plain_partial_paths()
*
* src/backend/optimizer/path/joinrels.c
*
* public functions:
* join_search_one_level(): We have to modify this to call my definition of
* make_rels_by_clause_joins.
*
* static functions:
* make_rels_by_clause_joins()
* make_rels_by_clauseless_joins()
* join_is_legal()
* has_join_restriction()
* restriction_is_constant_false()
* build_child_join_sjinfo()
* get_matching_part_pairs()
* compute_partition_bounds()
* try_partitionwise_join()
*
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*-------------------------------------------------------------------------
*/
#include "access/tsmapi.h"
#include "catalog/pg_operator.h"
#include "foreign/fdwapi.h"
EOS
}
sub make_join_rel_head
{
return << "EOS";
/*-------------------------------------------------------------------------
*
* make_join_rel.c
* Routines copied from PostgreSQL core distribution with some
* modifications.
*
* src/backend/optimizer/path/joinrels.c
*
* This file contains the following functions from corresponding files.
*
* static functions:
* make_join_rel()
* populate_joinrel_with_paths()
*
* Portions Copyright (c) 2013-2024, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*-------------------------------------------------------------------------
*/
/*
* adjust_rows: tweak estimated row numbers according to the hint.
*/
static double
adjust_rows(double rows, RowsHint *hint)
{
double result = 0.0; /* keep compiler quiet */
if (hint->value_type == RVT_ABSOLUTE)
result = hint->rows;
else if (hint->value_type == RVT_ADD)
result = rows + hint->rows;
else if (hint->value_type == RVT_SUB)
result = rows - hint->rows;
else if (hint->value_type == RVT_MULTI)
result = rows * hint->rows;
else
Assert(false); /* unrecognized rows value type */
hint->base.state = HINT_STATE_USED;
if (result < 1.0)
ereport(WARNING,
(errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
hint->base.hint_str)));
result = clamp_row_est(result);
elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
return result;
}
EOS
}
sub patch_make_join_rel
{
open(my $out, '|-', 'patch') || die "failed to open pipe: $!";
print $out <<"EOS";
diff --git b/make_join_rel.c a/make_join_rel.c
index 0e7b99f..287e7f1 100644
--- b/make_join_rel.c
+++ a/make_join_rel.c
@@ -126,6 +126,84 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
&restrictlist);
+ /* !!! START: HERE IS THE PART WHICH IS ADDED FOR PG_HINT_PLAN !!! */
+ {
+ RowsHint *rows_hint = NULL;
+ int i;
+ RowsHint *justforme = NULL;
+ RowsHint *domultiply = NULL;
+
+ /* Search for applicable rows hint for this join node */
+ for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_ROWS]; i++)
+ {
+ rows_hint = current_hint_state->rows_hints[i];
+
+ /*
+ * Skip this rows_hint if it is invalid from the first or it
+ * doesn't target any join rels.
+ */
+ if (!rows_hint->joinrelids ||
+ rows_hint->base.state == HINT_STATE_ERROR)
+ continue;
+
+ if (bms_equal(joinrelids, rows_hint->joinrelids))
+ {
+ /*
+ * This joinrel is just the target of this rows_hint, so tweak
+ * rows estimation according to the hint.
+ */
+ justforme = rows_hint;
+ }
+ else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
+ bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
+ bms_is_subset(rows_hint->joinrelids, joinrelids) &&
+ rows_hint->value_type == RVT_MULTI)
+ {
+ /*
+ * If the rows_hint's target relids is not a subset of both of
+ * component rels and is a subset of this joinrel, ths hint's
+ * targets spread over both component rels. This menas that
+ * this hint has been never applied so far and this joinrel is
+ * the first (and only) chance to fire in current join tree.
+ * Only the multiplication hint has the cumulative nature so we
+ * apply only RVT_MULTI in this way.
+ */
+ domultiply = rows_hint;
+ }
+ }
+
+ if (justforme)
+ {
+ /*
+ * If a hint just for me is found, no other adjust method is
+ * useles, but this cannot be more than twice becuase this joinrel
+ * is already adjusted by this hint.
+ */
+ if (justforme->base.state == HINT_STATE_NOTUSED)
+ joinrel->rows = adjust_rows(joinrel->rows, justforme);
+ }
+ else
+ {
+ if (domultiply)
+ {
+ /*
+ * If we have multiple routes up to this joinrel which are not
+ * applicable this hint, this multiply hint will applied more
+ * than twice. But there's no means to know of that,
+ * re-estimate the row number of this joinrel always just
+ * before applying the hint. This is a bit different from
+ * normal planner behavior but it doesn't harm so much.
+ */
+ set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
+ restrictlist);
+
+ joinrel->rows = adjust_rows(joinrel->rows, domultiply);
+ }
+
+ }
+ }
+ /* !!! END: HERE IS THE PART WHICH IS ADDED FOR PG_HINT_PLAN !!! */
+
/*
* If we've already proven this join is empty, we needn't consider any
* more paths for it.
EOS
}