summaryrefslogtreecommitdiff
blob: a80391ff750c32550d2efbde620796a9cf5abf47 (plain)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

=head1 NAME

Bugzilla::DB::Pg - Bugzilla database compatibility layer for PostgreSQL

=head1 DESCRIPTION

This module overrides methods of the Bugzilla::DB module with PostgreSQL
specific implementation. It is instantiated by the Bugzilla::DB module
and should never be used directly.

For interface details see L<Bugzilla::DB> and L<DBI>.

=cut

package Bugzilla::DB::Pg;

use 5.10.1;
use strict;
use warnings;

use Bugzilla::Error;
use Bugzilla::Version;
use DBD::Pg;
use Bugzilla::Util qw(trick_taint);

# This module extends the DB interface via inheritance
use parent qw(Bugzilla::DB);

use constant BLOB_TYPE => {pg_type => DBD::Pg::PG_BYTEA};

sub new {
  my ($class, $params) = @_;
  my ($user, $pass, $host, $dbname, $port)
    = @$params{qw(db_user db_pass db_host db_name db_port)};

  # The default database name for PostgreSQL. We have
  # to connect to SOME database, even if we have
  # no $dbname parameter.
  $dbname ||= 'template1';

  # construct the DSN from the parameters we got
  my $dsn = "dbi:Pg:dbname=$dbname";
  $dsn .= ";host=$host" if $host;
  $dsn .= ";port=$port" if $port;

  # This stops Pg from printing out lots of "NOTICE" messages when
  # creating tables.
  $dsn .= ";options='-c client_min_messages=warning'";

  my $attrs = {pg_enable_utf8 => Bugzilla->params->{'utf8'}};

  my $self = $class->db_new(
    {dsn => $dsn, user => $user, pass => $pass, attrs => $attrs});

  # all class local variables stored in DBI derived class needs to have
  # a prefix 'private_'. See DBI documentation.
  $self->{private_bz_tables_locked} = "";

  # Needed by TheSchwartz
  $self->{private_bz_dsn} = $dsn;

  bless($self, $class);

  return $self;
}

# if last_insert_id is supported on PostgreSQL by lowest DBI/DBD version
# supported by Bugzilla, this implementation can be removed.
sub bz_last_key {
  my ($self, $table, $column) = @_;

  my $seq = $table . "_" . $column . "_seq";
  my ($last_insert_id) = $self->selectrow_array("SELECT CURRVAL('$seq')");

  return $last_insert_id;
}

## REDHAT EXTENSION 1255227 BEGIN

=head2 bz_start_transaction

Customize how transactions start for RHBZ 1255227

=cut

sub bz_start_transaction {
  my $self = shift;

  unless ($self->bz_in_transaction) {
    $self->do('ROLLBACK');
  }

  return $self->SUPER::bz_start_transaction(@_);
}
## REDHAT EXTENSION 1255227 END

sub sql_group_concat {
  my ($self, $text, $separator, $sort, $order_by) = @_;
  $sort = 1 if !defined $sort;
  $separator = $self->quote(', ') if !defined $separator;

  # PostgreSQL 8.x doesn't support STRING_AGG
  if (vers_cmp($self->bz_server_version, 9) < 0) {
    my $sql = "ARRAY_ACCUM($text)";
    if ($sort) {
      $sql = "ARRAY_SORT($sql)";
    }
    return "ARRAY_TO_STRING($sql, $separator)";
  }

  if ($order_by && $text =~ /^DISTINCT\s*(.+)$/i) {

    # Since Postgres (quite rightly) doesn't support "SELECT DISTINCT x
    # ORDER BY y", we need to sort the list, and then get the unique
    # values
    return
      "ARRAY_TO_STRING(ANYARRAY_UNIQ(ARRAY_AGG($1 ORDER BY $order_by)), $separator)";
  }

  # Determine the ORDER BY clause (if any)
  if ($order_by) {
    $order_by = " ORDER BY $order_by";
  }
  elsif ($sort) {

    # We don't include the DISTINCT keyword in an order by
    $text =~ /^(?:DISTINCT\s*)?(.+)$/i;
    $order_by = " ORDER BY $1";
  }

  return "STRING_AGG(${text}::text, $separator${order_by}::text)";
}

sub sql_istring {
  my ($self, $string) = @_;

  return "LOWER(${string}::text)";
}

sub sql_position {
  my ($self, $fragment, $text) = @_;

  return "POSITION(${fragment}::text IN ${text}::text)";
}

