标签: postgresql

从 information_schema.schemata 返回行需要什么权限?

在 postgres 中(我在 9.3.4)返回行需要什么权限

select * from information_schema.schemata
Run Code Online (Sandbox Code Playgroud)

9.3文档说,“是由当前启用的角色拥有”。这是否意味着调用者schemata必须拥有(或属于拥有)各种模式的角色?

后来在9.5 文档中,它说模式“当前用户可以访问(通过成为所有者或拥有某些特权)”。

当前尝试运行查询并返回 0 行的帐户已应用于该帐户grant usage on schema

允许数据库帐户在 information_schema.schemata` 视图中查看架构的权限是什么?

postgresql permissions information-schema postgresql-9.3

7
推荐指数
1
解决办法
1657
查看次数

带有 btree 索引的 jsonb 列的统计数据不一致

我注意到涉及 jsonb 列的查询的性能在测试时在 VACUUM ANALYZE 运行之间存在显着差异。分析表格后,我似乎随机得到了完全不同的执行计划。

我在这里使用 Postgres 9.6。我的测试设置如下,我将一个键“x”插入到 jsonb 列“params”中,值在 1 到 6 之间,1 是最稀有的值,6 是最常见的值。我还有一个常规的 int 列“single_param”,其中包含用于比较的相同值分布。:

CREATE TABLE test_data (
    id      serial,
    single_param    int,
    params      jsonb
);

INSERT INTO test_data
SELECT 
    generate_series(1, 1000000) AS id, 
    floor(log(random() * 9999999 + 1)) AS single_param,
    json_build_object(
        'x', floor(log(random() * 9999999 + 1))
    ) AS params;

CREATE INDEX idx_test_btree ON test_data (cast(test_data.params->>'x' AS int));
CREATE INDEX idx_test_gin ON test_data USING GIN (params);
CREATE INDEX ON test_data(id)
CREATE INDEX ON test_data(single_param)
Run Code Online (Sandbox Code Playgroud)

我正在测试的查询是对结果进行分页的典型查询,我按 …

postgresql statistics execution-plan index-tuning json

7
推荐指数
1
解决办法
1739
查看次数

查找列不包含“空格”的行

我正在使用 Postgres 9.5。我想搜索我的名称列不包含空格的行。不过,我对如何为您定义空间有点模糊。我以为它只是我键盘上的空格键,所以我跑了:

.... where name not like '% %';
Run Code Online (Sandbox Code Playgroud)

但后来我得到了一些这样的结果:

 | JASON FALKNER
Run Code Online (Sandbox Code Playgroud)

这对我来说确实是一个空间,但可能还有其他一些事情正在发生。有没有更好的方法可以扫描我的名称列不包含空格的行?

使用正则表达式,not (name ~ '\s')仍然返回看起来有空格的列。

使用:

select cast(name as bytea) ... where name not like like '% %';
Run Code Online (Sandbox Code Playgroud)

回来:

\x4a41534f4ec2a0424c414b45
Run Code Online (Sandbox Code Playgroud)

但是,我仍然有点不清楚我如何使用这些数据来确定如何从我的结果中筛选空间。

我试过了where not (name ~ '[[:space:]]')',它返回“JASON BLAKE”与上面相同的字节序列,\x4a41534f4ec2a0424c414b45

postgresql pattern-matching postgresql-9.5

7
推荐指数
2
解决办法
1万
查看次数

PostgreSQL,我的默认数据库的主机名地址是什么?

我刚刚在我的笔记本电脑上创建了一个 PostgreSQL 数据库(按照这些说明),它运行的是 Linux CentOS 7。

现在我想了解我的默认数据库的主机名地址是什么。我以为是,localhost但不是。

我想知道要与psql -h命令一起使用的名称或 IP 地址。基本上,运行此psql -h HOSTNAME命令应该让我psql单独获得与该命令相同的结果,即让我连接到我的数据库。

我的默认数据库的主机名地址是什么?

> service postgresql status 
Run Code Online (Sandbox Code Playgroud)

状态:

? postgresql.service - PostgreSQL 数据库服务器已加载:已加载(/usr/lib/systemd/system/postgresql.service;已启用;供应商预设:已禁用)活动:自 2017 年 3 月 30 日星期四 14:40:30 EDT 起处于活动状态(正在运行) ; 1 天 1 小时前主 PID: 2256 (postgres

而两者psql -h 127.0.0.1psql -h 127.0.0.1 -U davide产生此消息:

psql:致命:用户“david”的身份验证失败`

