2013-07-15 2 views
1

Django ORM을 사용하여 장고 데이터베이스에 개체를로드하는 스크립트를 작성했습니다. 기본 데이터베이스는 Postgres입니다.django.db.utils.DatabaseError : 공유 메모리 없음

잠시 동안 행복하게 실행 한 후, 스크립트는이 오류와 함께 실패합니다

django.db.utils.DatabaseError: out of shared memory 
HINT: You might need to increase max_locks_per_transaction. 

나는이 추측하고있어 내 스크립트의 효율성보다는 데이터베이스 설정에 문제가 있습니다.

스크립트는 CSV 파일을 반복하고 CSV 파일의 모든 행에 대해 데이터베이스 개체를 만듭니다. 일반적으로 몇 천 가지 개체를 만들 수 있습니다. 나는 some background material on database efficiency in Django을 읽었다. 몇 가지 실수를 배제 할 수 있습니다. 쿼리 세트를 반복하거나 __in 쿼리 또는 OFFSET을 사용하지 않습니다.

그러나 나는 데이터베이스의 필드에 많은 인덱스를 가지고 있으며, 오브젝트를 생성하고 저장할 때마다 장고는 모든 인덱스를 업데이트해야한다고 생각합니다. 예를 들어 StoreItem 필드에 6 개의 색인이 있습니다.

for item in csv_rows: 
    s, created = StoreItem.objects.get_or_create(display_url=item['display_url'], \ 
     retailer_img_url=item['retailer_img_url'],store=store_obj) 
    s.name = item['name'] 
    s.description = item['description'] 
    s.affiliate = item['affiliate'] 
    ... more stuff 
    s.save() 

두 질문 :

  1. 는 데이터베이스 인덱스를 업데이트하면이 오류가 발생할 수 있음을 수 있습니까?
  2. 이 경우 어떻게 디버깅 할 수 있습니까?
+0

여기서 CSV의 몇 줄을 말하고 있습니까? –

+0

많지 않음. 일반적으로 수천입니다. – Richard

+0

인덱스가 원인 일 수 있다고 생각합니다. 내 테이블에 인덱스를 추가 한 후에이 오류가 발생했습니다 ... – monkut

답변

2

나는 빠른 구글이 있고 보는 자원의 몇 가지가있다 :이 http://www.postgresql.org/docs/9.1/static/runtime-config-locks.html

max_locks_per_transaction (integer)

The shared lock table tracks locks on max_locks_per_transaction * (max_connections + max_prepared_transactions) objects (e.g., tables); hence, no more than this many distinct objects can be locked at any one time. This parameter controls the average number of object locks allocated for each transaction; individual transactions can lock more objects as long as the locks of all transactions fit in the lock table. This is not the number of rows that can be locked; that value is unlimited. The default, 64, has historically proven sufficient, but you might need to raise this value if you have clients that touch many different tables in a single transaction. This parameter can only be set at server start.

Increasing this parameter might cause PostgreSQL to request more System V shared memory than your operating system's default configuration allows. See Section 17.4.1 for information on how to adjust those parameters, if necessary.

When running a standby server, you must set this parameter to the same or higher value than on the master server. Otherwise, queries will not be allowed in the standby server.

에서 가져 인용

  1. postgresql: out of shared memory?
  2. http://www.databasesoup.com/2012/06/postgresqlconf-maxlockspertransaction.html

따라서 max_locks_per_transaction의 기본값을 확인하여 적절한 값을 확인해야합니다.

마찬가지로 기본 설치 인 postgres를 사용하는 경우 일부 인스턴스와 마찬가지로 모든 다른 기본값을 확인해야합니다 기본값을 너무 낮게 설정할 수 있습니다.