sub sql_like {
  my ($self, $fragment, $column, $not) = @_;
  $not //= '';

  return
      "${column}::text $not LIKE "
    . $self->sql_like_escape($fragment)
    . " ESCAPE '|'";
}

sub sql_ilike {
  my ($self, $fragment, $column, $not) = @_;
  $not //= '';

  return
      "${column}::text $not ILIKE "
    . $self->sql_like_escape($fragment)
    . " ESCAPE '|'";
}

sub sql_not_ilike {
  return shift->sql_ilike(@_, 'NOT');
}

# Escapes any % or _ characters which are special in a LIKE match.
# Also performs a $dbh->quote to escape any quote characters.
sub sql_like_escape {
  my ($self, $fragment) = @_;

  $fragment =~ s/\|/\|\|/g;    # escape the escape character if it appears
  $fragment =~ s/%/\|%/g;      # percent and underscore are the special match
  $fragment =~ s/_/\|_/g;      # characters in SQL.

  return $self->quote("%$fragment%");
}

sub sql_regexp {
  my ($self, $expr, $pattern, $nocheck, $real_pattern) = @_;
  $real_pattern ||= $pattern;

  $self->bz_check_regexp($real_pattern) if !$nocheck;

  return "${expr}::text ~* $pattern";
}

sub sql_not_regexp {
  my ($self, $expr, $pattern, $nocheck, $real_pattern) = @_;
  $real_pattern ||= $pattern;

  $self->bz_check_regexp($real_pattern) if !$nocheck;

  return "${expr}::text !~* $pattern";
}

sub sql_limit {
  my ($self, $limit, $offset) = @_;

  if (defined($offset)) {
    return "LIMIT $limit OFFSET $offset";
  }
  else {
    return "LIMIT $limit";
  }
}

sub sql_from_days {
  my ($self, $days) = @_;

  return "TO_TIMESTAMP('$days', 'J')::date";
}

sub sql_to_days {
  my ($self, $date) = @_;

  return "TO_CHAR(${date}::date, 'J')::int";
}

sub sql_date_format {
  my ($self, $date, $format) = @_;

  $format = "%Y.%m.%d %H:%i:%s" if !$format;

  $format =~ s/\%Y/YYYY/g;
  $format =~ s/\%y/YY/g;
  $format =~ s/\%m/MM/g;
  $format =~ s/\%d/DD/g;
  $format =~ s/\%a/Dy/g;
  $format =~ s/\%H/HH24/g;
  $format =~ s/\%i/MI/g;
  $format =~ s/\%s/SS/g;

  return "TO_CHAR($date, " . $self->quote($format) . ")";
}

sub sql_date_math {
  my ($self, $date, $operator, $interval, $units) = @_;

  return "$date $operator $interval * INTERVAL '1 $units'";
}

sub sql_string_concat {
  my ($self, @params) = @_;

  # Postgres 7.3 does not support concatenating of different types, so we
  # need to cast both parameters to text. Version 7.4 seems to handle this
  # properly, so when we stop support 7.3, this can be removed.
  return '(CAST(' . join(' AS text) || CAST(', @params) . ' AS text))';
}

# Tell us whether or not a particular sequence exists in the DB.
sub bz_sequence_exists {
  my ($self, $seq_name) = @_;
  my $exists
    = $self->selectrow_array(
    'SELECT 1 FROM pg_statio_user_sequences WHERE relname = ?',
    undef, $seq_name);
  return $exists || 0;
}

sub bz_explain {
  my ($self, $sql) = @_;
  my $explain = $self->selectcol_arrayref("EXPLAIN ANALYZE $sql");
  return join("\n", @$explain);
}