postgresql default-value address

7
推荐指数
1
解决办法
5万
查看次数

在 Postgres 表和列上跟踪我自己的自定义元数据

有什么方法可以将我自己的元数据添加到Postgres 中的表和列中,例如短文本的键值对?

我正在从另一个数据库系统迁移表、列和数据。另一个系统具有在 Postgres 不匹配的表和列上定义的属性。例如,在其他数据库系统中,可以为每个表分配一种颜色,用于数据库结构的图形显示,例如CUSTOMER_表为绿色,INVOICE_表为红色。

所以我想要一种方法来跟踪我的 Postgres 数据库中的这些属性设置,这样我的 Java 应用程序就可以通过 JDBC 查询自定义元数据。

SET attribute ?

我注意到:

ALTER TABLE … SET ( attribute_option = value [, ... ] )
Run Code Online (Sandbox Code Playgroud)

…和…

ALTER TABLE … ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )
Run Code Online (Sandbox Code Playgroud)

的文档ALTER TABLE并没有真正解释这些属性的作用。我可以创建自己的属性名称以用作映射到我自己的元数据值的键吗?

或者是否有其他方法可以让 Postgres 记住我自己的自定义元数据?

Comment

我想一种解决方法可能是将键值对作为文本嵌入每个表和列上的COMMENT属性。看起来有点像黑客,有点乱,因为我经常使用 COMMENT 将业务规则记录为散文。但我想它会起作用。有没有更好的方法来记住每表和每列的属性?

Extended Properties Microsoft SQL Server 的

显然,MS SQL Server 为此类元数据提供了“扩展属性”功能。说明这里对堆栈溢出,并在这里对微软文档 …

postgresql metadata postgresql-9.6

7
推荐指数
1
解决办法
2366
查看次数

PostgreSQL 在没有 jsonb_set 的情况下更新 JSONB

使用普通的更新语句来更新 json(b) 列是否有任何缺点,如下所示:

update "events" set "properties" = '{"type":"graph"}'
Run Code Online (Sandbox Code Playgroud)

而不是使用 PostgreSQL 提供的 jsonb_set 函数,它会变成这样的语句:

update "events" set jsonb_set("properties", {'type'}, 'graph')
Run Code Online (Sandbox Code Playgroud)

当使用例如。一个 ORM,并在刚刚更新了 JSON 字段的模型上调用 .save(),第一个方法将被调用,但是由于 PostgreSQL 文档中没有提到这种做事方式,我担心这个可能有一些缺点。

鉴于我对数据库性能方面的任何事情都不太熟悉,我想我会来这里问一个问题。

提前致谢!

postgresql performance json postgresql-performance

7
推荐指数
1
解决办法
2万
查看次数

使用 WITH 构造的奇怪优化效果

我有 4 个表,让我们将它们命名为:

  1. 表 A,15M 行
  2. 表 B,40K 行,
  3. 表 C,30K 行,
  4. 表 D,25M 行

(kk - 表示数百万)

我有一个遗留查询,它是这样构造的:

select C.<some_fields>,B.<some_fields>,D.<some_fields> from C
inner join A on C.x = A.x
inner join D on D.z = 123 and D.a_id = A.a_id
inner join B on C.x = B.x and B.z = 123
where A.type = 'Xxx'
Run Code Online (Sandbox Code Playgroud)

