我发现自己写了以下内容:
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- 没有重复
我已经大量更新/访问了存储序列化 java 对象的表。它们在表中停留 2-3 小时(在此期间也在更新),然后被删除。表的大小约为 300MB。我发现它非常非常频繁地被 VACUUMed 并想知道改变它fillfactor是否会有所帮助?
我正在尝试从 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 期望固定数量参数的值。有没有办法使参数动态?
我试图获得两个不同日期之间的天数,周末除外。
我不知道如何达到这个结果。
最近我确实以超级用户的身份创建了一个表,其中包括一个序列号列,例如,
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)
有人可以帮我理解
我的函数接受一个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
我有 100 多个名为 public.test_* 的表
我怎样才能轻松地一次GRANT ALL访问所有这些表的用户测试?
我试过:
GRANT ALL ON TABLE public.test_* TO test;
Run Code Online (Sandbox Code Playgroud)
但它不起作用......
我看到其他问题的答案是否定的(即索引不会随标准转移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) 在最简单的情况下,当我们向表中插入新行(并且事务提交)时,它将对所有后续事务可见。请参阅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 使用的 …
我有一个如下所示的批量插入功能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有:
触发器执行以下操作:
DECLARE …Run Code Online (Sandbox Code Playgroud) postgresql ×10
permissions ×2
array ×1
datetime ×1
ddl ×1
deadlock ×1
duplication ×1
dynamic-sql ×1
fill-factor ×1
index ×1
mvcc ×1
nosql ×1
php ×1
plpgsql ×1
restore ×1
sequence ×1
subquery ×1
upsert ×1
vacuum ×1