2014-07-17 4 views
1

웹 앱에 장고와 포스트 지스를 사용하고 있습니다. 내 테스트 env에서 denorm_locs = MultiPointField(null=True)을 추가 (DB 테이블에 새 열 추가) 한 다음 새 필드를 채우기 위해 데이터 마이그레이션을 수행하기 위해 South 스키마 마이그레이션을 실행했습니다. 여기 datamigration는 다음과 같습니다Postgis UPDATE가 영원합니다.

# -*- coding: utf-8 -*- 
from south.utils import datetime_utils as datetime 
from south.db import db 
from south.v2 import DataMigration 
from django.db import models 

from django.contrib.gis.geos import MultiPoint 

import time 

class Migration(DataMigration): 

    def forwards(self, orm): 
     "Write your forwards methods here." 
     # Note: Don't use "from appname.models import ModelName". 
     # Use orm.ModelName to refer to models in this application, 
     # and orm['appname.ModelName'] for models in other applications. 

     i = 0 
     batch_size = 10 

     start = time.time() 
     print 'Start: %s' % start 

     cont = True 

     while cont: 
      id_list = orm.MyModel.objects.filter(denorm_locs__isnull=True).order_by('id').values_list('id', flat=True)[:batch_size] 

      id_list = list(id_list) 

      print 'Got id_list (%s ids): %s' % (len(id_list), time.time() - start,) 

      if len(id_list) < batch_size: 
       cont = False 

      for mymodel_id in id_list: 
       locs = [ 
        l.loc 
        for l in orm.Location.objects.filter(mymodel3__mymodels__id=mymodel_id) 
        if l.loc 
       ] 

       mp = MultiPoint(locs) 

       orm.MyModel.objects.filter(id=mymodel_id).update(denorm_locs=mp) 

      i += 1 
      print '%s batches of %s completed' % (i, batch_size,) 

위치 모델은 단지 PointField 그것에 loc라고 있습니다.

orm.MyModel.objects.filter(id=mymodel_id).update(denorm_locs=mp)는 다음 쿼리를 사용하여 실행합니다

UPDATE "core_mymodel" SET "denorm_locs" = ST_GeomFromEWKB('\x0104000020e610000001000000010100000086c954c1a81054c01d5a643bdfbf3940'::bytea) WHERE "core_mymodel"."id" = 123

내가 몇 반복이 완료 했어,하지만 매우 빠르게 루프에 대한 업데이트하려고 할 때 영원히 중단 행을 발견. 테스트로 나는 다른 칼럼을 업데이트하기 위해 마이 그 레이션을 변경했고, for 루프가 동일한 행을 만났을 때 계속 이동하기 전에 약 30 초 동안 걸렸지 만, 완료되었습니다. 반면에 denorm_locs를 업데이트하려고 시도하면 전혀 완료되지 않습니다 .

datid | datname | pid | usesysid | usename | application_name | client_addr | client_hostname | client_port |   backend_start   |   xact_start   |   query_start   |   state_change   | waiting | state |                     query                     
-------+----------+------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
16384 | rdsadmin | 1668 |  10 | rdsadmin |     |    |     |    |        |        |        |        |   |  | <insufficient privilege> 
16388 | mydb  | 3658 | 16386 | mydb  |     | 10.1.250.12 | 10.1.250.12  |  33658 | 2014-07-17 14:57:09.592821+00 | 2014-07-17 15:06:14.208525+00 | 2014-07-17 15:06:14.208525+00 | 2014-07-17 15:06:14.20853+00 | f  | active | SELECT * FROM pg_stat_activity; 
16388 | mydb  | 2241 | 16386 | mydb  |     | 10.1.0.7 | 10.1.0.7  |  51805 | 2014-07-16 20:25:13.621986+00 | 2014-07-16 20:25:14.474963+00 | 2014-07-16 20:25:15.040656+00 | 2014-07-16 20:25:15.040661+00 | f  | active | UPDATE "core_mymodel" SET "denorm_locs" = ST_GeomFromEWKB('\x0104000020e61000000100000001010000009b1da9bef36c54c0815ce2c803b93c40'::bytea) WHERE "core_mymodel"."id" = 1000 
16388 | mydb  | 2744 | 16386 | mydb  |     | 10.1.250.12 | 10.1.250.12  |  33554 | 2014-07-17 14:42:09.569238+00 | 2014-07-17 14:50:58.94293+00 | 2014-07-17 14:50:58.960509+00 | 2014-07-17 14:50:58.960512+00 | t  | active | UPDATE "core_mymodel" SET "denorm_locs" = ST_GeomFromEWKB('\x0104000020e610000001000000010100000086c954c1a81054c01d5a643bdfbf3940'::bytea) WHERE "core_mymodel"."id" = 123 
(4 rows) 

