我的 PostgreSQL 服务器意外重启并显示以下消息:
2017-08-16 03:44:34 GMT LOG: received fast shutdown request
2017-08-16 03:44:34 GMT LOG: aborting any active transactions
2017-08-16 03:44:34 GMT FATAL: terminating connection due to administrator command
2017-08-16 03:44:34 GMT FATAL: terminating connection due to administrator command
2017-08-16 03:44:34 GMT LOG: autovacuum launcher shutting down
2017-08-16 03:44:34 GMT LOG: shutting down
2017-08-16 03:44:34 GMT LOG: database system is shut down
2017-08-16 03:46:04 GMT LOG: incomplete startup packet
2017-08-16 03:46:04 GMT LOG: database system was shut down at 2017-08-16 …Run Code Online (Sandbox Code Playgroud) 我正在使用 postgres 表验证规则并尝试CHECK为数组列设置约束。一个想法是只允许长度 > 0 的数组。
这是我想要实现它的方式:
create table words_table (
id serial primary key,
words varchar(20)[] CHECK (array_length(words, 1) > 0)
);
Run Code Online (Sandbox Code Playgroud)
但看起来它不起作用。o_o
insert into words_table (words) values ('{}');
//INSERT 0 1
Run Code Online (Sandbox Code Playgroud)
如何实现这样的约束?
word_similarity 函数的文档说:
返回一个数字,该数字指示第一个字符串与第二个字符串中最相似的单词的相似程度。该函数在第二个字符串中搜索最相似的词,而不是最相似的子字符串。结果的范围是零(表示两个字符串完全不同)到一(表示第一个字符串与第二个字符串的一个单词相同)。
但是,如果“第一个字符串”包含多个单词,他们对结果应该是什么有点含糊。以下结果的解释是什么:
Run Code Online (Sandbox Code Playgroud)select word_similarity('foo bar','foo bar baz');| word_similarity | | :-------------- | | 1 |
Run Code Online (Sandbox Code Playgroud)select word_similarity('baz bar','foo bar baz');| word_similarity | | :-------------- | | 1 |
Run Code Online (Sandbox Code Playgroud)select word_similarity('baz foo','foo bar baz');| word_similarity | | :-------------- | | 0.8 |
dbfiddle在这里
这是用 PostgreSQL 9.6 测试过的,但这是一个通用的 SQL 问题。
是否可以在递归 CTE (rCTE) 中使用递归表的自联接?
我尝试了以下包含递归项自联接的 rCTE x,
WITH RECURSIVE
x (id) AS (
SELECT 1 id UNION ALL SELECT x1.id+x2.id FROM x x1, x x2
WHERE x1.id < 5 AND x2.id < 5 AND x1.id = x2.id
)
SELECT * FROM x;
Run Code Online (Sandbox Code Playgroud)
,希望应该等同于:
WITH RECURSIVE
x (id) AS (
SELECT 1 id UNION ALL SELECT id+id FROM x WHERE id < 5
)
SELECT * FROM x;
Run Code Online (Sandbox Code Playgroud)
但是之前的 rCTE 会产生一个错误:
ERROR: recursive reference …Run Code Online (Sandbox Code Playgroud) 我有 2 个表:tbl1、tbl2。
CREATE TABLE tbl1(time_1)
AS VALUES
( '2017-09-06 15:26:03'::timestamp ),
( '2017-09-06 15:26:02' ),
( '2017-09-06 15:28:01' ),
( '2017-09-06 15:40:00' );
CREATE TABLE tbl2(time_2)
AS VALUES
( '2017-09-06 15:29:01'::timestamp ),
( '2017-09-06 15:40:00' ),
( '2017-09-06 15:23:59' ),
( '2017-09-06 15:45:58' );
Run Code Online (Sandbox Code Playgroud)
我想加入表格,因此对于 tbl1 中的每一行都匹配 tbl2 中最接近的时间。输出是:
time_1 time_2
--------------------- --------------------
2017-09-06 15:26:03 2017-09-06 15:23:59
2017-09-06 15:26:02 2017-09-06 15:23:59
2017-09-06 15:28:01 2017-09-06 15:29:01
2017-09-06 15:40:00 2017-09-06 15:45:58
Run Code Online (Sandbox Code Playgroud)
我知道如何找到最近时间的单个值:
SELECT * from tbl1 where time_1=INPUT_TIME ORDER BY case …Run Code Online (Sandbox Code Playgroud) 标题说明了一切……如何从 macOS 计算机中完全删除 Postgres 集群?
我用提供的安装应用程序通过EnterpriseDB公司为尊重社会。
此卸载问题已在 Stack Overflow 上被多次询问。但这些问题和答案已经过时多年。
我在 Debian Linux 上使用 Postgres 9.4。我用一个用户创建了一个数据库,cindex可以访问该数据库。然而,当我尝试在命令行登录时,我什至没有提示输入密码:
myuser@myuserserver:~ $ psql -Ucindex cindex
psql: FATAL: Peer authentication failed for user "cindex"
Run Code Online (Sandbox Code Playgroud)
我还需要做什么来启用用户?下面你可以看到我已经设置的权限:
postgres@myuserserver:~$ psql
psql (9.4.13)
Type "help" for help.
postgres=# GRANT SELECT ON ALL TABLES IN SCHEMA cindex TO cindex;
GRANT
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
cindex | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =T/postgres +
| | | | | postgres=CTc/postgres+
| | …Run Code Online (Sandbox Code Playgroud) 我正在将 csv 文件导入 PostgreSQL 数据库。大多数文件导入没有任何问题,直到我收到此消息
ERROR: date/time field value out of range: "1421088300"
HINT: Perhaps you need a different "datestyle" setting.
CONTEXT: COPY accidents, line 6356158, column datetime: "1421088300"
Run Code Online (Sandbox Code Playgroud)
但这似乎是 01/12/2015 @ 6:45pm (UTC) 根据https://www.unixtimestamp.com/index.php的有效时间戳
那么为什么会超出范围呢?
我有一个带有history表的 PostgreSQL 数据库,我在其中存储一个fooID(不是主键序列而是一个文本)、一个属性target和当前时间戳,每当target此fooID更改时:
CREATE TABLE history (
fooId text not null,
target text not null,
updated_at timestamp not null default default now()
);
Run Code Online (Sandbox Code Playgroud)
该表中有几百万个条目,每天有几千次更改。每天我都会扫描表格并保留最后 5 个条目fooId并删除所有旧条目。
该DELETE ... WHERE id in ... rank() over (partiton by nr order by created_at...查询是不我的问题,即工程。只是需要很长时间。
我的问题是:标准 PostgreSQL 表是解决此问题的最佳方法吗?
PostgreSQL 表分区在这里有帮助吗?我知道分区用于轻松丢弃超过 X 天的大块数据,但分区按fooId在我的情况下分区似乎会创建太多分区。
是否有NoSQL数据库会更快,因为它们存储的数据不同?
是否有其他 PostgreSQL 技巧可以帮助我以不同的方式存储数据并针对日常清除的用例进行更优化(SELECT 不是问题,它很少被查询)?
每周 1 小时的独占锁定是可以接受的。大约有 100 万个不同,fooIDs因此每个fooID …
我prices在 PostgreSQL 10 DB 中有一个时间序列表。
这是一个简化的测试用例来说明问题:
CREATE TABLE prices (
currency text NOT NULL,
side boolean NOT NULL,
price numeric NOT NULL,
ts timestamptz NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
我想快速查询每个currency/side双人组的最后一个值,因为这会给我每种货币的当前买入/卖出价格。
我目前的解决方案是:
create index on prices (currency, side, ts desc);
select distinct on (currency, side) *
order by currency, side, ts desc;
Run Code Online (Sandbox Code Playgroud)
但这会让我在这个只有 ~30k 行的表中查询非常慢(~500ms)。
在实际的表都有,我想组四列而不是两个。下面是实际的表和查询的样子:
create table prices (
exchange integer not null,
pair text not null,
side …Run Code Online (Sandbox Code Playgroud) postgresql performance index-tuning greatest-n-per-group postgresql-10 postgresql-performance
postgresql ×10
constraint ×1
datetime ×1
index-tuning ×1
join ×1
mac-os-x ×1
null ×1
password ×1
performance ×1
permissions ×1
pg-hba.conf ×1
psql ×1
recursive ×1
self-join ×1
time ×1
timestamp ×1