Skip to content

Commit aff56cc

Browse files
committed
[IMP] util/pg: cache column_exists for common columns
In many cases we know that columns must exist, it's better to cache them to avoid thousands of small queries.
1 parent 86409e5 commit aff56cc

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/util/pg.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Utility functions for interacting with PostgreSQL."""
33

44
import collections
5+
import functools
56
import logging
67
import os
78
import re
@@ -43,7 +44,7 @@
4344

4445
from .exceptions import MigrationError, SleepyDeveloperError
4546
from .helpers import _validate_table, model_of_table
46-
from .misc import Sentinel, log_progress
47+
from .misc import Sentinel, log_progress, version_gte
4748

4849
_logger = logging.getLogger(__name__)
4950

@@ -472,6 +473,30 @@ def _column_info(cr, table, column):
472473
return cr.fetchone()
473474

474475

476+
_COLUMNS = {}
477+
if version_gte("10.0"): # Columns were added in 9.0
478+
_COLUMNS[("ir_ui_view", "arch_db")] = True
479+
_COLUMNS[("ir_filters", "sort")] = True
480+
_COLUMNS[("ir_model_fields", "relation_table")] = True
481+
_COLUMNS[("ir_model_fields", "depends")] = True
482+
if version_gte("14.0"): # Columns added in 13.0
483+
_COLUMNS[("ir_module_module_dependency", "auto_install_required")] = False
484+
485+
486+
def _forced_cache(cache):
487+
def decorator(f):
488+
@functools.wraps(f)
489+
def wrapper(cr, *args, **kwargs):
490+
if not kwargs and tuple(args) in cache:
491+
return cache[tuple(args)]
492+
return f(cr, *args, **kwargs)
493+
494+
return wrapper
495+
496+
return decorator
497+
498+
499+
@_forced_cache(_COLUMNS)
475500
def column_exists(cr, table, column):
476501
"""
477502
Return whether a column exists.

0 commit comments

Comments
 (0)