sub _postgres_fts_escape {
  my ($self, $message) = @_;

  my $map = {
    ':'  => 'BZESCAPECOLON',
    ';'  => 'BZESCAPECOMMA',
    '|'  => 'BZESCAPEPIPE',
    '/'  => 'BZESCAPESLASH',
    '\\' => 'BZESCAPEBSLASH',
    '('  => 'BZESCAPEOB',
    ')'  => 'BZESCAPECB',
    '{'  => 'BZESCAPEOP',
    '}'  => 'BZESCAPECP',
    '['  => 'BZESCAPESBO',
    ']'  => 'BZESCAPESBC',
    '<'  => 'BZESCAPELT',
    '>'  => 'BZESCAPEGT',
    '\'' => 'BZESCAPESQ',
    '"'  => 'BZESCAPEDQ',
    '='  => 'BZESCAPEEQ',
    ','  => 'BZESCAPECOMMA',
    '~'  => 'BZESCAPETIL',
    '!'  => 'BZESCAPEEXC',
    '@'  => 'BZESCAPEAT',
    '#'  => 'BZESCAPEHAS',
    '$'  => 'BZESCAEPEDOL',
    '%'  => 'BZESCAPEPC',
    '^'  => 'BZESCAPECAR',
    '&'  => 'BZESCAPEAMP',
    '*'  => 'BZESCAPEAST',
    '-'  => 'BZESCAPEDASH',
    '+'  => 'BZESCAPEPLU',
  };

  while (my ($key, $value) = each(%$map)) {
    my $qkey = quotemeta $key;
    my $qval = ' ' . $value . ' ';
    $message =~ s/$qkey/$qval/g;
  }
  chomp $message;
  $message =~ s/^\s+//;

  return $message;
}


## REDHAT EXTENSION 1191022 BEGIN
sub sql_fulltext_search {
  my ($self, $column, $text) = @_;

  my $fts_escaped = lc $self->_postgres_fts_escape($text);

  my $anded = join " & ", split /\s+/, $fts_escaped;

  my $quoted = $self->quote($anded);
  trick_taint($quoted);

  my $query = "${column}_vect @@ TO_TSQUERY($quoted)";
  my $rank  = "ts_rank_cd(${column}_vect, TO_TSQUERY($quoted))";

  return ($query, $rank);
}
## REDHAT EXTENSION 1191022 END

#####################################################################
# Custom Database Setup
#####################################################################

sub bz_check_server_version {
  my $self           = shift;
  my ($db)           = @_;
  my $server_version = $self->SUPER::bz_check_server_version(@_);
  my ($major_version, $minor_version) = $server_version =~ /^0*(\d+)\.0*(\d+)/;

  # Pg 9.0 requires DBD::Pg 2.17.2 in order to properly read bytea values.
  # Pg 9.2 requires DBD::Pg 2.19.3 as spclocation no longer exists.
  if ($major_version >= 9) {
    local $db->{dbd}->{version} = ($minor_version >= 2) ? '2.19.3' : '2.17.2';
    local $db->{name} = $db->{name} . " ${major_version}.$minor_version";
    Bugzilla::DB::_bz_check_dbd(@_);
  }
}

