Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/perl
#----------------------------------------------------------------------
#
# Generate wait events support files from wait_event_names.txt:
# - wait_event_types.h (if --code is passed)
# - pgstat_wait_event.c (if --code is passed)
# - wait_event_funcs_data.c (if --code is passed)
# - wait_event_types.sgml (if --docs is passed)
#
# Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
# src/backend/utils/activity/generate-wait_event_types.pl
#
#----------------------------------------------------------------------
 
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
 
my $output_path = '.';
my $gen_docs = 0;
my $gen_code = 0;
 
my $continue = "\n";
my %hashwe;
 
GetOptions(
    'outdir:s' => \$output_path,
    'docs' => \$gen_docs,
    'code' => \$gen_code) || usage();
 
die "Needs to specify --docs or --code"
  if (!$gen_docs && !$gen_code);
 
die "Not possible to specify --docs and --code simultaneously"
  if ($gen_docs && $gen_code);
 
open my $wait_event_names, '<', $ARGV[0] or die;
 
my @abi_compatibility_lines;
my @lines;
my $abi_compatibility = 0;
my $section_name;
 
# Remove comments and empty lines and add waitclassname based on the section
while (<$wait_event_names>)
{
    chomp;
 
    # Skip comments
    next if /^#/;
 
    # Skip empty lines
    next if /^\s*$/;
 
    # Get waitclassname based on the section
    if (/^Section: ClassName(.*)/)
    {
        $section_name = $_;
        $section_name =~ s/^.*- //;
        $abi_compatibility = 0;
        next;
    }
 
    # ABI_compatibility region, preserving ABI compatibility of the code
    # generated.  Any wait events listed in this part of the file will
    # not be sorted during the code generation.
    if (/^ABI_compatibility:$/)
    {
        $abi_compatibility = 1;
        next;
    }
 
    if ($gen_code && $abi_compatibility)
    {
        push(@abi_compatibility_lines, $section_name . "\t" . $_);
    }
    else
    {
        push(@lines, $section_name . "\t" . $_);
    }
}
 
# Sort the lines based on the second column.
# uc() is being used to force the comparison to be case-insensitive.
my @lines_sorted =
  sort { uc((split(/\t+/, $a))[1]) cmp uc((split(/\t+/, $b))[1]) } @lines;
 
# If we are generating code, concat @lines_sorted and then
# @abi_compatibility_lines.
if ($gen_code)
{
    push(@lines_sorted, @abi_compatibility_lines);
}
 
