Skip to content

Commit d8ffad7

Browse files
committed
Set id as the PRIMARY KEY of AttachmentsIndex for Pg
This is consistent with mysql's fulltext table. As id is the primary key, DB automatically indexes the column which helps performance especially for the following SQL that is called quite frequently when indexing data: SELECT MAX(id) FROM AttachmentsIndex Mainly to support customized fulltext table name, we implement the upgrade in Perl instead of SQL. Use "indexes" instead of "content" considering the database handle "content" uses usually doesn't have the permission to alter tables, not mentioning that it's more like an index change.
1 parent 2ee360a commit d8ffad7

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

etc/upgrade/5.0.6/indexes

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use strict;
2+
use warnings;
3+
4+
if ( RT->Config->Get('DatabaseType') eq 'Pg' ) {
5+
my $fulltext = RT->Config->Get('FullTextSearch');
6+
if ( my $table = $fulltext->{Table} ) {
7+
my $handle = RT->DatabaseHandle;
8+
if ( my $indexes = { $handle->Indexes }->{ lc $table } ) {
9+
if ( !grep { $_ eq lc "${table}_pkey" } @$indexes ) {
10+
my $res = $handle->dbh->do("ALTER TABLE IF EXISTS $table ADD PRIMARY KEY(id)");
11+
if ( !$res ) {
12+
RT->Logger->error("Could not add PRIMARY KEY to table $table");
13+
}
14+
}
15+
}
16+
}
17+
}
18+
19+
1;

sbin/rt-setup-fulltext-index.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ elsif ( $DB{'type'} eq 'Pg' ) {
327327
push @schema, split /;\n+/, <<SCHEMA;
328328
CREATE TABLE $table (
329329
id BIGSERIAL,
330-
$column tsvector
330+
$column tsvector,
331+
PRIMARY KEY (id)
331332
);
332333
GRANT SELECT, INSERT, UPDATE, DELETE ON $table TO "$DB{user}"
333334
SCHEMA

0 commit comments

Comments
 (0)