당신은 아직 완료되지 지난 밤에서 다른 쿼리를 볼 수 있습니다

여기 SELECT * FROM pg_stat_activity;의 출력입니다.

여기 SELECT relation::regclass, * FROM pg_locks WHERE NOT granted;의 :

relation | locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath 
----------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+-----------+---------+---------- 
      | transactionid |   |   |  |  |   |  14771737 |   |  |   | 5/11758   | 2744 | ShareLock | f  | f 
(1 row) 

여기 SELECT relation::regclass, * FROM pg_locks WHERE granted;입니다 :

      relation       | locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid |  mode  | granted | fastpath 
------------------------------------------------------------+---------------+----------+----------+-------+-------+------------+---------------+---------+-------+----------+--------------------+------+------------------+---------+---------- 
pg_locks             | relation  | 16388 | 11090 |  |  |   |    |   |  |   | 3/39008   | 3658 | AccessShareLock | t  | t 
                  | virtualxid |   |   |  |  | 3/39008 |    |   |  |   | 3/39008   | 3658 | ExclusiveLock | t  | t 
core_mymodel_mymodel3s           | relation  | 16388 | 19314 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel3             | relation  | 16388 | 19386 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_location            | relation  | 16388 | 19243 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_denorm_locs_id         | relation  | 16388 | 2725185 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_title_index          | relation  | 16388 | 2700114 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_detail_html_index        | relation  | 16388 | 2700113 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
title              | relation  | 16388 | 2645243 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_mymodel5_id_6dce956e264c19df_uniq | relation  | 16388 | 2576203 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_mymodels_query         | relation  | 16388 | 2484701 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_mymodel2_id          | relation  | 16388 | 333698 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_mymodel4_id       | relation  | 16388 | 302491 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_mymodel5_id      | relation  | 16388 | 302490 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_start_date          | relation  | 16388 | 302487 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_end_date          | relation  | 16388 | 302483 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel_pkey           | relation  | 16388 | 302308 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
core_mymodel             | relation  | 16388 | 19293 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | t 
                  | virtualxid |   |   |  |  | 4/44  |    |   |  |   | 4/44    | 2241 | ExclusiveLock | t  | t 
core_mymodel_mymodel3s           | relation  | 16388 | 19314 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel3             | relation  | 16388 | 19386 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_location            | relation  | 16388 | 19243 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_denorm_locs_id         | relation  | 16388 | 2725185 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_title_index          | relation  | 16388 | 2700114 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_detail_html_index        | relation  | 16388 | 2700113 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
title              | relation  | 16388 | 2645243 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_mymodel5_id_6dce956e264c19df_uniq | relation  | 16388 | 2576203 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_mymodels_query         | relation  | 16388 | 2484701 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_mymodel2_id          | relation  | 16388 | 333698 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_mymodel4_id       | relation  | 16388 | 302491 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_mymodel5_id      | relation  | 16388 | 302490 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_start_date          | relation  | 16388 | 302487 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_end_date          | relation  | 16388 | 302483 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel_pkey           | relation  | 16388 | 302308 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
core_mymodel             | relation  | 16388 | 19293 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | t 
                  | virtualxid |   |   |  |  | 5/11758 |    |   |  |   | 5/11758   | 2744 | ExclusiveLock | t  | t 
core_mymodel_mymodel4_id       | relation  | 16388 | 302491 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_mymodel3s_pkey          | relation  | 16388 | 302312 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel_mymodel3s_mymodel_id_5dcd946e263a391f_uniq   | relation  | 16388 | 302310 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel_start_date          | relation  | 16388 | 302487 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
                  | transactionid |   |   |  |  |   |  14771737 |   |  |   | 4/44    | 2241 | ExclusiveLock | t  | f 
