Projet

Général

Profil

0001-misc-remove-SQL-objects-in-order-to-use-less-locks-4.patch

Benjamin Dauvergne, 19 mai 2020 20:31

Télécharger (3,61 ko)

Voir les différences:

Subject: [PATCH 1/3] misc: remove SQL objects in order to use less locks
 (#43108)

 wcs_olap/feeder.py | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
wcs_olap/feeder.py
3 3
from __future__ import unicode_literals
4 4

  
5 5
from collections import OrderedDict
6
import contextlib
6 7
import datetime
7 8
import six
8 9
import copy
......
12 13
import hashlib
13 14
from .utils import Whatever
14 15
import psycopg2
16
import psycopg2.errorcodes
15 17

  
16 18
from cached_property import cached_property
17 19
from wcs_olap.wcs_api import WcsApiError
......
20 22
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
21 23

  
22 24

  
25
@contextlib.contextmanager
26
def ignore_undefined_object_or_table():
27
    try:
28
        yield
29
    except psycopg2.ProgrammingError as e:
30
        if e.pgcode not in [psycopg2.errorcodes.UNDEFINED_TABLE, psycopg2.errorcodes.UNDEFINED_OBJECT]:
31
            raise
32

  
33

  
23 34
def quote(name):
24 35
    return '"%s"' % name
25 36

  
......
326 337
        """
327 338
        Drop tables one by one in order to avoid reaching max_locks_per_transaction
328 339
        """
329
        self.ex("SELECT tablename FROM pg_tables WHERE schemaname = '%s'" % schema)
340
        # drop foreign key constraints first
341
        self.ex("SELECT table_name, constraint_name FROM "
342
                "information_schema.key_column_usage "
343
                "WHERE table_schema = %s AND constraint_name LIKE '%%_fkey'", vars=[schema])
344
        for table_name, constraint_name in self.cur.fetchall():
345
            # drop of PK constraints can have effects on FK constraint on other tables.
346
            with ignore_undefined_object_or_table():
347
                print('Dropping s', constraint_name)
348
                self.ex('ALTER TABLE %s.%s DROP CONSTRAINT IF EXISTS %s CASCADE'
349
                        % (quote(schema), quote(table_name), quote(constraint_name)))
350
        # remove others
351
        self.ex("SELECT table_name, constraint_name FROM "
352
                "information_schema.key_column_usage "
353
                "WHERE table_schema = %s", vars=[schema])
354
        for table_name, constraint_name in self.cur.fetchall():
355
            # drop of PK constraints can have effects on FK constraint on other tables.
356
            with ignore_undefined_object_or_table():
357
                self.ex('ALTER TABLE %s.%s DROP CONSTRAINT IF EXISTS %s CASCADE'
358
                        % (quote(schema), quote(table_name), quote(constraint_name)))
359
        # then drop indexes
360
        self.ex("SELECT tablename, indexname FROM pg_indexes WHERE schemaname = %s", vars=[schema])
361
        for table_name, index_name in self.cur.fetchall():
362
            with ignore_undefined_object_or_table():
363
                self.ex('DROP INDEX %s.%s CASCADE' % (quote(schema), quote(index_name)))
364

  
365
        # finally drop tables, cascade will have no effect
366
        self.ex("SELECT tablename FROM pg_tables WHERE schemaname = %s ORDER BY tablename DESC", vars=[schema])
330 367
        for table in self.cur.fetchall():
331 368
            tablename = '%s.%s' % (quote(schema), quote(table[0]))
332
            self.ex('DROP TABLE IF EXISTS %s CASCADE;' % tablename)
369
            self.ex('DROP TABLE IF EXISTS %s;' % tablename)
333 370

  
334 371
    def do_dates_table(self):
335 372
        self.ex("DROP TABLE IF EXISTS public.dates")
336
-