我的堆栈包括 django (1.4.3)、psycopg2 (0.0.3) 和 postgres (9.1)。此外,我正在使用 psycogreen.gevent.patch_psycopg,因为我正在使用 gunicorn 和 gevent 提供我的 django。
一切似乎都很愉快,但我得到了很多(约 40 个)打开的数据库连接。直到我在 django 数据库设置中将 'autocommit' 设置为 True 之前,它们都处于“事务空闲”状态。现在他们都只是“空闲”。
这是我的 pg_top 输出示例。
last pid: 22043; load avg: 0.09, 0.05, 0.05; up 6+21:49:58 16:21:08
45 processes: 45 sleeping
CPU states: 3.0% user, 0.9% nice, 0.2% system, 96.0% idle, 0.0% iowait
Memory: 871M used, 130M free, 32M buffers, 530M cached
Swap: 10M used, 246M free, 2192K cached
PID USERNAME PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND
10035 …Run Code Online (Sandbox Code Playgroud) 在一个应用程序中参考Django文档中的多个数据库的示例,
https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example
"它也没有考虑事务与数据库利用策略的交互."
我如何处理上述交互.
场景是这样的:
我使用postgresql作为我的数据库.我已经设置了一个副本,并希望所有对"auth"表的读取都转到副本.根据文档,我写了一个数据库路由器.现在每当我尝试登录我的应用程序时都会抛出以下错误.
DatabaseError: cannot execute UPDATE in a read-only transaction.
Run Code Online (Sandbox Code Playgroud)
当Django试图保存"last_login"时间时会发生这种情况.因为,在同一视图中,它首先从副本提取记录,然后尝试更新last_login时间.由于它发生在一个事务中,因此使用相同的数据库,即副本.
我该如何处理?
思考?
我想使用date_trunc SQL函数,但它似乎不适用于时区.
用Django测试1:
from django.db import connection
cursor = connection.cursor()
cursor.execute("""
SELECT (date_trunc('day', when_start)) AS "d",
COUNT("stats_histo_call"."id") AS "agg"
FROM "stats_histo_call"
WHERE ("stats_histo_call"."offre_id" = 28
AND "stats_histo_call"."when_start" BETWEEN '2014-08-04 00:00:00+02:00' and '2014-08-08 23:59:59.999999+02:00')
GROUP BY (date_trunc('day', when_start))""")
print cursor.fetchone()
print cursor.fetchone()
print cursor.fetchone()
print cursor.fetchone()
print cursor.fetchone()
Run Code Online (Sandbox Code Playgroud)
postgres日志中的SQL查询:
2014-08-21 15:18:31 CEST LOG: instruction : SELECT (date_trunc('day', when_start)) AS "d", COUNT("stats_histo_call"."id") AS "agg" FROM "stats_histo_call" WHERE ("stats_histo_call"."offre_id" = 28 AND "stats_histo_call"."when_start" BETWEEN '2014-08-04 00:00:00+02:00' and '2014-08-08 23:59:59.999999+02:00') GROUP BY (date_trunc('day', …Run Code Online (Sandbox Code Playgroud) 所以我正在尝试将SearchVectorField添加到 Django 中的模型中:
class JobPosting(models.Model):
...
...
search_vector = SearchVectorField()
Run Code Online (Sandbox Code Playgroud)
我知道它应该是nullable或具有默认值才能迁移,因此我删除了表中的所有条目以防止出现此问题。
但是,运行时出现以下错误makemigrations:
You are trying to add a non-`nullable` field 'search_vector' to jobposting without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now
(will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an …Run Code Online (Sandbox Code Playgroud) 我遇到了Django应用程序的问题.对模型Scope的查询极其缓慢,经过一些调试后我仍然不知道问题所在.
当我scope = Scope.objects.get(pk='Esoterik I')在django中查询db 时需要5到10秒.该数据库的主键少于10个条目和索引.所以这太慢了.在db上执行等效查询时,SELECT * FROM scope WHERE title='Esoterik I';一切正常,只需要大约50ms.
如果我使用一组结果进行查询,scope_list = Scope.objects.filter(members=some_user)然后执行例如print(scope_list)或迭代列表的元素,则会出现同样的问题.查询本身只需要几毫秒,但元素的打印或迭代再次需要5到10秒,但该集只有两个条目.
数据库后端是Postgresql.问题在本地开发服务器和apache上发生相同.
这里是模型的代码:
class Scope(models.Model):
title = models.CharField(primary_key=True, max_length=30)
## the semester the scope is linked with
assoc_semester = models.ForeignKey(Semester, null=True)
## the grade of the scope. can be Null if the scope is not a class
assoc_grade = models.ForeignKey(Grade, null=True)
## the timetable of the scope. can be null if the scope is not direct associated with …Run Code Online (Sandbox Code Playgroud) database django performance django-queryset django-postgresql
安装django_postgrespool并相应地配置我的项目后,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\Administrator\Documents\my_proj\my_module.py", line 14, in <module>
from cortex.my_app.models import MyModel
File "C:\Users\Administrator\Documents\my_proj\my_app\models.py", line 3, in <module>
from django.db import models
File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\django\db\__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\django\db\__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\django\db\utils.py", line 92, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\Administrator\virtual_environments\spyker\lib\site-packages\django\db\utils.py", line 44, in load_backend
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'django_postgrespool' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', …Run Code Online (Sandbox Code Playgroud) 我正在heroku上设置我的django项目.我一直在关注文档,但是当foreman start我收到一个我无法弄清楚的错误时.我已经设置了我的引擎文件,但它似乎不想工作.
完全追溯:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in …Run Code Online (Sandbox Code Playgroud) 我在我的django应用程序中使用postgres,我在数据库中手动创建了hstore扩展.但是,当我运行测试时,它会尝试创建一个新数据库,并在找不到hstore扩展时失败.
我收到以下错误:
django.db.utils.ProgrammingError: hstore type not found in the database. please install it from your 'contrib/hstore.sql' file
Run Code Online (Sandbox Code Playgroud)
我按照这篇文章更新了我的迁移,结果如下:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
from django.conf import settings
from django.contrib.postgres.operations import HStoreExtension
import django.contrib.postgres.fields
import django.contrib.postgres.fields.hstore
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
HStoreExtension(),
migrations.CreateModel(
name='AppUser',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('created_date', models.DateTimeField(auto_now_add=True)),
('modified_date', models.DateTimeField(auto_now=True)),
('lock', models.BooleanField(default=False)),
('username', models.CharField(max_length=100)),
('email', models.EmailField(max_length=150)),
('social_data', django.contrib.postgres.fields.hstore.HStoreField(blank=True, default='')), …Run Code Online (Sandbox Code Playgroud) 我有一个像下面这样的元组列表 -
[(float.inf, 1.0), (270, 0.9002), (0, 0.0)]
我正在寻找一个简单的序列化器/反序列化器,它可以帮助我将此元组存储在 PostgreSQL 的 jsonb 字段中。
我尝试使用JSONEncoder().encode(a_math_function)但没有帮助。
我在尝试将上述列表存储在 jsonb 字段中时遇到以下错误 -
django.db.utils.DataError: invalid input syntax for type json
LINE 1: ...", "a_math_function", "last_updated") VALUES (1, '[[Infinit...
DETAIL: Token "Infinity" is invalid.
Run Code Online (Sandbox Code Playgroud)
注意:字段 a_math_function 是 JSONField() 类型
我试图运行python3 manage.py migrate,但出现此错误:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django /db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type integer to uuid
LINE 1: ...BLE "core_post" ALTER COLUMN "id" TYPE uuid USING "id"::uuid
Run Code Online (Sandbox Code Playgroud)
我正在使用PostgreSQL
我的models.py: 链接到pastebin
我希望你能帮助我)
谢谢!
django postgresql django-models django-postgresql django-migrations
django ×10
python ×3
postgresql ×2
database ×1
django-orm ×1
gevent ×1
gunicorn ×1
heroku ×1
infinity ×1
performance ×1
psycopg2 ×1
python-3.x ×1