Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

switch back from GIST to GIN indexes #2769

Merged
merged 15 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions synapse/storage/schema/delta/38/postgres_fts_gist.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
* limitations under the License.
*/

INSERT into background_updates (update_name, progress_json)
VALUES ('event_search_postgres_gist', '{}');
-- We no longer do this given we back it out again in schema 46

-- INSERT into background_updates (update_name, progress_json)
-- VALUES ('event_search_postgres_gist', '{}');
17 changes: 17 additions & 0 deletions synapse/storage/schema/delta/46/postgres_fts_gin.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright 2018 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

INSERT into background_updates (update_name, progress_json)
VALUES ('event_search_postgres_gin', '{}');
37 changes: 23 additions & 14 deletions synapse/storage/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SearchStore(BackgroundUpdateStore):

EVENT_SEARCH_UPDATE_NAME = "event_search"
EVENT_SEARCH_ORDER_UPDATE_NAME = "event_search_order"
EVENT_SEARCH_USE_GIST_POSTGRES_NAME = "event_search_postgres_gist"
EVENT_SEARCH_USE_GIN_POSTGRES_NAME = "event_search_postgres_gin"

def __init__(self, db_conn, hs):
super(SearchStore, self).__init__(db_conn, hs)
Expand All @@ -43,8 +43,8 @@ def __init__(self, db_conn, hs):
self._background_reindex_search_order
)
self.register_background_update_handler(
self.EVENT_SEARCH_USE_GIST_POSTGRES_NAME,
self._background_reindex_gist_search
self.EVENT_SEARCH_USE_GIN_POSTGRES_NAME,
self._background_reindex_gin_search
)

@defer.inlineCallbacks
Expand Down Expand Up @@ -145,25 +145,34 @@ def reindex_search_txn(txn):
defer.returnValue(result)

@defer.inlineCallbacks
def _background_reindex_gist_search(self, progress, batch_size):
def _background_reindex_gin_search(self, progress, batch_size):
'''This handles old synapses which used GIST indexes, if any;
converting them back to be GIN as per the actual schema.
'''

def create_index(conn):
conn.rollback()
conn.set_session(autocommit=True)
c = conn.cursor()
try:
conn.rollback()
conn.set_session(autocommit=True)
c = conn.cursor()

c.execute(
"CREATE INDEX CONCURRENTLY event_search_fts_idx_gist"
" ON event_search USING GIST (vector)"
)
c.execute(
"CREATE INDEX CONCURRENTLY event_search_fts_idx"
" ON event_search USING GIN (vector)"
)

c.execute("DROP INDEX event_search_fts_idx")
c.execute("DROP INDEX event_search_fts_idx_gist")

conn.set_session(autocommit=False)
conn.set_session(autocommit=False)
except e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

itym except Exception as e

logger.warn(
"Ignoring error %s when trying to switch from GIST to GIN" % (e,)
)

if isinstance(self.database_engine, PostgresEngine):
yield self.runWithConnection(create_index)

yield self._end_background_update(self.EVENT_SEARCH_USE_GIST_POSTGRES_NAME)
yield self._end_background_update(self.EVENT_SEARCH_USE_GIN_POSTGRES_NAME)
defer.returnValue(1)

@defer.inlineCallbacks
Expand Down