Opened 10 years ago

Closed 10 years ago

#23514 closed Cleanup/optimization (fixed)

Prevent database connection at PostGISOperations init time

Reported by: Claude Paroz Owned by: nobody
Component: GIS Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Mattia Procopio)

Currently, PostGISOperations does query the database during its init time (to check the spatial version). This should be avoided if possible (see use case in #17656), and should be doable by transforming some variables into properties.

Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
    class Permission(models.Model):
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/models/base.py", line 125, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/models/base.py", line 300, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/utils.py", line 243, in __getitem__
    conn = backend.DatabaseWrapper(db, alias)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 14, in __init__
    self.ops = PostGISOperations(self)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 166, in __init__
    if self.spatial_version < (1, 3, 4):
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 260, in spatial_version
    vtup = self.postgis_version_tuple()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 434, in postgis_version_tuple
    version = self.postgis_lib_version()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 414, in postgis_lib_version
    return self._get_postgis_func('postgis_lib_version')
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 404, in _get_postgis_func
    with self.connection.temporary_connection() as cursor:
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/__init__.py", line 543, in temporary_connection
    cursor = self.cursor()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/__init__.py", line 165, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/__init__.py", line 138, in _cursor
    self.ensure_connection()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/__init__.py", line 122, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 134, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/matt/repos/uwncom/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  role "234124323" does not exist

Change History (5)

comment:1 by Claude Paroz, 10 years ago

Description: modified (diff)

comment:2 by Mattia Procopio, 10 years ago

Description: modified (diff)

traceback added

comment:4 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Claude Paroz <claude@…>, 10 years ago

Resolution: fixed
Status: newclosed

In abc11b0a33fd823ad341ab2cc113c73545eb61e6:

Fixed #23514 -- Prevented queries in PostGISOperations init

Thanks Mattia Procopio for the report.

Note: See TracTickets for help on using tickets.
Back to Top