core_location_loc_id          | relation  | 16388 | 302470 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel3_pkey           | relation  | 16388 | 302361 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel_end_date          | relation  | 16388 | 302483 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_pkey           | relation  | 16388 | 302308 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel3_name           | relation  | 16388 | 333705 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel_denorm_locs_id         | relation  | 16388 | 2725185 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_title_index          | relation  | 16388 | 2700114 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel3_name_129459df841fd9de_uniq      | relation  | 16388 | 333668 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel             | tuple   | 16388 | 19293 | 93104 |  3 |   |    |   |  |   | 5/11758   | 2744 | ExclusiveLock | t  | f 
core_mymodel3_location_id          | relation  | 16388 | 302517 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel3_name_index          | relation  | 16388 | 2700014 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel_mymodel5_id      | relation  | 16388 | 302490 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_mymodel2_id          | relation  | 16388 | 333698 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_mymodel5_id_6dce956e264c19df_uniq | relation  | 16388 | 2576203 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_mymodel3s_mymodel_id         | relation  | 16388 | 302488 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel3_address_id          | relation  | 16388 | 302516 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel3_mymodel2_id          | relation  | 16388 | 333704 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel             | relation  | 16388 | 19293 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_location_pkey           | relation  | 16388 | 302276 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel_mymodel3s_mymodel3_id         | relation  | 16388 | 302489 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
title              | relation  | 16388 | 2645243 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_detail_html_index        | relation  | 16388 | 2700113 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_mymodels_query         | relation  | 16388 | 2484701 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_location_pkey           | relation  | 16388 | 302276 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel_mymodel3s_mymodel3_id         | relation  | 16388 | 302489 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
title              | relation  | 16388 | 2645243 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_detail_html_index        | relation  | 16388 | 2700113 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_mymodels_query         | relation  | 16388 | 2484701 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_mymodel3s_mymodel_id         | relation  | 16388 | 302488 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel3_address_id          | relation  | 16388 | 302516 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel3_mymodel2_id          | relation  | 16388 | 333704 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel             | relation  | 16388 | 19293 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel3_name_index          | relation  | 16388 | 2700014 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel_mymodel5_id      | relation  | 16388 | 302490 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_mymodel2_id          | relation  | 16388 | 333698 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_mymodel5_id_6dce956e264c19df_uniq | relation  | 16388 | 2576203 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_denorm_locs_id         | relation  | 16388 | 2725185 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_title_index          | relation  | 16388 | 2700114 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel3_name_129459df841fd9de_uniq      | relation  | 16388 | 333668 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel3_location_id          | relation  | 16388 | 302517 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel_end_date          | relation  | 16388 | 302483 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_pkey           | relation  | 16388 | 302308 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel3_name           | relation  | 16388 | 333705 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
                  | transactionid |   |   |  |  |   |  14771961 |   |  |   | 5/11758   | 2744 | ExclusiveLock | t  | f 
core_location_loc_id          | relation  | 16388 | 302470 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel3_pkey           | relation  | 16388 | 302361 |  |  |   |    |   |  |   | 5/11758   | 2744 | AccessShareLock | t  | f 
core_mymodel_mymodel3s_mymodel_id_5dcd946e263a391f_uniq   | relation  | 16388 | 302310 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
core_mymodel_start_date          | relation  | 16388 | 302487 |  |  |   |    |   |  |   | 5/11758   | 2744 | RowExclusiveLock | t  | f 
core_mymodel_mymodel4_id       | relation  | 16388 | 302491 |  |  |   |    |   |  |   | 4/44    | 2241 | RowExclusiveLock | t  | f 
core_mymodel_mymodel3s_pkey          | relation  | 16388 | 302312 |  |  |   |    |   |  |   | 4/44    | 2241 | AccessShareLock | t  | f 
(91 rows) 

를 지금까지 내가 말할 수있는, 아이디 = 123 행에 액세스하려는 유일한 클라이언트 업데이트하려고 같은 클라이언트입니다 그것. 또한 모든 열에 대해 잠금을 요청하는 이유는 모르겠지만 DB를 양식을 가져 왔을 때 기본 키 목록이었고 쿼리가 이미 완료되었습니다. 또한, 마이그레이션 프로세스가 중단 될 때이를 제거해야 할 경우 많은 권한 부여 잠금이 적용됩니다. 데이터베이스를 다시 시작하려고했지만 마이 그 레이션이 여전히 실패합니다.

또한 SELECT pg_terminate_backend(pid);은 백엔드를 중지하지 않고 DB 만 다시 시작하면 문제가 해결됩니다.

+0

마지막 요점은 흥미 롭습니다. 라이브러리처럼 'CHECK_FOR_INTERRUPTS'이 아닌 무언가에 대한 작업을 계속하는 것이 좋습니다. 당신이 예민한 경우'gdb '를 붙이고 그들 중 하나의 백 트레이스를 얻으려고 시도하십시오. https://wiki.postgresql.org/wiki/Getting_a_stack_trace_of_a_running_PostgreSQL_backend_on_Linux/BSD –

+0

고마워, 나는 그것을 줄 것이다. –

+0

불행히도, 나는 RDS를 사용하고 있기 때문에 할 수 없다고 생각합니다. –

답변

0

데이터베이스를 재부팅하고 새 필드를 삭제 한 다음 다시 읽은 다음 VACUUM ANALYZE VERBOSE을 실행했습니다. UPDATE 쿼리는 현재 완료되고있는 것처럼 보이지만 일부는 무작위로 나머지 것보다 몇 배 더 오래 걸릴 것입니다.