Questions tagged «postgresql-9.4»

PostgreSQL 9.4版

2
在PostgreSQL中增量刷新子视图
是否可以在PostgreSQL中增量刷新实例化视图,即仅刷新新数据或已更改的数据? 考虑此表和实例化视图: CREATE TABLE graph ( xaxis integer NOT NULL, value integer NOT NULL, ); CREATE MATERIALIZED VIEW graph_avg AS SELECT xaxis, AVG(value) FROM graph GROUP BY xaxis 定期将新值添加到graph或更新现有值。我只想graph_avg每两个小时刷新一次已更新值的视图。但是在PostgreSQL 9.3中,整个表都被刷新了。这非常耗时。下一版本9.4允许CONCURRENT更新,但仍刷新整个视图。对于亿万行,这需要几分钟。 跟踪更新和新值并仅部分刷新视图的好方法是什么?


3
在Postgres中查询实例化视图的定义
我想知道如何在Postgres中查询实例化视图的定义。作为参考,我希望执行的操作与常规视图的操作非常相似: SELECT * FROM information_schema.views WHERE table_name = 'some_view'; 它为您提供以下列: table_catalog table_schema table_name view_definition check_option is_updatable is_insertable_into is_trigger_updatable is_trigger_deletable is_trigger_insertable_into 物化视图可能吗? 到目前为止,从我的研究看来,物化视图似乎是故意从information_schema中排除的,因为 information_schema仅可以显示SQL标准中存在的对象。 (http://www.postgresql.org/message-id/3794.1412980686@sss.pgh.pa.us) 由于它们似乎被完全排除在information_schema之外,因此我不确定该怎么做,但是我想做的事情有两个: 查询是否存在特定的实例化视图。(到目前为止,我发现这样做的唯一方法是尝试创建一个具有相同名称的垫视图,看看它是否爆炸了。) 然后查询实例化视图的定义(类似于上的view_definition列information_schema.views)。

2
使用位图索引扫描的查询计划中的“重新检查条件:”行
这是从注释到上一个问题的副产品: Postgres 9.4.4查询需要永远 使用PostgreSQL 9.4,Recheck Cond:在所输出的查询计划中的位图索引扫描之后,似乎总是出现一行EXPLAIN。 就像EXPLAIN所引用问题的输出中一样: -> Bitmap Heap Scan on table_three (cost=2446.92..19686.74 rows=8159 width=7) Recheck Cond: (("timestamp" > (now() - '30 days'::interval)) AND (client_id > 0)) -> BitmapAnd (cost=2446.92..2446.92 rows=8159 width=0) -> Bitmap Index Scan on table_one_timestamp_idx (cost=0.00..1040.00 rows=79941 width=0) Index Cond: ("timestamp" > (now() - '30 days'::interval)) …



2
如何在Postgres中每小时进行增量备份?
尝试每小时对单个Postgres服务器(Win7 64)进行增量备份。 我在中进行以下设置postgresql.conf: max_wal_senders = 2 wal_level = archive archive_mode = on archive_command = 'copy "%p" "c:\\postgres\\foo\\%f"' (重新开始) 我做了一个基本的备份 pg_basebackup -U postgres -D ..\foo -F t -x 它在base.tar文件foo夹中创建了一个大文件,并添加了一些16,384 KB的文件,我认为这些文件是WAL。 我不明白的是为什么WAL foo不变。data/pg_xlog变化中的WAL 。pg不应该复制它们吗?它如何决定这样做? 也许我需要设置archive_timeout=3600? 我已经看到几个站点(pg的邮件列表,bacula的postgres页面)说需要调用pg_start_backup()和pg_stop_backup(),但我相信这些不是必需的。真的吗? 次要问题: WAL多久data/pg_xlog写入一次?是什么触发写操作? 如果我\q在psql中执行一些DML,似乎更新了WAL 。或在pgAdmin中编辑表,然后关闭窗口。我认为它将在提交时编写。 最佳做法?pg_basebackup每周一次?将WAL归档到与PG相同的计算机或远程计算机上吗?

1
PostgreSQL JSON查询数组针对多个值
我想针对jsonbPostgres 中的类型编写查询,因为给定的客户ID数组将找到对应的组。 给定此示例表: CREATE TABLE grp(d JSONB NOT NULL); INSERT INTO grp VALUES ('{"name":"First","arr":["foo"], "customers":[{"id":"1", "name":"one"},{"id":"2", "name":"two"}]}') , ('{"name":"Second","arr":["foo","bar"], "customers":[{"id":"3", "name":"three"},{"id":"4", "name":"four"}]}') , ('{"name":"Third","arr":["bar","baz"], "customers":[{"id":"5", "name":"five"},{"id":"6", "name":"seven"}]}'); 我发现了类似的问题(针对多个值的PostgreSql JSONB SELECT),并使用此查询设法在简单数组上实现了我想要的功能: SELECT d FROM grp WHERE d->'arr' ?| ARRAY['foo', 'bar']; 但是,当数组包含JSON 对象时,我无法使其工作: SELECT d FROM grp WHERE d->'customers' ?| ARRAY['{"id":"1"}', '{"id":"5"}']; …

2
Trigram搜索随着搜索字符串变长而变慢
在Postgres 9.1数据库中,我有一个table1约150万行和一列的表label(为方便起见,使用简化名称)。 上有一个功能性trigram-index lower(unaccent(label))(unaccent()已使其不可变,以允许在索引中使用)。 以下查询非常快: SELECT count(*) FROM table1 WHERE (lower(unaccent(label)) like lower(unaccent('%someword%'))); count ------- 1 (1 row) Time: 394,295 ms 但是以下查询速度较慢: SELECT count(*) FROM table1 WHERE (lower(unaccent(label)) like lower(unaccent('%someword and some more%'))); count ------- 1 (1 row) Time: 1405,749 ms 即使搜索更加严格,添加更多单词的速度甚至会更慢。 我尝试了一个简单的技巧,即先对第一个单词运行子查询,然后对完整的搜索字符串进行查询,但是(不幸的是)查询计划者看到了我的想法: EXPLAIN ANALYZE SELECT * FROM ( SELECT …

1
如何将ctid分解为页码和行号?
表格中的每一行都有一个系统列 ctid,其类型tid表示该行的物理位置: create table t(id serial); insert into t default values; insert into t default values; select ctid , id from t; ctid | ID :---- | -: (0,1)| 1个 (0,2)| 2 dbfiddle 在这里 是什么让刚刚页面数从最好的方式ctid在最适合的类型(例如integer,bigint或numeric(1000,0))? 我能想到的唯一方法是非常丑陋。

1
如何将长长的PL / pgSQL代码行分成多行?
有没有办法将多行PL / pgSQL代码分割成多行?我的上下文是一个触发函数,在该函数中,我按照以下记录将插入日志记录到表中: INSERT INTO insert_log (log_time, description) VALUES ( now() , 'A description. Made up of 3 semi long sentences. That I want to split, in the code, not in the log table, over 3 lines for readability.' );

2
PostgreSQL使用JSONB加入
我有这个SQL: CREATE TABLE test(id SERIAL PRIMARY KEY, data JSONB); INSERT INTO test(data) VALUES ('{"parent":null,"children":[2,3]}'), ('{"parent":1, "children":[4,5]}'), ('{"parent":1, "children":[]}'), ('{"parent":2, "children":[]}'), ('{"parent":2, "children":[]}'); 这将给: id | data ----+-------------------------------------- 1 | {"parent": null, "children": [2, 3]} 2 | {"parent": 1, "children": [4, 5]} 3 | {"parent": 1, "children": []} 4 | {"parent": …


2
同时调用同一函数:死锁是如何发生的?
new_customerWeb应用程序每秒调用我的函数几次(但每个会话仅调用一次)。它要做的第一件事就是锁定customer表(执行“如果不存在,请插入”-的简单变体upsert)。 我对文档的理解是,对的其他调用new_customer应该简单地排队,直到所有先前的调用完成为止: LOCK TABLE获取表级别的锁,必要时等待释放任何冲突的锁。 为什么有时会陷入僵局呢? 定义: create function new_customer(secret bytea) returns integer language sql security definer set search_path = postgres,pg_temp as $$ lock customer in exclusive mode; -- with w as ( insert into customer(customer_secret,customer_read_secret) select secret,decode(md5(encode(secret, 'hex')),'hex') where not exists(select * from customer where customer_secret=secret) returning customer_id ) insert …


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.