sub bz_setup_database {
  my $self = shift;
  $self->SUPER::bz_setup_database(@_);

  my ($has_plpgsql)
    = $self->selectrow_array(
    "SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'");
  $self->do('CREATE LANGUAGE plpgsql') unless $has_plpgsql;
  ## BUGBUG you need super user on the DB to do this.
  ## This is just here so it blows up if they are missing.
  $self->do('CREATE EXTENSION IF NOT EXISTS plperl');
  $self->do('CREATE EXTENSION IF NOT EXISTS pg_trgm');

  if (vers_cmp($self->bz_server_version, 9) < 0) {

    # Custom Functions for Postgres 8
    my $function = 'array_accum';
    my $array_accum
      = $self->selectrow_array('SELECT 1 FROM pg_proc WHERE proname = ?',
      undef, $function);
    if (!$array_accum) {
      print "Creating function $function...\n";
      $self->do(
        "CREATE AGGREGATE array_accum (
                           SFUNC = array_append,
                           BASETYPE = anyelement,
                           STYPE = anyarray,
                           INITCOND = '{}' 
                       )"
      );
    }

    $self->do(<<'END');
CREATE OR REPLACE FUNCTION array_sort(ANYARRAY)
RETURNS ANYARRAY LANGUAGE SQL
IMMUTABLE STRICT
AS $$
SELECT ARRAY(
    SELECT $1[s.i] AS each_item
    FROM
        generate_series(array_lower($1,1), array_upper($1,1)) AS s(i)
    ORDER BY each_item
);
$$;
END
  }
  else {
    # Custom functions for Postgres 9.0+

   # -Copyright © 2013 Joshua D. Burns (JDBurnZ) and Message In Action LLC
   # JDBurnZ: https://github.com/JDBurnZ
   # Message In Action: https://www.messageinaction.com
   #
   #Permission is hereby granted, free of charge, to any person obtaining a copy of
   #this software and associated documentation files (the "Software"), to deal in
   #the Software without restriction, including without limitation the rights to
   #use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
   #the Software, and to permit persons to whom the Software is furnished to do so,
   #subject to the following conditions:
   #
   #The above copyright notice and this permission notice shall be included in all
   #copies or substantial portions of the Software.
   #
   #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   #FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   #COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   #IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   #CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    $self->do(
      q|
            DROP FUNCTION IF EXISTS anyarray_uniq(anyarray);
            CREATE OR REPLACE FUNCTION anyarray_uniq(with_array anyarray)
            RETURNS anyarray AS $BODY$
            DECLARE
                -- The variable used to track iteration over "with_array".
                loop_offset integer;

                -- The array to be returned by this function.
                return_array with_array%TYPE := '{}';
            BEGIN
                IF with_array IS NULL THEN
                    return NULL;
                END IF;

                -- Iterate over each element in "concat_array".
                FOR loop_offset IN ARRAY_LOWER(with_array, 1)..ARRAY_UPPER(with_array, 1) LOOP
                    IF NOT with_array[loop_offset] = ANY(return_array) THEN
                        return_array = ARRAY_APPEND(return_array, with_array[loop_offset]);
                    END IF;
                END LOOP;

                RETURN return_array;
            END;
            $BODY$ LANGUAGE plpgsql;
        |
    );
  }

  # PostgreSQL doesn't like having *any* index on the thetext
  # field, because it can't have index data longer than 2770
  # characters on that field.
  $self->bz_drop_index('longdescs', 'longdescs_thetext_idx');

  # Same for all the comments fields in the fulltext table.
  $self->bz_drop_index('bugs_fulltext', 'bugs_fulltext_comments_idx');
  $self->bz_drop_index('bugs_fulltext', 'bugs_fulltext_comments_noprivate_idx');

  # PostgreSQL also wants an index for calling LOWER on
  # login_name, which we do with sql_istrcmp all over the place.
  $self->bz_add_index(
    'profiles',
    'profiles_login_name_lower_idx',
    {FIELDS => ['LOWER(login_name)'], TYPE => 'UNIQUE'}
  );

  # Now that Bugzilla::Object uses sql_istrcmp, other tables
  # also need a LOWER() index.
  _fix_case_differences('fielddefs', 'name');
  $self->bz_add_index('fielddefs', 'fielddefs_name_lower_idx',
    {FIELDS => ['LOWER(name)'], TYPE => 'UNIQUE'});
  _fix_case_differences('keyworddefs', 'name');
  $self->bz_add_index('keyworddefs', 'keyworddefs_name_lower_idx',
    {FIELDS => ['LOWER(name)'], TYPE => 'UNIQUE'});
  _fix_case_differences('products', 'name');
  $self->bz_add_index('products', 'products_name_lower_idx',
    {FIELDS => ['LOWER(name)'], TYPE => 'UNIQUE'});

  # bz_rename_column and bz_rename_table didn't correctly rename
  # the sequence.
  $self->_fix_bad_sequence('fielddefs', 'id', 'fielddefs_fieldid_seq',
    'fielddefs_id_seq');

  # If the 'tags' table still exists, then bz_rename_table()
  # will fix the sequence for us.
  if (!$self->bz_table_info('tags')) {
    my $res = $self->_fix_bad_sequence('tag', 'id', 'tags_id_seq', 'tag_id_seq');

    # If $res is true, then the sequence has been renamed, meaning that
    # the primary key must be renamed too.
    if ($res) {
      $self->do('ALTER INDEX tags_pkey RENAME TO tag_pkey');
    }
  }

  # Certain sequences got upgraded before we required Pg 8.3, and
  # so they were not properly associated with their columns.
  my @tables = $self->bz_table_list_real;
  foreach my $table (@tables) {
    my @columns = $self->bz_table_columns_real($table);
    foreach my $column (@columns) {

      # All our SERIAL pks have "id" in their name at the end.
      next unless $column =~ /id$/;
      my $sequence = "${table}_${column}_seq";
      if ($self->bz_sequence_exists($sequence)) {
        my $is_associated = $self->selectrow_array('SELECT pg_get_serial_sequence(?,?)',
          undef, $table, $column);
        next if $is_associated;
        print "Fixing $sequence to be associated" . " with $table.$column...\n";
        $self->do("ALTER SEQUENCE $sequence OWNED BY $table.$column");

        # In order to produce an exactly identical schema to what
        # a brand-new checksetup.pl run would produce, we also need
        # to re-set the default on this column.
        $self->do(
          "ALTER TABLE $table
                          ALTER COLUMN $column
                           SET DEFAULT nextval('$sequence')"
        );
      }
    }
  }

  ## REDHAT EXTENSION 1191022 BEGIN
  # These columns are needed to hold full text search data.
  $self->bz_add_column('bugs_fulltext', 'comments_vect', {TYPE => 'TSVECTOR'});
  $self->bz_add_column('bugs_fulltext', 'comments_noprivate_vect',
    {TYPE => 'TSVECTOR'});
  $self->bz_add_column('bugs_fulltext', 'short_desc_vect', {TYPE => 'TSVECTOR'});

  # These triggers are to manage the inserting and updating of full text records.
  $self->do(<<'SQLDATA');
        DROP TRIGGER IF EXISTS pg_fts_comments ON bugs_fulltext;
        DROP TRIGGER IF EXISTS pg_fts_comments_noprivate ON bugs_fulltext;
        DROP TRIGGER IF EXISTS pg_fts_short_desc ON bugs_fulltext;
        DROP TRIGGER IF EXISTS pg_fts_insert ON bugs_fulltext;

        CREATE OR REPLACE FUNCTION escape_special_tokens(text) RETURNS text AS $$

            my $message = $_[0];

            my $map = {
                ':' => 'BZESCAPECOLON',
                ';' => 'BZESCAPECOMMA',
                '|' => 'BZESCAPEPIPE',
                '/' => 'BZESCAPESLASH',
                '\\' => 'BZESCAPEBSLASH',
                '(' => 'BZESCAPEOB',
                ')' => 'BZESCAPECB',
                '{' => 'BZESCAPEOP',
                '}' => 'BZESCAPECP',
                '[' => 'BZESCAPESBO',
                ']' => 'BZESCAPESBC',
                '<' => 'BZESCAPELT',
                '>' => 'BZESCAPEGT',
                '\'' => 'BZESCAPESQ',
                '"' => 'BZESCAPEDQ',
                '=' => 'BZESCAPEEQ',
                ',' => 'BZESCAPECOMMA',
                '~' => 'BZESCAPETIL',
                '!' => 'BZESCAPEEXC',
                '@' => 'BZESCAPEAT',
                '#' => 'BZESCAPEHAS',
                '$' => 'BZESCAEPEDOL',
                '%' => 'BZESCAPEPC',
                '^' => 'BZESCAPECAR',
                '&' => 'BZESCAPEAMP',
                '*' => 'BZESCAPEAST',
                '-' => 'BZESCAPEDASH',
                '+' => 'BZESCAPEPLU',
           };

            while(my ($key, $value) = each(%$map)) {
                my $qkey = quotemeta $key;
                my $qval = ' ' . $value . ' ';
                $message =~ s/$qkey/$qval/g
            }

            return $message;

        $$ LANGUAGE plperl;

        CREATE OR REPLACE FUNCTION bugzilla_fts_update_trigger() RETURNS trigger AS $$
        BEGIN
            IF NEW.comments IS NOT NULL
            THEN
                NEW.comments_vect := to_tsvector('english', quote_literal(escape_special_tokens(NEW.comments)));
            END IF;

            IF NEW.comments_noprivate IS NOT NULL
            THEN
                NEW.comments_noprivate_vect :=
                    to_tsvector('english', quote_literal(escape_special_tokens(NEW.comments_noprivate)));
            END IF;

            IF NEW.short_desc IS NOT NULL
            THEN
                NEW.short_desc_vect := to_tsvector('english', quote_literal(escape_special_tokens(NEW.short_desc)));
            END IF;

            RETURN NEW;
        END;
        $$
        LANGUAGE plpgsql;

        CREATE TRIGGER pg_fts_insert
        BEFORE INSERT
        ON bugs_fulltext
        FOR EACH ROW
        EXECUTE PROCEDURE bugzilla_fts_update_trigger();

        CREATE TRIGGER pg_fts_comments
        BEFORE UPDATE OF comments
        ON bugs_fulltext
        FOR EACH ROW
        EXECUTE PROCEDURE bugzilla_fts_update_trigger();

        CREATE TRIGGER pg_fts_comments_noprivate
        BEFORE UPDATE OF comments_noprivate
        ON bugs_fulltext
        FOR EACH ROW
        EXECUTE PROCEDURE bugzilla_fts_update_trigger();

        CREATE TRIGGER pg_fts_short_desc
        BEFORE UPDATE OF short_desc
        ON bugs_fulltext
        FOR EACH ROW
        EXECUTE PROCEDURE bugzilla_fts_update_trigger();

