如果我做对了,设置是分层的:
postgres.conf->
postgres.auto.conf (ALTER SYSTEM)->
ALTER DATABASE(where applicable)->
ALTER USER(where applicable)->
SET SESSION(where applicable)->
SET LOCAL(where applicable)
Run Code Online (Sandbox Code Playgroud)
假设我已经在某个时候enable_seqscan设置了off。SHOW或current_setting(或pg_settings将仅显示当前值。但是要检查为什么我关闭它,我必须检查整个链条。例如,我怀疑有人为每个用户或每个数据库设置了它,或者在auto.conf- 要找到设置的范围,我必须全部检查。否则重置值可能会失败,例如:
vao=# show enable_seqscan;
enable_seqscan
----------------
off
(1 row)
vao=# set enable_seqscan to default;
SET
vao=# show enable_seqscan;
enable_seqscan
----------------
off
(1 row)
Run Code Online (Sandbox Code Playgroud)
因为对于用户vao,或者对于数据库vao,或者更深...
找到当前值的主要来源的捷径是什么? .. 或者最好是设置的所有来源的矩阵。是否有任何界面或猴子黑客?
更新以反映出色的 Abelisto 答案:
source, sourcefile from pg_settings是一个很好的信息来源,我仍然要求一种猴子的方式来检查这些原因:看到database或user在source我必须重新登录 - 显然。因此,在它们实际应用于会话之前,我无法检查它们。本地和会话集都显示了 SESSION(这也很合理),最后我仍然必须逐步将所有层次结构重置为默认值。例如:
vao=# select setting, source, sourcefile …Run Code Online (Sandbox Code Playgroud) 假设我有一个 100 万条记录表。每条记录都可以通过 id 唯一标识。如果该表被划分为 10 个游标,并且 Web 客户端为每个游标发出 10 个请求,说将姓氏字段更改为大写,并且有 10 个内核可用,那么工作速度会快 10 倍吗?还是比直接 for .. 循环对整个表进行常规处理更快?
我知道 PostgreSQl 无法运行进行更改的并行查询。我试图通过使用代码来解决这个问题。请注意,这些记录是独立的。所有更改都严格针对记录。
slava=# explain (analyze) SELECT * from pending_posts WHERE user_id <> 1456
slava-# ;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Seq Scan on pending_posts (cost=0.00..17906.00 rows=999994 width=14) (actual time=0.021..231.803 rows=999994 loops=1)
Filter: (user_id <> 1456)
Rows Removed by Filter: 6
Planning time: 0.108 ms
Execution time: 289.845 ms
(5 rows)
slava=# explain (analyze) select * from pending_posts where user_id <> 1456 order_by user_id;
ERROR: syntax error at or near "order_by"
LINE 1: ...select * from pending_posts where user_id <> 1456 order_by u...
^
slava=# …Run Code Online (Sandbox Code Playgroud) 我创建了一个 Ruby on Rails 应用程序只是为了向同事演示,然后删除了该应用程序,但我没有删除同样使用 Postgresql 创建的数据库。
我像这样进入 posgresql:
psql postgres
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------------------------+--------+----------+-------------+-------------+-------------------
GenerateApp_development | danale | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
GenerateApp_test | danale | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
GeneratorApp_development | danale | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
GeneratorApp_test | danale | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
Overtime_development | danale | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
Overtime_test …Run Code Online (Sandbox Code Playgroud) 谷歌搜索后,很明显你不能从函数运行真空:
Postgres 邮件 - plpgsql 函数中的真空行为
由于内存管理限制,您无法从函数内部运行 VACUUM。在当前来源中有一个错误检查以防止您尝试。
Stackoverflow - 无法从函数或多命令字符串执行 VACUUM
加载数据后,我想对受影响的表进行“真空分析”,既要从已删除的记录中恢复空间,又要准确反映新内容。[...] 当我运行这个时,我得到:
ERROR: VACUUM cannot be executed from a function or multi-command string
但是我真的需要在运行某个函数时“自动”运行vacuum:这个函数更新了很多记录(如果不是全部),所以死行的数量增加得非常快,其他查询性能下降很多。问题是该函数是从另一个程序调用的。任何最佳实践?我应该在函数执行后手动将真空语句添加到一段单独的代码中,就像
sql = SELECT my_function()
<program.execute(sql)>
sql = VACUUM [FULL | ANALYZE] my_updated_table
<program.execute(sql)>
我正在尝试使用视图替换我们应用程序中的表。它在大多数情况下运行良好,但我无法克服这个错误:“列必须出现在 GROUP BY 子句中或用于聚合函数中”
重现步骤:
CREATE TABLE example_t (
did serial PRIMARY KEY,
a text,
b text
);
INSERT INTO example_t(a, b) VALUES ('a', 'b');
CREATE VIEW example_t_v AS
SELECT t.did as did, t.a as a, t.b as b
FROM example_t t;
Run Code Online (Sandbox Code Playgroud)
现在这个查询工作正常:
SELECT t.a, t.b FROM example_t t GROUP BY (t.did);
Run Code Online (Sandbox Code Playgroud)
但是来自视图的相同查询失败:
SELECT t.a, t.b FROM example_t_v t GROUP BY (t.did);
ERROR: column "t.a" must appear in the GROUP BY clause or be used in an aggregate function
LINE …Run Code Online (Sandbox Code Playgroud) 我知道这个问题必须经常出现,但我似乎找不到最近的答案来解决更简单的问题,比如我正在尝试做的事情。我读过几个类似的问题。
我正在使用 PostgreSQL 9.4 并有 2 个表,每个表都有接近 200M 的行,包含两种类型的指标。列是 the_date、feature_A、feature_B 和 24 小时指标。两个表中的指标差异很大,因此无法合并这些表。或者至少我不这么认为。某些指标不适用于所有功能,这就是将它们分开的原因。两个表在日期列上都有索引。
当我尝试执行连接它们的查询时,如果我指定一个日期,查询将在大约 3 秒内返回,这对我来说似乎是合理的。但是,如果我只是简单地进行a.the_date between '8/1/2017' and '8/1/2017'查询,现在基本上需要永远。我在 10 分钟后杀死它,因为我不耐烦。显然 PostgreSQL 不再使用索引。
为什么会这样,我怎样才能让它再次使用索引?我想做像a.the_date between '7/1/2017' and '7/31/2017'or 之类的查询a.the_date >= '1/1/2017'。现在我只是有一个 python 程序,一次运行一个日期的查询,然后汇总结果,这似乎是一个黑客。但至少它是可预测的,并且每天 3 秒快得多。
查询:
select a.feature_a, a.feature_b, count(*) tot_ct,
count(case when b.br01 > 0 and a.bc01 <= 10 then 1 end) cx01,
count(case when b.br02 > 0 and a.bc02 <= 10 then 1 end) cx02,
count(case when b.br03 > 0 and …Run Code Online (Sandbox Code Playgroud) 我将数百万行插入 PostgreSQL 9.5 数据库并观察到内存使用量的持续增长。由于表不是那么大,并且执行的操作(插入触发 Pl/Python 函数)不应该那么昂贵,我想知道为什么会发生这种情况。
目前 PostgreSQL 使用了大约 50 GB 的总可用空间 60 GB。我想了解 PostgreSQL 如何使用这 50 GB,尤其是因为我担心该进程会耗尽内存。
[更新] 今晚 PostgreSQL 内存不足,被操作系统杀死。
$ pg_top
last pid: 13535; load avg: 1.26, 1.41, 1.42; up 2+02:57:11 19:29:26
3 processes: 1 running, 2 sleeping
CPU states: 12.4% user, 0.0% nice, 0.1% system, 87.4% idle, 0.0% iowait
Memory: 63G used, 319M free, 192M buffers, 28G cached
DB activity: 2 tps, 0 rollbs/s, 0 buffer r/s, 100 hit%, 42 row r/s, 0 row w/s
DB …Run Code Online (Sandbox Code Playgroud) 我有一个表,其中的架构如下:
CREATE TABLE table1(
account VARCHAR(64) NOT NULL,
json JSONB NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
我想在其上创建一个索引,以允许我运行以下查询:
SELECT *
FROM table1
WHERE account = :account
AND json ... -- Some JSON search expression
Run Code Online (Sandbox Code Playgroud)
所以我尝试在这个表上添加一个索引,如下所示:
CREATE INDEX index1 ON table1 USING GIN (account, json);
Run Code Online (Sandbox Code Playgroud)
我得到:
ERROR: data type character varying has no default operator class for
access method "gin"
Hint: You must specify an operator class for the index or define a default
operator class for the data type.
Run Code Online (Sandbox Code Playgroud)
我试过添加_varchar_ops:
CREATE INDEX …Run Code Online (Sandbox Code Playgroud) 我正在输入类似的内容:
psql --single-transaction --quiet my_db < my_file.sql
Run Code Online (Sandbox Code Playgroud)
当脚本失败时,我收到很多行说:current transaction is aborted, commands ignored until end of transaction block.
在 linux 中我可以使用| more管道传输结果,但在 Windows 中这可能需要很多时间(显示第一个错误,因为管道不是管道)。
我的问题:
有没有办法只得到第一个错误?