Clean up work_mem handling

Add some comments and improve exception handling when twiddling work_mem for
the search update
pull/2768/head
Richard van der Hoff 2018-02-03 23:05:41 +00:00
parent 4eeae7ad65
commit bd25f9cf36
1 changed files with 41 additions and 11 deletions

View File

@ -12,7 +12,7 @@
# 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.
import sys
from twisted.internet import defer
from .background_updates import BackgroundUpdateStore
@ -256,12 +256,27 @@ class SearchStore(BackgroundUpdateStore):
value (str):
"""
if isinstance(self.database_engine, PostgresEngine):
txn.execute("SET work_mem='256kB'")
sql = (
"INSERT INTO event_search"
" (event_id, room_id, key, vector, stream_ordering, origin_server_ts)"
" (event_id, room_id, key, vector, stream_ordering, "
" origin_server_ts)"
" VALUES (?,?,?,to_tsvector('english', ?),?,?)"
)
# inserts to a GIN index are normally batched up into a pending
# list, and then all committed together once the list gets to a
# certain size. The trouble with that is that postgres (pre-9.5)
# uses work_mem to determine the length of the list, and work_mem
# is typically very large.
#
# We therefore reduce work_mem while we do the insert.
#
# (postgres 9.5 uses the separate gin_pending_list_limit setting,
# so doesn't suffer the same problem, but changing work_mem will
# be harmless)
txn.execute("SET work_mem='256kB'")
try:
txn.execute(
sql,
(
@ -270,7 +285,22 @@ class SearchStore(BackgroundUpdateStore):
event.origin_server_ts,
)
)
except Exception:
# we need to reset work_mem, but doing so may throw a new
# exception and we want to preserve the original
t, v, tb = sys.exc_info()
try:
txn.execute("RESET work_mem")
except Exception as e:
logger.warn(
"exception resetting work_mem during exception "
"handling: %r",
e,
)
raise t, v, tb
else:
txn.execute("RESET work_mem")
elif isinstance(self.database_engine, Sqlite3Engine):
sql = (
"INSERT INTO event_search (event_id, room_id, key, value)"