SQLDATA
  ## REDHAT EXTENSION 1191022 END
  $self->do(<<'SQLDATA');
DO
$$
BEGIN
   IF NOT EXISTS (
      SELECT *
      FROM   pg_class c
      JOIN   pg_namespace n ON n.oid = c.relnamespace
      WHERE  c.relname = 'profiles_login_name_trgm_idx'
      AND    n.nspname = 'public'
   ) THEN
      CREATE INDEX profiles_login_name_trgm_idx ON public.profiles USING gin (to_tsvector('english', login_name));
    END IF;
END
$$;
SQLDATA

  return;
}

sub _fix_bad_sequence {
  my ($self, $table, $column, $old_seq, $new_seq) = @_;
  if ( $self->bz_column_info($table, $column)
    && $self->bz_sequence_exists($old_seq))
  {
    print "Fixing $old_seq sequence...\n";
    $self->do("ALTER SEQUENCE $old_seq RENAME TO $new_seq");
    $self->do(
      "ALTER TABLE $table ALTER COLUMN $column
                    SET DEFAULT NEXTVAL('$new_seq')"
    );
    return 1;
  }
  return 0;
}

# Renames things that differ only in case.
sub _fix_case_differences {
  my ($table, $field) = @_;
  my $dbh = Bugzilla->dbh;

  my $duplicates = $dbh->selectcol_arrayref(
    "SELECT DISTINCT LOWER($field) FROM $table 
        GROUP BY LOWER($field) HAVING COUNT(LOWER($field)) > 1"
  );

  foreach my $name (@$duplicates) {
    my $dups
      = $dbh->selectcol_arrayref(
      "SELECT $field FROM $table WHERE LOWER($field) = ?",
      undef, $name);
    my $primary = shift @$dups;
    foreach my $dup (@$dups) {
      my $new_name = "${dup}_";

      # Make sure the new name isn't *also* a duplicate.
      while (1) {
        last
          if (!$dbh->selectrow_array(
          "SELECT 1 FROM $table WHERE LOWER($field) = ?",
          undef, lc($new_name)
          ));
        $new_name .= "_";
      }
      print "$table '$primary' and '$dup' have names that differ",
        " only in case.\nRenaming '$dup' to '$new_name'...\n";
      $dbh->do("UPDATE $table SET $field = ? WHERE $field = ?",
        undef, $new_name, $dup);
    }
  }
}

