我正在运行一个 Django 1.8 站点,由 nginx 和 Gunicorn 提供服务,后端为 Postgres 9.4。
当我连续对站点的 API 进行大量数据密集型调用时,我注意到大约百分之一的查询失败,并提供 nginx 404 页面。
我的第一个假设是 Postgres 数据库在负载下嘎吱作响,但 Postgres 日志实际上向我显示了这一点:
2015-08-06 14:33:43 UTC:127.0.0.1(42287):dbuser@dbname:[3737]: LOG: could not send data to client: Broken pipe
2015-08-06 14:33:43 UTC:127.0.0.1(42287):dbuser@dbname:[3737]: STATEMENT: SELECT ... (psql statement)
2015-08-06 14:33:43 UTC:127.0.0.1(42287):dbuser@dbname:[3737]: FATAL: connection to client lost
2015-08-06 14:33:43 UTC:127.0.0.1(42287):dbuser@dbname:[3737]: STATEMENT: SELECT ... (psql statement)
Run Code Online (Sandbox Code Playgroud)
我认为这意味着 Postgres 实际上运行查询正常,但它无法将数据发送到客户端。
所以我认为要调试这个问题,我实际上应该看看 Django/Gunicorn/nginx。
我的理解正确吗?
insert into addresses(street, city, user_id)
select
'street1','LA', 2
where exists (select * from users where users.id = $$$$$ AND users.storeaddress='true'))
Run Code Online (Sandbox Code Playgroud)
仅当用户的 storeaddress 值为“true”时,我才想插入用户的地址。
我希望 sql 自动使用 2 代替 $$$$$。如何在sql中做到这一点?
如果我简单地用 user_id 代替 $$$$$ 我得到
HINT: There is a column named "user_id" in table "addresses", but it cannot be referenced from this part of the query.
Run Code Online (Sandbox Code Playgroud) 我有一个返回 CTE 的查询,如下所示
+-----------+-------------+
| node_id | ancestors |
|-----------+-------------|
| 1 | [] |
| 2 | [] |
| 3 | [1] |
| 4 | [2] |
| 5 | [4, 2] |
+-----------+-------------+
Run Code Online (Sandbox Code Playgroud)
我想要做的是加入表nodes并将该列中的 id 替换ancestors为表中的另一列nodes。这是我到目前为止的查询:
WITH RECURSIVE tree AS (
-- snip --
)
SELECT node.entity_id AS id,
array_remove(array_agg(parent_nodes.entity_id), NULL) AS ancestors
FROM tree
JOIN entity.nodes AS node ON node.id = tree.node_id
LEFT OUTER JOIN entity.nodes AS parent_nodes ON …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 bash 中编写一行快速代码,它将使用 psql 连接到数据库并删除两个表中的所有条目:
\connect some_db
DELETE from some_table
DELETE from another_table
Run Code Online (Sandbox Code Playgroud)
我想在一行 bash 脚本中执行此操作,如下所示:
psql -U <username> -c "\c some_db; DELETE from some_table; DELETE from another_table"
Run Code Online (Sandbox Code Playgroud)
但似乎我不能这样做,因为该\c命令创建了一个新的上下文,后续命令不适用于该上下文。
从 CLI 中用一两行代码执行此操作的正确方法是什么?
我正在尝试修改共享开发数据库,供开发团队用于应用程序开发/测试。
大多数集体工作(表名、视图等)都存储在公共环境中模式中,但我已经为每个用户设置了模式以用作临时空间。然而,真正的目标是用户使用其架构中的对象(如果存在),然后依赖其他对象,就像今天使用 search_path 的方式一样。
用一个例子可能会更好地描述这一点。假设团队正在开发一个汽车维修应用程序的数据库,其中包含一些表和视图:
public.automobiles
public.parts
public.inventory
public.mechanics
public.schedule
public.v_repairs -- view that joins fields from all tables above
Run Code Online (Sandbox Code Playgroud)
这非常有效,但假设开发人员(例如 Sally)想要测试一项新功能,用她自己的数据集来检查视觉反馈或阈值测试。她在自己的架构中创建了一个表sally.schedule。因为默认的search_path类似于"$user",public,所以她创建的任何简单查询都会在公开之前首先检查她的架构。例如:
SELECT * FROM schedule LEFT JOIN mechanics USING(mechanic_id);
Run Code Online (Sandbox Code Playgroud)
当 sally 连接到数据库时,这将使用 public.mechanics 和 sally.schedule。这正是预期的用途,但是在保存视图时,它通过插入架构来完全限定表名称。因此,如果将上面相同的查询创建为公共模式中的视图,它将如下所示:
SELECT * FROM public.schedule LEFT JOIN public.mechanics USING(mechanic_id);
Run Code Online (Sandbox Code Playgroud)
search_path 的魔力被否定了。当Sally连接到数据库调用视图时(SELECT * FROM v_mechanics_schedule ) 时,它会忽略她创建的 sally.schedule 表,而只使用公共表。
有没有办法在保存视图时不让 Postgres 存储表/视图对象的架构名称?
注意:这是我正在研究的新事物,但我从未真正需要过它,因为开发人员通常可以克隆应用程序、复制数据库并在自己的沙箱环境中工作。不需要一些巧妙的协作模式设置
我有一个包含三个整数列id和a的表b。
我想获取所有记录,其中a或b匹配指定参数排序id:
select id, a, b from t where a=x or b=x order by id
Run Code Online (Sandbox Code Playgroud)
请注意,和 的x值相同。ab
这里最合适的索引是什么?
更新:我们总是在列和中寻找相同的值,这一事实有什么用处吗?我们可以为此创建一个表达式索引吗?ab
我对以下脚本有疑问:
CREATE DOMAIN shop.amount AS numeric(8,2) DEFAULT 0 NOT NULL CHECK (value > 0::numeric);
CREATE TYPE shop.money AS (
m_amount shop.amount,
m_currency shop.currency
);
CREATE TABLE shop.order_item
(
oi_id bigserial NOT NULL,
oi_order_id bigint NOT NULL,
oi_article_id bigint NOT NULL,
oi_article_price shop.money NOT NULL,
CONSTRAINT oi_pk_id PRIMARY KEY (oi_id),
CONSTRAINT oi_fk_article_id FOREIGN KEY (oi_article_id)
REFERENCES shop.article (a_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT oi_pk_order_id FOREIGN KEY (oi_order_id)
REFERENCES shop."order" (o_id) MATCH SIMPLE
ON UPDATE NO …Run Code Online (Sandbox Code Playgroud) 考虑以下业务领域:
Airline具有唯一的航空公司 ID ( aid) 并包含零个或多个Planes。Plane有一个唯一的飞机 id ( pid) Airline(但来自不同国家的飞机Airlines可以有重叠ids)。Plane都有一个或多个Seats。Seat有一个唯一的座位 id ( sid) (但Seats不同的座位 idPlanes可能有重叠ids)。这是我解决这个问题的尝试:
CREATE SEQUENCE planes_seq;
CREATE TABLE Airlines (
aid INTEGER PRIMARY KEY DEFAULT nextval('planes_seq')
);
CREATE TABLE Planes (
aid INTEGER REFERENCES Airlines(aid)
, pid INTEGER
, PRIMARY KEY(aid, pid)
);
CREATE TABLE Seats ( …Run Code Online (Sandbox Code Playgroud) 如果我创建一个表。
CREATE TABLE foo AS
SELECT CASE WHEN random() > 0.5 THEN x END AS x
FROM generate_series(1,10) AS x;
Run Code Online (Sandbox Code Playgroud)
然后我在事务中运行以下命令
BEGIN;
SELECT count(*)
FROM foo
WHERE x IS NOT NULL;
--time
SELECT count(*)
FROM foo
WHERE x IS NOT NULL;
END;
Run Code Online (Sandbox Code Playgroud)
在什么交易级别下我的结果保证在交易中保持不变?
postgresql ×10
array ×1
command-line ×1
concurrency ×1
constraint ×1
cte ×1
index ×1
locking ×1
mvcc ×1
performance ×1
pgadmin ×1
pgadmin-3 ×1
psql ×1
recursive ×1
transaction ×1
view ×1