Projet

Général

Profil

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

Benjamin Dauvergne, 25 septembre 2020 08:10

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

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

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

  
346 383
    def do_dates_table(self):
347 384
        self.ex('CREATE TABLE IF NOT EXISTS public.dates (date date, day text, month text)')
348
-