Projet

Général

Profil

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

Benjamin Dauvergne, 17 décembre 2020 15:36

Télécharger (3,64 ko)

Voir les différences:

Subject: [PATCH] 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 copy
8 9
import itertools
......
11 12
import hashlib
12 13
from .utils import Whatever
13 14
import psycopg2
15
import psycopg2.errorcodes
14 16

  
15 17
from cached_property import cached_property
16 18
from wcs_olap.wcs_api import WcsApiError
......
30 32
            + identifier[-(63 - hash_length) // 2:])
31 33

  
32 34

  
35
@contextlib.contextmanager
36
def ignore_undefined_object_or_table():
37
    try:
38
        yield
39
    except psycopg2.ProgrammingError as e:
40
        if e.pgcode not in [psycopg2.errorcodes.UNDEFINED_TABLE, psycopg2.errorcodes.UNDEFINED_OBJECT]:
41
            raise
42

  
43

  
33 44
def quote(name):
34 45
    return '"%s"' % name
35 46

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

  
374
        # finally drop tables, cascade will have no effect
375
        self.ex("SELECT tablename FROM pg_tables WHERE schemaname = %s ORDER BY tablename DESC", vars=[schema])
339 376
        for table in self.cur.fetchall():
340 377
            tablename = '%s.%s' % (quote(schema), quote(table[0]))
341
            self.ex('DROP TABLE IF EXISTS %s CASCADE;' % tablename)
378
            self.ex('DROP TABLE IF EXISTS %s;' % tablename)
342 379

  
343 380
    def do_dates_table(self):
344 381
        self.ex('CREATE TABLE IF NOT EXISTS public.dates (date date, day text, month text)')
345
-