我有一个问题正在尝试解决,我需要使用前 7 列作为基础来消除表中重复的连续行,以确定它是否重复。数据如下:
textColA|textColB|textColC|textColD|textColE|numCol1|numCol2|eventNum
--------+--------+--------+--------+--------+-------+-------+--------
x | y | z | foo | bar | 1 | 2 | 1
x | y | z | foo | bar | 1 | 2 | 2
x | y | z | foo | bar | 1.1 | 2 | 3
x | y | z | foo | bar | 1.1 | 2 | 4
x | y | z | foo | bar | 1 | 2 | 5 …Run Code Online (Sandbox Code Playgroud) 我意识到类似的问题已经被问过多次,但我能找到的所有问题都是 2014/2015 和 postgres 之前的许多版本。
我使用的是 postgres 11。我正在为 类型的表引入一个新的主键uuid。我发现自 postgres 10 以来,hash索引类型的状态非常好(例如http://rhaas.blogspot.com/2017/09/postgresqls-hash-indexes-are-now-cool.html),我想知道uuid现在 postgres 中的 key是否是更好的选择?
更新我意识到我遗漏了影响这个问题的部分用例。
我的主要用例是,我以前有一个复合主键,并将其替换为 UUID,以便于连接,并且不必将主键的数据传播到其他表。
从这个意义上说,我可能希望使用INCLUDE (composite, fields, here)仅支持btree索引类型的主键来创建主键。所以从这个意义上说,我有我的答案。
然而,我仍然对“通用”用例感到好奇,即只想快速访问通过主键查找值。
在尝试回答从现有字符串列创建整数 id 列(整数编码?)时,结果发现我需要我自己的 postgres 版本来使用。我已经安装了一个不错的 postgres 版本:
stack=# select version();
version
--------------------------------------------------------------------------------
------------------------
PostgreSQL 11.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 9.1.1 20190503 (R
ed Hat 9.1.1-1), 64-bit
Run Code Online (Sandbox Code Playgroud)
但它没有 pgcrypto 模块:
stack=# CREATE EXTENSION pgcrypto;
ERROR: could not open extension control file "/usr/pgsql-11/share/extension/pgcrypto.control": No such file or directory
Run Code Online (Sandbox Code Playgroud)
所以我安装了:
sudo dnf install postgresql-contrib
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到同样的错误。如果我查看:
]$ ls /usr/pgsql-11/share/extension/
plpgsql--1.0.sql plpgsql.control plpgsql--unpackaged--1.0.sql
Run Code Online (Sandbox Code Playgroud)
我还需要安装其他软件包吗,或者我还缺少其他东西吗?
更新:
rpm -qa postgresql\*
postgresql-contrib-11.3-1.fc30.x86_64
postgresql11-11.4-1PGDG.f30.x86_64
postgresql-11.3-1.fc30.x86_64
postgresql11-server-11.4-1PGDG.f30.x86_64
postgresql11-libs-11.4-1PGDG.f30.x86_64
Run Code Online (Sandbox Code Playgroud)
我收到 dnf install postgresql11-contrib 错误:
[...] 无法同步存储库“fedora-modular”的缓存错误:无法同步存储库“fedora-modular”的缓存
pdgd …
我的 postgres 数据库有一个名为“id”的列,其范围从 40,000,000 到大约 50,000,000。“id”列是主键。我需要更改“id”列值,使它们跨越不同的数字,以便将此数据库与另一个数据库合并。
我如何生成代码以将值从 40,000,000 - 50,000,000 更改为 0 - 10,000,000?
表的定义是
CREATE TABLE public.keyvaluehistory (
id bigint NOT NULL
DEFAULT nextval('keyvaluehistory_id_seq'::regclass),
segkey text NOT NULL,
dvalue double precision,
bvalue bytea,
tstamp timestamp with time zone,
CONSTRAINT keyvaluehistory_pkey PRIMARY KEY (id)
);
Run Code Online (Sandbox Code Playgroud)
表上没有外键。
我可以承受几分钟/几小时的停机时间。
我想使用 Postgres(CloudSQL 中的 11)作为高效的键值存储。我有大约 200GB 的字典(平均大小为 10kB,结构可以不同并且可以嵌套)。我正在考虑利用改进的哈希索引。这是架构:
\n\n CREATE EXTENSION IF NOT EXISTS "uuid-ossp";\n\n CREATE TABLE IF NOT EXISTS key_val (\n id uuid DEFAULT uuid_generate_v4(),\n value jsonb,\n EXCLUDE using hash (id with =)\n );\n\n CREATE INDEX IF NOT EXISTS idx_key_val ON key_val USING hash (id);\nRun Code Online (Sandbox Code Playgroud)\n\n获取、更新和插入非常简单,但我不知道如何实现高效的更新插入。
\n\nINSERT INTO key_val VALUES ($1, $2) ON CONFLICT ON CONSTRAINT key_val_id_excl DO UPDATE SET value = ($2)\nRun Code Online (Sandbox Code Playgroud)\n\n结果是WrongObjectTypeError ON CONFLICT DO UPDATE not supported with exclusion constraints
可能的解决方案:
\n\n …我在 PostgreSQL 中创建了一个小型数据库来管理我的论文数据。我刚刚开始使用 Postgres,所以这可能是一个新手问题。我在几个表中添加了一列,该列应该保存某个字段的最后更新。其中一个表如下所示(此处相关的列是lastupdate:
-- Drop table
-- DROP TABLE public.finds;
CREATE TABLE public.finds (
id bigserial NOT NULL,
id_investigations int8 NOT NULL,
quote_short varchar NULL,
"group" varchar NULL,
"type" varchar NULL,
subtype varchar NULL,
description varchar NULL,
"number" int4 NULL,
certainty bool NULL,
note varchar NULL,
checked bool NULL,
lastupdate timestamptz NULL,
CONSTRAINT finds_check_group CHECK ((("group")::text = ANY ((ARRAY['Keramik'::character varying, 'Waffe'::character varying, 'Werkzeug'::character varying, 'Schmuck'::character varying])::text[]))),
CONSTRAINT finds_pkey PRIMARY KEY (id),
CONSTRAINT id_investigations FOREIGN KEY (id_investigations) REFERENCES …Run Code Online (Sandbox Code Playgroud) 在 aws RDS 上的 postgresql 主从复制方案上的复制服务器中,我收到以下错误:
SQLSTATE[40001]: Serialization failure: 7 ERROR: canceling statement due to conflict with recovery
Run Code Online (Sandbox Code Playgroud)
据我了解,原因是复制的发生类似于数据库迁移。查询序列被写入称为 WAL 的内容,然后以 FIFO 序列执行。
另外,我了解到,一旦在执行 wal 时执行查询,可能会导致冲突,因为有时当前正在执行的查询可能会导致获取陈旧数据。
因此,根据文档,存在延迟,允许首先执行当前查询,然后应用 wal 更改。这些是:
max_standby_archive_delaymax_standby_streaming_delay但是在繁重的查询(查询执行时间> 30秒)上将这些值设置为-1会导致副本在较长时间内拥有过时的数据吗?
在 PostgreSQL 中选择随机行的一种可能方法是:
\n\nselect * from table order by random() limit 1000;
(另请参阅此处。)
\n\n我的问题是,order by random()到底是什么意思?是否以某种方式生成了一个随机数并将其视为某种“种子”?或者这是特殊的内置语法,在这个地方random()具有与其他上下文不同的含义?
从一些实验来看,最后一种解释似乎更合理。考虑以下:
\n\n# select random();\n random \n\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\n 0.336829286068678\n(1 row)\nRun Code Online (Sandbox Code Playgroud)\n\n# select * from article order by 0.336829286068678 limit 5;\nERROR: non-integer constant in ORDER BY\nLINE 1: select * from article order by 0.336829286068678 limit 5;\nRun Code Online (Sandbox Code Playgroud)\n 在 PostgreSQL 中,当我在另一个数据库中时,如何才能GRANT USAGE在某个数据库中为某个用户提供某个模式?
first我以具有特权的用户身份连接到数据库(假设是数据库) CREATEDB。我想second为用户创建另一个数据库normal_user,并授予该用户公共模式的使用权。我可以CREATE DATABASE second;从当前连接的数据库执行此操作,但据我所知我不能执行此操作GRANT USAGE ON SCHEMA public TO normal_user;,因为这只会授予对public当前连接数据库中的架构的使用权限。
我想在单个 SQL 脚本中自动执行数据库创建、用户创建和权限授予。
我希望有一种巧妙的方法来做到这一点。我想这样做是为了自动化数据库和用户创建以及权限授予。我知道一些工具 Lime Ansible 或 Terraform可能有助于实现自动化。但我希望我们能够仅通过 Postgres 脚本来做到这一点。
我使用 AWS RDS 作为我的 Postgres,但我认为我没有办法通过 ssh 来从 cmd 行运行 Postgres 命令。我正在尝试从 dbeaver 脚本页面执行此操作。所以我认为 SQL 就是我要做这件事的全部。如果我可以使用 SQL 轻松切换用户或切换数据库,那就很有帮助。
经过思考,我认为使用psql -u user -p -h hostname我也许能够连接到实例并在 shell 中切换数据库。但如果我弄错了,请告诉我。
我有下表:
CREATE TABLE orders
(
info_date date,
country_code VARCHAR,
order_total int,
CONSTRAINT orders_pk PRIMARY KEY (info_date, country_code)
) PARTITION BY LIST (country_code);
CREATE TABLE orders_def PARTITION OF orders DEFAULT;
Run Code Online (Sandbox Code Playgroud)
我插入一些行country_code 'foo',它们最终位于默认分区中。
一段时间后,我决定应该'foo'有自己的分区,我该怎么做?
根据文档:
如果存在 DEFAULT 分区,并且 DEFAULT 分区中存在任何行,否则它们将适合要添加的新分区,则无法添加新分区。
这是我的尝试:
begin;
CREATE TEMP TABLE temp_table ON COMMIT DROP AS
SELECT * FROM orders where country_code = 'foo';
DELETE FROM orders where country_code = 'foo';
CREATE TABLE orders_foo PARTITION OF orders FOR VALUES IN ('foo');
INSERT INTO …Run Code Online (Sandbox Code Playgroud) postgresql migration partitioning default-value postgresql-11
postgresql ×10
amazon-rds ×1
hashing ×1
index ×1
json ×1
linux ×1
migration ×1
partitioning ×1
permissions ×1
primary-key ×1
query ×1
random ×1
replication ×1
select ×1
timestamp ×1
upsert ×1
uuid ×1