[IMP] translations: use a btree index on src instead of a hash index
The motivation behind that change is that the hash index in postgres is not compatible with streaming replication. The new index is defined on md5(src), which provides good performance without size limitation on src.
This commit is contained in:
@@ -239,25 +239,25 @@ class ir_translation(osv.osv):
|
||||
def _auto_init(self, cr, context=None):
|
||||
super(ir_translation, self)._auto_init(cr, context)
|
||||
|
||||
# FIXME: there is a size limit on btree indexed values so we can't index src column with normal btree.
|
||||
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltns',))
|
||||
if cr.fetchone():
|
||||
#temporarily removed: cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (name, lang, type, src)')
|
||||
cr.execute('DROP INDEX ir_translation_ltns')
|
||||
cr.commit()
|
||||
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_lts',))
|
||||
if cr.fetchone():
|
||||
#temporarily removed: cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
|
||||
cr.execute('DROP INDEX ir_translation_lts')
|
||||
cr.execute("SELECT indexname FROM pg_indexes WHERE indexname LIKE 'ir_translation_%'")
|
||||
indexes = [row[0] for row in cr.fetchall()]
|
||||
|
||||
# Removed because there is a size limit on btree indexed values (problem with column src):
|
||||
# cr.execute('CREATE INDEX ir_translation_ltns ON ir_translation (name, lang, type, src)')
|
||||
# cr.execute('CREATE INDEX ir_translation_lts ON ir_translation (lang, type, src)')
|
||||
#
|
||||
# Removed because hash indexes are not compatible with postgres streaming replication:
|
||||
# cr.execute('CREATE INDEX ir_translation_src_hash_idx ON ir_translation USING hash (src)')
|
||||
if set(indexes) & set(['ir_translation_ltns', 'ir_translation_lts', 'ir_translation_src_hash_idx']):
|
||||
cr.execute('DROP INDEX IF EXISTS ir_translation_ltns, ir_translation_lts, ir_translation_src_hash_idx')
|
||||
cr.commit()
|
||||
|
||||
# add separate hash index on src (no size limit on values), as postgres 8.1+ is able to combine separate indexes
|
||||
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_src_hash_idx',))
|
||||
if not cr.fetchone():
|
||||
cr.execute('CREATE INDEX ir_translation_src_hash_idx ON ir_translation using hash (src)')
|
||||
# Add separate md5 index on src (no size limit on values, and good performance).
|
||||
if 'ir_translation_src_md5' not in indexes:
|
||||
cr.execute('CREATE INDEX ir_translation_src_md5 ON ir_translation (md5(src))')
|
||||
cr.commit()
|
||||
|
||||
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_translation_ltn',))
|
||||
if not cr.fetchone():
|
||||
if 'ir_translation_ltn' not in indexes:
|
||||
cr.execute('CREATE INDEX ir_translation_ltn ON ir_translation (name, lang, type)')
|
||||
cr.commit()
|
||||
|
||||
@@ -304,12 +304,15 @@ class ir_translation(osv.osv):
|
||||
|
||||
def _get_source_query(self, cr, uid, name, types, lang, source, res_id):
|
||||
if source:
|
||||
# Note: the extra test on md5(src) is a hint for postgres to use the
|
||||
# index ir_translation_src_md5
|
||||
query = """SELECT value
|
||||
FROM ir_translation
|
||||
WHERE lang=%s
|
||||
AND type in %s
|
||||
AND src=%s"""
|
||||
params = (lang or '', types, tools.ustr(source))
|
||||
AND src=%s AND md5(src)=md5(%s)"""
|
||||
source = tools.ustr(source)
|
||||
params = (lang or '', types, source, source)
|
||||
if res_id:
|
||||
query += " AND res_id=%s"
|
||||
params += (res_id,)
|
||||
|
||||
@@ -151,11 +151,11 @@ csv.register_dialect("UNIX", UNIX_LINE_TERMINATOR)
|
||||
#
|
||||
def translate(cr, name, source_type, lang, source=None):
|
||||
if source and name:
|
||||
cr.execute('select value from ir_translation where lang=%s and type=%s and name=%s and src=%s', (lang, source_type, str(name), source))
|
||||
cr.execute('select value from ir_translation where lang=%s and type=%s and name=%s and src=%s and md5(src)=md5(%s)', (lang, source_type, str(name), source, source))
|
||||
elif name:
|
||||
cr.execute('select value from ir_translation where lang=%s and type=%s and name=%s', (lang, source_type, str(name)))
|
||||
elif source:
|
||||
cr.execute('select value from ir_translation where lang=%s and type=%s and src=%s', (lang, source_type, source))
|
||||
cr.execute('select value from ir_translation where lang=%s and type=%s and src=%s and md5(src)=md5(%s)', (lang, source_type, source, source))
|
||||
res_trans = cr.fetchone()
|
||||
res = res_trans and res_trans[0] or False
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user