# Read the sorted lines and populate the hash table
foreach my $line (@lines_sorted)
{
    die "unable to parse wait_event_names.txt for line $line\n"
      unless $line =~ /^(\w+)\t+(\w+)\t+("\w.*\.")$/;
 
    (my $waitclassname, my $waiteventname, my $waitevendocsentence) =
      ($1, $2, $3);
 
    # Generate the element name for the enums based on the
    # description.  The C symbols are prefixed with "WAIT_EVENT_".
    my $waiteventenumname = "WAIT_EVENT_$waiteventname";
 
    # Build the descriptions.  These are in camel-case.
    # LWLock and Lock classes do not need any modifications.
    my $waiteventdescription = '';
    if (   $waitclassname eq 'WaitEventLWLock'
        || $waitclassname eq 'WaitEventLock')
    {
        $waiteventdescription = $waiteventname;
    }
    else
    {
        my @waiteventparts = split("_", $waiteventname);
        foreach my $waiteventpart (@waiteventparts)
        {
            $waiteventdescription .= substr($waiteventpart, 0, 1)
              . lc(substr($waiteventpart, 1, length($waiteventpart)));
        }
    }
 
    # Store the event into the list for each class.
    my @waiteventlist =
      [ $waiteventenumname, $waiteventdescription, $waitevendocsentence ];
    push(@{ $hashwe{$waitclassname} }, @waiteventlist);
}
 
 
# Generate the .c and .h files.
if ($gen_code)
{
    # Include PID in suffix in case parallel make runs this script
    # multiple times.
    my $htmp = "$output_path/wait_event_types.h.tmp$$";
    my $ctmp = "$output_path/pgstat_wait_event.c.tmp$$";
    my $wctmp = "$output_path/wait_event_funcs_data.c.tmp$$";
    open my $h, '>', $htmp or die "Could not open $htmp: $!";
    open my $c, '>', $ctmp or die "Could not open $ctmp: $!";
    open my $wc, '>', $wctmp or die "Could not open $wctmp: $!";
 
    my $header_comment =
      '/*-------------------------------------------------------------------------
 *
 * %s
 *    Generated wait events infrastructure code
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * NOTES
 *  ******************************
 *  *** DO NOT EDIT THIS FILE! ***
 *  ******************************
 *
 *  It has been GENERATED by src/backend/utils/activity/generate-wait_event_types.pl
 *
 *-------------------------------------------------------------------------
 */
 
';
 
    printf $h $header_comment, 'wait_event_types.h';
    printf $h "#ifndef WAIT_EVENT_TYPES_H\n";
    printf $h "#define WAIT_EVENT_TYPES_H\n\n";
    printf $h "#include \"utils/wait_classes.h\"\n\n";
 
    printf $c $header_comment, 'pgstat_wait_event.c';
 
    printf $wc $header_comment, 'wait_event_funcs_data.c';
 
    # Generate the pgstat_wait_event.c and wait_event_types.h files
    # uc() is being used to force the comparison to be case-insensitive.
    foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe)
    {
        # Don't generate the pgstat_wait_event.c and wait_event_types.h files
        # for types handled independently.
        next
          if ( $waitclass eq 'WaitEventExtension'
            || $waitclass eq 'WaitEventInjectionPoint'
            || $waitclass eq 'WaitEventLWLock'
            || $waitclass eq 'WaitEventLock');
 
        my $last = $waitclass;
        $last =~ s/^WaitEvent//;
        my $lastuc = uc $last;
        my $lastlc = lc $last;
        my $firstpass = 1;
        my $pg_wait_class;
 
        printf $c
          "static const char *\npgstat_get_wait_$lastlc($waitclass w)\n{\n";
        printf $c "\tconst char *event_name = \"unknown wait event\";\n\n";
        printf $c "\tswitch (w)\n\t{\n";
 
        foreach my $wev (@{ $hashwe{$waitclass} })
        {
            if ($firstpass)
            {
                printf $h "typedef enum\n{\n";
                $pg_wait_class = "PG_WAIT_" . $lastuc;
                printf $h "\t%s = %s", $wev->[0], $pg_wait_class;
                $continue = ",\n";
            }
            else
            {
                printf $h "%s\t%s", $continue, $wev->[0];
                $continue = ",\n";
            }
            $firstpass = 0;
 
            printf $c "\t\t case %s:\n", $wev->[0];
            # Apply quotes to the wait event name string.
            printf $c "\t\t\t event_name = \"%s\";\n\t\t\t break;\n",
              $wev->[1];
        }
 
        printf $h "\n} $waitclass;\n\n";
 
        printf $c
          "\t\t\t /* no default case, so that compiler will warn */\n";
        printf $c "\t}\n\n";
        printf $c "\treturn event_name;\n";
        printf $c "}\n\n";
    }
 
    # Generate wait_event_funcs_data.c, building the contents of a static
    # C structure holding all the information about the wait events.
    # uc() is being used to force the comparison to be case-insensitive,
    # even though it is not required here.
    foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe)
    {
        my $last = $waitclass;
        $last =~ s/^WaitEvent//;
 
        foreach my $wev (@{ $hashwe{$waitclass} })
        {
            my $new_desc = substr $wev->[2], 1, -2;
            # Escape single quotes.
            $new_desc =~ s/'/\\'/g;
 
            # Replace the "quote" markups by real ones.
            $new_desc =~ s/<quote>(.*?)<\/quote>/\\"$1\\"/g;
 
            # Remove SGML markups.
            $new_desc =~ s/<.*?>(.*?)<.*?>/$1/g;
 
            # Tweak contents about links <xref linkend="text"/>
            # on GUCs,
            while (my ($capture) =
                $new_desc =~ m/<xref linkend="guc-(.*?)"\/>/g)
            {
                $capture =~ s/-/_/g;
                $new_desc =~ s/<xref linkend="guc-.*?"\/>/$capture/g;
            }
            # Then remove any reference to
            # "see <xref linkend="text"/>".
            $new_desc =~ s/; see.*$//;
 
            # Build one element of the C structure holding the
            # wait event info, as of (type, name, description).
            printf $wc "\t{\"%s\", \"%s\", \"%s\"},\n", $last, $wev->[1],
              $new_desc;
        }
    }
 
    printf $h "#endif                          /* WAIT_EVENT_TYPES_H */\n";
    close $h;
    close $c;
    close $wc;
 
    rename($htmp, "$output_path/wait_event_types.h")
      || die "rename: $htmp to $output_path/wait_event_types.h: $!";
    rename($ctmp, "$output_path/pgstat_wait_event.c")
      || die "rename: $ctmp to $output_path/pgstat_wait_event.c: $!";
    rename($wctmp, "$output_path/wait_event_funcs_data.c")
      || die "rename: $wctmp to $output_path/wait_event_funcs_data.c: $!";
}
# Generate the .sgml file.
elsif ($gen_docs)
{
    # Include PID in suffix in case parallel make runs this multiple times.
    my $stmp = "$output_path/wait_event_names.s.tmp$$";
    open my $s, '>', $stmp or die "Could not open $stmp: $!";
 
    # uc() is being used to force the comparison to be case-insensitive.
    foreach my $waitclass (sort { uc($a) cmp uc($b) } keys %hashwe)
    {
        my $last = $waitclass;
        $last =~ s/^WaitEvent//;
        my $lastlc = lc $last;
 
        printf $s "  <table id=\"wait-event-%s-table\">\n", $lastlc;
        printf $s
          "   <title>Wait Events of Type <literal>%s</literal></title>\n",
          ucfirst($lastlc);
        printf $s "   <tgroup cols=\"2\">\n";
        printf $s "    <thead>\n";
        printf $s "     <row>\n";
        printf $s
          "      <entry><literal>$last</literal> Wait Event</entry>\n";
        printf $s "      <entry>Description</entry>\n";
        printf $s "     </row>\n";
        printf $s "    </thead>\n\n";
        printf $s "    <tbody>\n";
 
        foreach my $wev (@{ $hashwe{$waitclass} })
        {
            printf $s "     <row>\n";
            printf $s "      <entry><literal>%s</literal></entry>\n",
              $wev->[1];
            printf $s "      <entry>%s</entry>\n", substr $wev->[2], 1, -1;
            printf $s "     </row>\n";
        }
 
        printf $s "    </tbody>\n";
        printf $s "   </tgroup>\n";
        printf $s "  </table>\n\n";
    }
 
    close $s;
 
    rename($stmp, "$output_path/wait_event_types.sgml")
      || die "rename: $stmp to $output_path/wait_event_types.sgml: $!";
}
 
close $wait_event_names;
 
sub usage
{
    die <<EOM;
Usage: perl  [--output <path>] [--code ] [ --docs ] input_file
 
Options:
    --outdir         Output directory (default '.')
    --code           Generate C and header files.
    --docs           Generate wait_event_types.sgml.
 
generate-wait_event_types.pl generates the SGML documentation and code
related to wait events.  This should use wait_event_names.txt in input, or
an input file with a compatible format.
 
Report bugs to <pgsql-bugs\@lists.postgresql.org>.
EOM
}