设置如下:1 个小型 Amazon Linux(EBS 支持)EC2 实例和 3 个额外的卷。这既是 Web 服务器又是数据库服务器。一卷用于代码,一卷用于 PostgreSQL (8.4) 数据目录,一卷用于存储来自 PostgreSQL 的 WAL 文件。
(1) 带有 WAL 文件的卷也将有数据目录的基本备份,它在执行 pg_start_backup() 后被复制。然后它将存储来自 PostgreSQL(WAL 文件)的连续存档输出。要对该卷进行快照,发出同步并冻结文件系统是否有任何意义(如果是 XFS,则使用 xfs_freeze,如果是 EXT4,则使用 dmsetup)?或者我可以拍摄实时快照吗?WAL 文件将以每分钟一个的速度发送。是否有可能在复制单个 WAL 文件并导致数据损坏时启动快照?
(2) 包含实时 PostgreSQL 数据目录的卷也将被备份以进行良好的测量(每天)。在做这个卷的快照之前,我发出一个 pg_dump 并将生成的 SQL 文件保存在数据目录中。采取预防措施以确保实际数据库数据的一致性有什么意义吗?假设拍摄实时快照将正确 (a) 备份配置文件(postgresql.conf、pg_hba.conf、pg_ident.conf)和 (b) 备份 SQL 转储文件是否正确。备份这两件事,sql 转储文件和配置文件,将是对该卷进行快照的主要内容。数据库不是很大,所以我不介意数据文件会使这个快照膨胀。在这种情况下,我可以做一个实时快照——对吗?
(2a) 将数据目录保留在根卷上,并有一个备份脚本将 sql 转储文件和配置文件复制到另一个卷上,并在复制完成后对该卷进行快照会更好吗?
(3) 至于上面有代码的卷,还有同步和冻结文件系统的意义吗?或者可以只拍摄实时快照?这些数据应该是相当“静态的”。
(4) 这是一个可靠的备份方案吗?根卷不会定期备份,因为我只会在设置和配置后保留机器映像。
谢谢
这很好地说明了这个问题:
当 b 列是文本类型而不是数组时,以下工作:
select *
from json_to_record('{"a":1,"b":["hello", "There"],"c":"bar"}')
as x(a int, b text, d text);
a | b | d
---+--------------------+---
1 | ["hello", "There"] |
Run Code Online (Sandbox Code Playgroud)
但是,如果我将该b列定义为数组,则会出现此错误:
select *
from json_to_record('{"a":1,"b":["hello", "There"],"c":"bar"}')
as x(a int, b text[], d text)
ERROR: malformed array literal: "["hello", "There"]"
DETAIL: "[" must introduce explicitly-specified array dimensions.
Run Code Online (Sandbox Code Playgroud)
如何说服/强制json_to_record(或json_populate_record)将 JSON 数组转换为目标列类型的 Postgres 数组?
我想从 ARGV[] 转换到 PostgreSQL 中的 int 数组,我在代码中用TODO标记了伪代码。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 events (
event_id SERIAL PRIMARY KEY NOT NULL,
measurement_id INTEGER NOT NULL,
event_index_start INTEGER NOT NULL,
event_index_end INTEGER NOT NULL
);
CREATE OR REPLACE FUNCTION insaft_function()
RETURNS TRIGGER AS
$func$
BEGIN
-- TODO Loop until TG_ARGV[0] empty
INSERT INTO events (measurement_id, event_index_start, event_index_end)
SELECT …Run Code Online (Sandbox Code Playgroud) 我有一张代表电影的表。这些字段是:
id (PK), title, genre, runtime, released_in, tags, origin, downloads。
我的数据库不会被重复的行污染,所以我想强制执行唯一性。问题是不同的电影可能有相同的标题,甚至除了tags和之外的相同字段downloads。如何实现唯一性?
我想到了两种方法:
downloads主键以外的所有字段。downloads因为它是 JSON,所以我一直在外面,它可能会影响性能。id作为主键保留,但为所有其他列添加唯一约束(再次除外downloads)。我读了这个非常相似的问题,但我不太明白我该怎么做。目前该表与任何其他表都没有关系,但将来可能会。
目前我的记录略少于 20,000 条,但我预计这个数字会增长。我不知道这是否与问题有些相关。
编辑:我修改了架构,这里是我将如何创建表:
CREATE TABLE movies (
id serial PRIMARY KEY,
title text NOT NULL,
runtime smallint NOT NULL CHECK (runtime >= 0),
released_in smallint NOT NULL CHECK (released_in > 0),
genres text[] NOT NULL default ARRAY[]::text[],
tags text[] NOT NULL default ARRAY[]::text[],
origin text[] NOT NULL …Run Code Online (Sandbox Code Playgroud) 注意:我指的是数学序列,而不是PostgreSQL的序列机制。
我有一个表示整数序列的表。定义是:
CREATE TABLE sequences
(
id serial NOT NULL,
title character varying(255) NOT NULL,
date date NOT NULL,
sequence integer[] NOT NULL,
CONSTRAINT "PRIM_KEY_SEQUENCES" PRIMARY KEY (id)
);
Run Code Online (Sandbox Code Playgroud)
我的目标是使用给定的子序列查找行。也就是说,sequence字段是包含给定子序列的序列的行(在我的情况下,序列是有序的)。
假设该表包含以下数据:
+----+-------+------------+-------------------------------+
| id | title | date | sequence |
+----+-------+------------+-------------------------------+
| 1 | BG703 | 2004-12-24 | {1,3,17,25,377,424,242,1234} |
| 2 | BG256 | 2005-05-11 | {5,7,12,742,225,547,2142,223} |
| 3 | BD404 | 2004-10-13 | {3,4,12,5698,526} |
| …Run Code Online (Sandbox Code Playgroud) 我有一张订单表
Column | Type | Modifiers
------------+-----------------------------+-----------------------------------------------------
id | integer | not null default nextval('orders_id_seq'::regclass)
client_id | integer | not null
start_date | date | not null
end_date | date |
order_type | character varying | not null
Run Code Online (Sandbox Code Playgroud)
数据具有针对 client_id 的非重叠常规订单,并且偶尔会出现临时订单,当它们具有匹配的 client_id 时,会覆盖其 start_date 的常规订单。存在应用程序级别的限制,以防止相同类型的订单重叠。
id | client_id | start_date | end_date | order_type
----+-----------+------------+------------+------------
17 | 11 | 2014-02-05 | | standing
18 | 15 | 2014-07-16 | 2015-07-19 | standing
19 | 16 | 2015-04-01 | | standing …Run Code Online (Sandbox Code Playgroud) 现在我必须使用查询来获取文本文件中的命令。然后从中删除双引号。最后,在 psql shell 中运行该文件。
如何一步删除 PostgreSQL中的所有函数?
PostgreSQL 9.2
我试图了解Hash Semi Join和 just之间的区别Hash Join。
这里有两个查询:
一世
EXPLAIN ANALYZE SELECT * FROM orders WHERE customerid IN (SELECT
customerid FROM customers WHERE state='MD');
Hash Semi Join (cost=740.34..994.61 rows=249 width=30) (actual time=2.684..4.520 rows=120 loops=1)
Hash Cond: (orders.customerid = customers.customerid)
-> Seq Scan on orders (cost=0.00..220.00 rows=12000 width=30) (actual time=0.004..0.743 rows=12000 loops=1)
-> Hash (cost=738.00..738.00 rows=187 width=4) (actual time=2.664..2.664 rows=187 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 7kB
-> Seq Scan on customers (cost=0.00..738.00 rows=187 width=4) (actual …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Postgres 9.4 中编写一个触发器函数。像这样的东西(还没有工作):
CREATE FUNCTION set_point_from_coords(source _regclass, target _regclass)
RETURNS trigger AS $func$
BEGIN
NEW.target := ST_SetSRID(ST_Point(NEW.source[1], NEW.source[2]), 4326);
RETURN NEW;
END;
$func$ LANGUAGE plpgsql
Run Code Online (Sandbox Code Playgroud)
在这种情况下,target是一个类型的列geometry并且source是一个小数数组。
当一行插入coords数组时,我想将其转换为point. 如果我只是对列名进行硬编码,则上述方法会起作用,但我想使用相同的函数为不同的表和不同的列对执行此操作。而且我对它INSERT本身没有直接控制权。
这是我的一些实验:http : //sqlfiddle.com/#!15/ddddcd/1
找到了这篇相关的博客文章,我很难解析它。
如果这样更容易编码,我可以在插入/更新之后而不是之前运行它。
我有一个表,需要添加一个没有默认值的新列:
ALTER TABLE integrations.billables
DROP CONSTRAINT IF EXISTS cc_at_least_one_mapping_needed_billables,
ADD CONSTRAINT cc_at_least_one_mapping_needed_billables
CHECK ((("qb_id" IS NOT NULL) :: INTEGER +
("xero_id" IS NOT NULL) :: INTEGER +
("freshbooks_id" IS NOT NULL) :: INTEGER +
("unleashed_id" IS NOT NULL) :: INTEGER +
("csv_data" IS NOT NULL) :: INTEGER +
("myob_id" IS NOT NULL) :: INTEGER) > 0);
Run Code Online (Sandbox Code Playgroud)
ALTER TABLE integrations.billables
DROP COLUMN IF EXISTS myob_id,
ADD COLUMN myob_id varchar(255);
Run Code Online (Sandbox Code Playgroud)
如何为下一个值添加约束,而不是为那些已经存在的值添加约束?(否则我会得到某些行违反了错误检查约束“”)。
这与我之前的问题有关:错误:某些行违反了检查约束
postgresql null database-design postgresql-9.2 check-constraints
postgresql ×10
array ×2
dynamic-sql ×2
join ×2
plpgsql ×2
cast ×1
functions ×1
hashing ×1
insert ×1
json ×1
null ×1
performance ×1
primary-key ×1
schema ×1
snapshot ×1
trigger ×1