小编Erw*_*ter的帖子

如何简洁地检查子查询是否只有一个不同的结果和指定的值?

我发现自己写了以下内容:

select 'yes' 
where exists(select * from foo where val=1)
and not exists(select * from foo where val<>1);
Run Code Online (Sandbox Code Playgroud)

并想知道是否有更简洁的方法而不牺牲太多的可读性。

我找到了一种作为答案发布的方法,但我对此并不完全满意,并且对替代方案非常感兴趣

在这种情况下val是唯一的foo- 没有重复

postgresql duplication

10
推荐指数
2
解决办法
5547
查看次数

缓存表的填充因子是什么?

我已经大量更新/访问了存储序列化 java 对象的表。它们在表中停留 2-3 小时(在此期间也在更新),然后被删除。表的大小约为 300MB。我发现它非常非常频繁地被 VACUUMed 并想知道改变它fillfactor是否会有所帮助?

nosql postgresql configuration vacuum fill-factor

10
推荐指数
1
解决办法
2158
查看次数

带有 IN() 参数的 PostgreSQL PREPARE 查询

我正在尝试从 PHP 准备一个查询,例如:

pg_prepare($con, "prep", "select * from test where tid in ($1)");
Run Code Online (Sandbox Code Playgroud)

然后执行它:

$strpar = "3,4,6,8,10";
pg_execute($con, "prep", array($strpars));
Run Code Online (Sandbox Code Playgroud)

问题是我无法传递一系列构建为 prepare 期望固定数量参数的值。有没有办法使参数动态?

postgresql prepared-statement php array

10
推荐指数
1
解决办法
5424
查看次数

选择两个日期之间的 COUNT 天(周末除外)

我试图获得两个不同日期之间的天数,周末除外。

我不知道如何达到这个结果。

postgresql datetime

10
推荐指数
2
解决办法
4万
查看次数

明确授予更新序列列序列的必要权限?

最近我确实以超级用户的身份创建了一个表,其中包括一个序列号列,例如,

create table my_table
(
    id serial primary key,
    data integer
);
Run Code Online (Sandbox Code Playgroud)

因为我希望我的非超级用户用户拥有对该表的写访问权限,所以我授予了它权限:

grant select, update, insert, delete on table my_table to writer;
Run Code Online (Sandbox Code Playgroud)

在这样做之后的随机时间点,该用户所做的插入开始失败,因为该用户没有修改my_table_id_seq与串行列关联的序列的权限。不幸的是,我无法在我当前的数据库上重现它。

我通过授予用户所需的权限来解决这个问题,如下所示:

grant all on table my_table_id_seq to writer;
Run Code Online (Sandbox Code Playgroud)

有人可以帮我理解

  • 为什么,在某些时候,以前足够的权限可能会开始失败?
  • 为具有串行列的表授予写权限的正确方法是什么?

postgresql permissions sequence

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

调用函数,其中参数是(子)选择语句

我的函数接受一个int4作为参数并返回一个表:

SELECT * FROM test_function(545421); -- works fine

SELECT * FROM test_function(SELECT customerid
                            FROM tableX where id = 1); -- syntax error
Run Code Online (Sandbox Code Playgroud)

我怎样才能使这项工作?
服务器 PostgreSQL 9.3.1

postgresql subquery set-returning-functions

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

PostgreSQL GRANT ALL 在多个表上使用 LIKE

我有 100 多个名为 public.test_* 的表

我怎样才能轻松地一次GRANT ALL访问所有这些表的用户测试?

我试过:

GRANT ALL ON TABLE public.test_* TO test;
Run Code Online (Sandbox Code Playgroud)

但它不起作用......

postgresql permissions dynamic-sql ddl postgresql-9.3

10
推荐指数
2
解决办法
6578
查看次数

索引是否通过 pg_restore 传输

我看到其他问题的答案是否定的(即索引不会随标准转移pg_restore)。但是,它看起来像在我最近的转储/恢复中一样,我不确定它是否真的转移了。

我将我的数据库从 PostgreSQL 9.3 迁移到 9.4.5 并使用转储/恢复来这样做。

下面是使用的命令:

sudo -u postgres pg_dump -h localhost -p 5432 -d nominatim -F d -f dump/postgres/backup -j 20
sudo -u postgres pg_restore --create --dbname=nominatim --exit-on-error -h localhost -p 5432 -F d -j 7 dump/postgres/backup
Run Code Online (Sandbox Code Playgroud)

转储和恢复成功(没有错误)。

我在进行恢复时将 autovacuum 设置为关闭,因此我analyze从 psql 中运行(无参数)并连接到已恢复的数据库。

我注意到的第一件事是,在 9.3 下,数据目录占用了大约 860GB,而在 9.4 下,它占用了大约 680GB。9.4 是不是更高效?

我注意到的第二件事是我看到了数据库中的索引:

nominatim=# \d country_name
                   Table "public.country_name"
            Column             |         Type         | Modifiers 
-------------------------------+---------------------------------    
country_code                  | character varying(2) |   
name                          | hstore               |   
country_default_language_code …
Run Code Online (Sandbox Code Playgroud)

postgresql index restore postgresql-9.4

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

行可见性究竟是如何确定的?

在最简单的情况下,当我们向表中插入新行(并且事务提交)时,它将对所有后续事务可见。请参阅xmax此示例中的 0:

CREATE TABLE vis (
  id serial,
  is_active boolean
);

INSERT INTO vis (is_active) VALUES (FALSE);

SELECT ctid, xmin, xmax, * FROM vis;

  ctid ?xmin ? xmax ? id ? is_active 
?????????????????????????????????????
 (0,1) ?2699 ?    0 ?  1 ? f
Run Code Online (Sandbox Code Playgroud)

当我们更新它时(因为该标志是FALSE偶然设置的),它会发生一些变化:

UPDATE vis SET is_active = TRUE;

SELECT ctid, xmin, xmax, * FROM vis;

 ctid ? xmin ? xmax ? id ? is_active 
?????????????????????????????????????
(0,2) ? 2700 ?    0 ?  1 ? t
Run Code Online (Sandbox Code Playgroud)

根据PostgreSQL 使用的 …

postgresql transaction-log mvcc

10
推荐指数
2
解决办法
899
查看次数

尽管存在冲突,但仍会出现多行插入的死锁 DO NOTHING

设置

我有一个如下所示的批量插入功能set_interactions(arg_rows text)

with inserts as (
    insert into interaction (
        thing_id,
        associate_id, created_time)
    select t->>'thing_id', t->>'associate_id', now() from
    json_array_elements(arg_rows::json)  t
    ON CONFLICT (thing_id, associate_id) DO NOTHING
    RETURNING thing_id, associate_id
) select into insert_count count(*) from inserts;

-- Followed by an insert in an unrelated table that has two triggers, neither of which touch any of the tables here (also not by any of their triggers, etc.)
Run Code Online (Sandbox Code Playgroud)

(我这样包装它是因为我需要计算实际插入的数量,而没有“假行更新”技巧。)

该表interaction有:

  1. 只有一个约束:多列主键 (thing_id, associate_id)
  2. 没有索引
  3. 只有一个触发器:插入后,对于每一行。

触发器执行以下操作:

DECLARE …
Run Code Online (Sandbox Code Playgroud)

postgresql deadlock plpgsql upsert postgresql-9.6

10
推荐指数
1
解决办法
9338
查看次数