我在 x86_64-unknown-linux-gnu 上的 PostgreSQL 9.4.3 中的表和触发器,由 gcc (Debian 4.9.2-10) 4.9.2,64 位编译:
CREATE TABLE measurements (
measurement_id SERIAL PRIMARY KEY NOT NULL,
measurement_size_in_bytes INTEGER NOT NULL
);
CREATE TABLE file_headers (
header_id SERIAL PRIMARY KEY NOT NULL,
measurement_id INTEGER NOT NULL,
file_header_index_start INTEGER,
file_header_index_end INTEGER
);
CREATE TRIGGER measurement_ids AFTER INSERT
ON measurements FOR EACH ROW
EXECUTE PROCEDURE ins_function('SELECT measurement_id FROM measurements
ORDER BY measurement_id desc limit 1;', 1, 666 );
Run Code Online (Sandbox Code Playgroud)
在那里我假设 SELECT 的数据类型自 SERIAL 以来是 INTEGER 但它显然是错误的,因为我从这个启动触发器的命令中收到错误消息:
INSERT INTO measurements …Run Code Online (Sandbox Code Playgroud) 我们有一个网络应用程序,可以检测数据库是否不存在并创建它。用户喜欢它,但我的想法是这样的用户拥有太多的权限。
我最好建议更改应用程序的数据库创建屏幕以提供 2 组凭据 - 一组将用于最初创建数据库而不是永久存储(例如“postgres”角色),另一组将被网络使用app 并存储在配置文件中(例如“webapp”)。此外,'webapp' 将需要能够 1) 添加新的和 2) 修改现有的表、过程和函数(Web 应用程序根据需要自动更新架构)
所以我的一些问题是:
这个答案向我提出了如何在这样的函数之间VALUES和SELECT中进行选择的问题。在 x86_64-unknown-linux-gnu 上使用PostgreSQL 9.4.3,由 gcc (Debian 4.9.2-10) 4.9.2, 64-bit 编译:
CREATE OR REPLACE FUNCTION insaft_function()
RETURNS TRIGGER AS
$func$
BEGIN
INSERT INTO file_headers (measurement_id, file_header_index_start
, file_header_index_end)
VALUES (NEW.measurement_id, TG_ARGV[0]::int, TG_ARGV[1]::int);
RETURN NULL; -- result ignored since this is an AFTER trigger
END
$func$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
VALUES处理多行,但SELECT你可以做更多的事情。这里唯一的要求是对INSERT表格执行上述操作。您可以假设INSERT在系统的持续质量保证中每个周期完成100k 次这样的操作。
我注意到这些差异与我的数据不同,这里选择了三个中值:
VALUES
real user sys
-------------------------------
0m0.353s 0m0.256s 0m0.028s
0m0.327s 0m0.252s 0m0.036s
0m0.358s …Run Code Online (Sandbox Code Playgroud) postgresql trigger performance select postgresql-performance
我的数据库中的许多表共享一个公共布局,特别是它们有一个serial以关系名称命名的主键。从 Web 应用程序中更新这些表通常涉及以下形式的查询:
UPDATE table SET attribute = x WHERE table_id = y
Run Code Online (Sandbox Code Playgroud)
这很常见,以至于我有一个执行此任务的存储过程:
CREATE OR REPLACE FUNCTION setvalue(
relname text,
row_id integer,
colname text,
newvalue text)
RETURNS void AS
$BODY$
BEGIN
EXECUTE format('UPDATE %I SET %I = $1 WHERE %I = $2', relname, colname, relname || '_id', colname) USING row_id;
END;
$BODY$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不适用于非文本类型。例如,更新一date列给出ERROR: column ... is of type date but expression is of type text. 假设文本是预期类型的有效文字表示,是否有一种安全的方法让 DBMS 找出正确的类型并做正确的事情?
我正在尝试从 Postgres 9.4 转储模式,在 Cents 6.5 上运行,但出现错误:
pg_dump:找不到匹配的模式
这是我正在运行的命令:
pg_dump -U postgres -n "OLD-TODELETE-bushes" --verbose site > output_nf
Run Code Online (Sandbox Code Playgroud)
我对此有点困惑,因为当我运行时select schema_name from information_schema.schemata,我可以看到架构名称。
如果我为所有模式运行 pg_dump,则按OLD-TODELETE-bushes预期将其转储到文件中。
Postgres 日志中没有错误。
谁有想法?
我目前在 Amazon Web Services 上设置了一个Postgres 9.4.4 RDS实例。在这个实例中,我持有 20 张桌子。此外,我还在我的本地服务器(操作系统:64 位 Linux)上安装了 Postgres 9.4.4,它在一个数据库下保存了大约 30 个表。云实例和我的本地服务器实例都有重叠的表,即云上有一些我复制到本地的表,反之亦然。我不太熟悉 Postgres 复制过程,因为我从来不需要它们,而且目前通过 python 脚本在这两个数据库之间读/写/附加表的传输效率很低。
我将不胜感激来自社区的任何关于最佳程序的指导。理想情况下,我想要一个解决方案,其中云上的某些表可以在本地成为 MASTER 和 SLAVE,并允许其他其他表在本地成为 MASTER,在云中成为 SLAVE。这可能吗?
听说过 Burcardo 或 Slony 可以做到这一点吗?这方面的任何好的文档/站点也会有所帮助。
postgresql replication transactional-replication postgresql-9.4 amazon-rds
使用 PostgreSQL 8.4,我试图使用 order by 和两个表的索引列查询两个包含 100 万条记录的表,并且我正在失去性能(1 列需要 30 毫秒,两列需要 5 分钟)。例如:
select r.code, r.test_code, r.sample_code, s.barcode, s.registry_date
from requests r
inner join samples s on (s.code = r.sample_code)
order by s.barcode asc , r.code asc
limit 21;
Run Code Online (Sandbox Code Playgroud)
表信息:
CREATE TABLE public.samples (
code BIGINT NOT NULL,
barcode VARCHAR(40) NOT NULL,
registry_date TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT samples_pkey PRIMARY KEY(code)
);
CREATE INDEX idx_samp_barcode ON public.samples (barcode);
CREATE INDEX idx_samp_barcode_code ON public.samples (barcode, code);
CREATE INDEX …Run Code Online (Sandbox Code Playgroud) postgresql performance postgresql-8.4 recursive postgresql-performance
我正在尝试加速执行三个ILIKE查询的查询并使用 or 减少这些查询(返回总计数和 10 个条目)
SELECT *, count(*) OVER() as filtered_count FROM "users"
WHERE (
(f_unaccent("users"."first_name") ILIKE f_unaccent('%foo%') OR
f_unaccent("users"."last_name") ILIKE f_unaccent('%foo%')) OR
f_unaccent("users"."club_or_hometown") ILIKE f_unaccent('%foo%')
) LIMIT 10 OFFSET 0
Run Code Online (Sandbox Code Playgroud)
在为所有查询的属性添加 gin-indexes 后,这工作得相当快(这里仅适用于 first_name):
CREATE INDEX users_first_name_gin
ON users
USING gin
(f_unaccent(first_name::text) COLLATE pg_catalog."default" gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
然而,如果我添加一个额外的 order 子句,例如ORDER BY users.first_name ASC,postgresql 不使用 gin 索引,而是使用正常的 b-tree 索引 on first_name,然后过滤结果。这在我的应用程序中需要更长的时间。即使对于有序查询,我如何调整查询/索引以继续使用 gin 索引?
编辑:我使用的是 postgresql 9.4
无序查询的解释:
"Limit (cost=125.98..139.61 rows=10 width=58) (actual time=17.828..17.833 rows=10 loops=1)"
" -> …Run Code Online (Sandbox Code Playgroud) 我有一个大约 310 万行的表,其定义和索引如下:
CREATE TABLE digiroad_liikenne_elementti (
ogc_fid serial NOT NULL,
wkb_geometry geometry(Geometry,4258),
tiee_tila numeric(9,0),
vaylatyypp numeric(9,0),
toiminnall numeric(9,0),
eurooppati character varying(254),
kansalline numeric(9,0),
tyyppi numeric(9,0),
liikennevi numeric(9,0),
ens_talo_o numeric(9,0),
talonumero numeric(9,0),
ens_talo_v numeric(9,0),
oik_puol_t character varying(254),
tieosan_ta numeric(9,0),
viim_talo_ numeric(9,0),
viim_tal_1 numeric(9,0),
vas_puol_t character varying(254),
laut_tyypp numeric(9,0),
lautta_lii numeric(9,0),
inv_paalu_ numeric(19,11),
inv_paal_1 numeric(19,11),
liitalue_o numeric(9,0),
ketju_oid numeric(9,0),
tietojoukk numeric(9,0),
ajoratanum numeric(4,0),
viite_guid character varying(254),
"timestamp" date,
tiee_kunta numeric(9,0),
toissij_ti character varying(254),
viite_oid numeric(9,0),
k_elem_id numeric(9,0),
region character varying(40) DEFAULT …Run Code Online (Sandbox Code Playgroud) postgresql performance insert postgresql-9.3 write-ahead-logging postgresql-performance
我正在尝试创建一个 postgresql 用户,该用户可以选择但不能在数据库上创建或删除、插入等,但我终生无法弄清楚如何阻止用户创建自己的表。
到目前为止我所做的:
create database test;
create role readonly;
alter role readonly with login;
alter role readonly with encrypted password 'test';
revoke all on schema public from public;
revoke all on schema public from readonly;
revoke all on database test from public;
revoke all on database test from readonly;
grant connect on database test to readonly;
Run Code Online (Sandbox Code Playgroud)
然而,当我以只读身份登录到数据库时,我仍然可以不受惩罚地创建表。我错过了什么?
postgresql ×10
performance ×3
plpgsql ×2
trigger ×2
amazon-rds ×1
datatypes ×1
dynamic-sql ×1
index ×1
insert ×1
permissions ×1
pg-dump ×1
recursive ×1
replication ×1
security ×1
select ×1
users ×1