此查询非常慢,执行结果最多需要 3 分钟(对于特定情况,它返回 35k 行)。

但是当我将其更改为以下结构时:

with t as (
   select C.<some_fields>,D.<some_fields> from C
   inner join A on C.x = A.x
   inner join D …
Run Code Online (Sandbox Code Playgroud)

postgresql performance optimization execution-plan amazon-rds query-performance

7
推荐指数
1
解决办法
166
查看次数

PostgreSQL 随机组合与 LATERAL

在下面的示例中,我有一个表foo,我想从中随机选择每组的一行。

CREATE TABLE foo (
  line INT
);
INSERT INTO foo (line)
SELECT generate_series(0, 999, 1);
Run Code Online (Sandbox Code Playgroud)

假设我想按 分组line % 10。我可以这样做:

SELECT DISTINCT ON (bin) bin, line
FROM (
    SELECT line, line % 10 AS bin, random() x
    FROM foo
    ORDER BY x
) X
Run Code Online (Sandbox Code Playgroud)

我想做的是多次从每个垃圾箱中随机选择。我原以为我能和做到这一点generate_series(),并LATERAL

SELECT i, line, bin
FROM
(
 SELECT generate_series(1,3) i
) m,
LATERAL
(SELECT DISTINCT ON (bin) bin, line
FROM (
    SELECT line, line % 10 bin, random() x …
Run Code Online (Sandbox Code Playgroud)

postgresql random subquery

7
推荐指数
1
解决办法
492
查看次数

Postgres 无法创建表空间

我刚刚安装了一个外部 HD 来存储我的新表空间。

postgres@vostro ~ $ df -k
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sdb1      480589520    71580 456082280   1% /mnt/DB

postgres@vostro ~ $ ls -l /mnt/DB/
total 4
drwxrwxrwx 2 postgres postgres 4096 May 25 17:02 postgres

demisc@vostro /mnt $ mount | grep -i sdb1
/dev/sdb1 on /mnt/DB type ext4 (rw,nosuid,nodev,relatime,data=ordered)
Run Code Online (Sandbox Code Playgroud)

我授予了所有必要的权限,以便 posgres 可以在/mnt/DB/postgres. 以下是用户组:

postgres@vostro ~ $ id
uid=123(postgres) gid=131(postgres) groups=131(postgres),112(ssl-cert),1000(demisc)
Run Code Online (Sandbox Code Playgroud)

发生在运行 create tablespace 命令时,我得到以下信息:

postgres=# create tablespace ts_project location '/mnt/DB/postgres';
ERROR:  could not set permissions …
Run Code Online (Sandbox Code Playgroud)

postgresql

7
推荐指数
1
解决办法
4544
查看次数

在 Postgres 9.6.2 数据库上执行 pg_restore 时,此错误意味着什么?

所以我有一个在 Heroku 上运行的 Rails 5.0.x 应用程序和 Postgres 9.6.1。我拉下来做一个pg_restore --exit-on-error --verbose --clean --no-acl --no-owner -h localhost -d database_development ~/Documents/Backups/myproduction.dump

我收到以下错误:

pg_restore: connecting to database for restore
pg_restore: dropping FK CONSTRAINT trial_versions fk_rails_f888baa05c
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 3091; 2606 16768 FK CONSTRAINT trial_versions fk_rails_f888baa05c u7dok58iar41mh
pg_restore: [archiver (db)] could not execute query: ERROR:  constraint "fk_rails_f888baa05c" of relation "trial_versions" does not exist
    Command was: ALTER TABLE ONLY "public"."trial_versions" DROP CONSTRAINT "fk_rails_f888baa05c";
Run Code Online (Sandbox Code Playgroud)

这个错误告诉我什么?我以前从未遇到过这个问题。这最近才开始发生,我需要先了解错误,然后才能开始对其进行故障排除。

postgresql pg-restore postgresql-9.6

7
推荐指数
1
解决办法
1万
查看次数