#####################################################################
# Custom Schema Information Functions
#####################################################################

# Pg includes the PostgreSQL system tables in table_list_real, so
# we need to remove those.
sub bz_table_list_real {
  my $self = shift;

  my @full_table_list = $self->SUPER::bz_table_list_real(@_);

  # All PostgreSQL system tables start with "pg_" or "sql_"
  my @table_list = grep(!/(^pg_)|(^sql_)/, @full_table_list);
  return @table_list;
}

sub quote {
  my $self   = shift;
  my $str    = shift;
  my $decode = 0;
  if (utf8::is_utf8($str)) {
    utf8::encode($str);
    $decode = 1;
  }
  my $retval = $self->SUPER::quote($str, @_);
  trick_taint($retval)  if defined $retval;
  utf8::decode($retval) if defined $retval && $decode;
  return $retval;
}

1;

=head2 Functions

=over

=item C<sql_like_escape>

=over

=item B<Description>

The postgres versions of the sql_like methods use the ANSI SQL LIKE
statements to perform substring searching.  To prevent issues with
users attempting to search for strings containing special characters
associated with LIKE, we escape them out so they don't affect the search
terms.

=item B<Params>

=over

=item C<$fragment> - The string fragment in need of escaping and quoting

=back

=item B<Returns>

The fragment with any pre existing %,_,| characters escaped out, wrapped in
percent characters and quoted.

=back

=back

=head1 B<Methods in need of POD>

=over

=item quote

=item sql_date_format

=item sql_fulltext_search

=item bz_explain

=item bz_sequence_exists

=item bz_last_key

=item sql_position

=item sql_like

=item sql_ilike

=item sql_not_ilike

=item sql_limit

=item sql_not_regexp

=item sql_string_concat

=item sql_date_math

=item sql_to_days

=item bz_check_server_version

=item sql_from_days

=item bz_table_list_real

=item sql_regexp

=item sql_istring

=item sql_group_concat

=item bz_setup_database

=back