我有一个表,其中数据占用 200 GB 大小,其上的 6 个索引占用 180 GB 大小。它膨胀了 30%,所以我想回收它占用的不需要的空间。它聚集在job_id_idx 索引上。
那么要回收空间我需要使用cluster命令还是vacuum full命令?
这两个命令有什么区别?
是vacuum full为了通过一些列相同cluster的命令?
两个命令中是否都重新创建了索引?
在我的情况下,哪一个会更快?
PostgreSQL 数据库版本为 9.1
这是我的实际问题的一个最小示例:
create table t(id serial primary key, rnd double precision);
Run Code Online (Sandbox Code Playgroud)
当然,您可以使用returning子句返回插入的列:
with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID | RND |
|----|----------------|
| 9 | 0.203221440315 |
*/
Run Code Online (Sandbox Code Playgroud)
你也可以返回一个文字:
with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *, 1.0 dummy;
/*
| ID | RND | DUMMY |
|----|----------------|-------|
| 11 | 0.594980469905 | 1 …Run Code Online (Sandbox Code Playgroud) 鉴于表:
Column | Type
id | integer
latitude | numeric(9,6)
longitude | numeric(9,6)
speed | integer
equipment_id | integer
created_at | timestamp without time zone
Indexes:
"geoposition_records_pkey" PRIMARY KEY, btree (id)
Run Code Online (Sandbox Code Playgroud)
该表有 2000 万条记录,相对而言,这不是一个大数目。但它会使顺序扫描变慢。
我怎样才能获得max(created_at)每个的最后一条记录 ( ) equipment_id?
我已经尝试了以下两个查询,其中有几个变体,我已经阅读了本主题的许多答案:
select max(created_at),equipment_id from geoposition_records group by equipment_id;
select distinct on (equipment_id) equipment_id,created_at
from geoposition_records order by equipment_id, created_at desc;
Run Code Online (Sandbox Code Playgroud)
我也尝试过创建 btree 索引,equipment_id,created_at但 Postgres 发现使用 seqscan 更快。强制enable_seqscan = off也没有用,因为读取索引与 seq 扫描一样慢,可能更糟。
查询必须定期运行,始终返回最后一个。
使用 Postgres …
当使用单个 SQL 命令运行 PostgreSQL 时,错误代码按预期返回:
% psql -c "SELECT * FROM AWDASDASDASDAS" my_db
ERROR: relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
1
Run Code Online (Sandbox Code Playgroud)
但是在运行文件时,错误被抑制:
% psql -f test.sql my_db
psql:test.sql:1: ERROR: relation "awdasdasdasdas" does not exist
LINE 1: SELECT * FROM AWDASDASDASDAS
% echo $?
0
Run Code Online (Sandbox Code Playgroud)
知道如何恢复这些错误吗?
我需要对数据库的独占访问。是否可以使用 SQL 命令从 postgres 数据库中“分离”所有其他用户。或者可能关闭所有其他连接,然后获得独占访问权限。
这是用于单元测试,并且测试仅手动运行,因此不涉及危险。只有旧的死连接会受到影响。
没有其他用户连接到这些 unittest 数据库。
旧的死联系来自发展。当正在编写或失败的测试没有退出干净时,这种情况总是发生。
如果有人在生产场景中断开其他用户的连接后还需要将其锁定一段时间,请参阅下面 Scott Marlowe 的回答:https : //dba.stackexchange.com/a/6184/2024
另请参阅 dba 上的类似问题:如何在不停止服务器的情况下删除与特定数据库的所有连接?
双方C:\PostgreSQL并C:\PostgreSQL\data拥有postgres完全访问和管理权限的用户。
我以管理员身份从 postgres 用户运行 postgresql-9.1.2-1-windows.exe。目标C:\PostgreSQL
我尝试的每一种方式都得到“数据库集群初始化失败”。
问题
我正在尝试设置 PostGIS 以与 GeoDjango 一起使用。
我能够手动安装 PostGIS。我是 PostgreSQL 的新手,我对所有这些都感到信心危机。第一次从 MySQL 到 PostgreSQL。
来自 C:\Users\Larry\AppData\Local\Temp\install-postgresql.log 的相关日志输出:
WScript.Network initialized...
Called IsVistaOrNewer()...
'winmgmts' object initialized...
Version:6.1
MajorVersion:6
Ensuring we can read the path C: (using icacls) to Larry:
Executing batch file 'radA3CF7.bat'...
Output file does not exists...
Called IsVistaOrNewer()...
'winmgmts' object initialized...
Version:6.1
MajorVersion:6
Ensuring we can read the path C:\PostgreSQL (using icacls) …Run Code Online (Sandbox Code Playgroud) 给定数据库角色,定义为存储过程user1的函数something()和创建的视图,如下所示:
CREATE VIEW view1 AS select * from something()
Run Code Online (Sandbox Code Playgroud)
并且,鉴于此权限:
REVOKE ALL ON FUNCTION something FROM user1
REVOKE SELECT ON view1 FROM user1
Run Code Online (Sandbox Code Playgroud)
当我运行时SELECT * FROM view1,出现错误permission denied for function something()。
我的问题是,如果我撤销对视图的选择权限,为什么会调用该函数?我期待收到类似的东西:
permission denied for relation view1
Run Code Online (Sandbox Code Playgroud)
谢谢!
我station_logs在 PostgreSQL 9.6 数据库中有一个表:
Column | Type |
---------------+-----------------------------+
id | bigint | bigserial
station_id | integer | not null
submitted_at | timestamp without time zone |
level_sensor | double precision |
Indexes:
"station_logs_pkey" PRIMARY KEY, btree (id)
"uniq_sid_sat" UNIQUE CONSTRAINT, btree (station_id, submitted_at)
Run Code Online (Sandbox Code Playgroud)
我试图level_sensor根据submitted_at, 对于每个station_id. 大约有 400 个唯一station_id值,每个station_id.
创建索引之前:
EXPLAIN ANALYZE
SELECT DISTINCT ON(station_id) station_id, submitted_at, level_sensor
FROM station_logs ORDER BY station_id, submitted_at DESC;
Run Code Online (Sandbox Code Playgroud)
唯一(成本=4347852.14..4450301.72行=89宽度=20)(实际时间=22202.080..27619.167行=98循环=1) -> Sort …
postgresql performance greatest-n-per-group postgresql-9.6 query-performance
SELECT json_array_elements('["one", "two"]'::json)
Run Code Online (Sandbox Code Playgroud)
给出结果
| json_array_elements | | :------------------ | | “一个” | | “两个” |
我想要相同但没有引号:
one
two
Run Code Online (Sandbox Code Playgroud)
看起来我不能->>在这里使用,因为我在 JSON 中没有字段名称。它只是一个字符串数组。
Postgres 版本:PostgreSQL 10.0 on x86_64-apple-darwin,由 i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (基于 Apple Inc. build 5658) (LLVM build 2336.11.00) 编译,64-少量
我们有一个只有大约 500 行的表,但这对我们来说非常重要。
我想查看发生在此表上的所有更改。应使用时间戳跟踪更改。
我不希望在应用程序代码中进行跟踪,因为我也想跟踪通过psqlshell发生的更改。
我对特定于 PostgreSQL 的解决方案感到满意,因为在这种情况下我没有使用不同的数据库。
postgresql ×10
performance ×2
audit ×1
index ×1
json ×1
permissions ×1
vacuum ×1
view ×1
windows ×1