我有一个非常大的表 (35GB),它在四个列的组合中是独一无二的。
该表不是很宽,它唯一的四列是较大的列(以字节为单位)。最终结果是保持表唯一的索引是 21GB。这不是索引大小随时间膨胀的结果,而是索引创建后立即的大小。
我根本不需要优化插入速度,因为插入每月只会分批进行一次。一旦插入,任何行都不会进行任何更新。
我正在运行 PostgreSQL 9.5.0。
有没有办法不复制如此大的数据库部分来强制执行唯一约束?可能使用聚集索引之类的东西?
全表说明:
CREATE TABLE medi_cal_base_eligibility (
client_index_number text NOT NULL,
medi_cal_date date NOT NULL,
eligibility_date date NOT NULL,
aidcode text,
responsible_county text,
status text,
cardinal smallint NOT NULL,
id SERIAL PRIMARY KEY
);
Run Code Online (Sandbox Code Playgroud)
索引:
"medi_cal_base_eligibility_pkey" PRIMARY KEY, btree
(id)
"medi_cal_base_eligibility_uq_dates_cin_cardinal" UNIQUE CONSTRAINT, btree
(eligibility_date, client_index_number, medi_cal_date, cardinal)
Run Code Online (Sandbox Code Playgroud) 我在运行 Ubuntu 的 VPS 中运行 Postgres 9.4 服务器。它经常被杀死(一天多次)。
这是来自 dmesg
内存不足:杀死进程 1020 (postgres) 得分 64 或牺牲子进程
杀死进程 1020 (postgres) total-vm:445764kB, anon-rss:140640kB, file-rss:136092kB
如何调试导致此崩溃的原因?是长时间运行的查询或服务器的某些错误配置还是打开了大量空闲连接?
postgresql performance postgresql-9.4 postgresql-performance
我Books在 Postgres 中创建了一个类型,它有 2 个numeric字段和 2 个varchar字段。我想将Books一个函数的数组发送到INSERT匹配表中的那些值。
这是我的类型:
CREATE TYPE Books AS (
V_Book_ID NUMERIC,
V_Row_Num NUMERIC,
V_Book_OWNER TEXT,
V_Book_OWNER_ID TEXT
);
Run Code Online (Sandbox Code Playgroud)
这是我的功能:
CREATE OR REPLACE FUNCTION Update_Table(row_book Books[]) RETURNS TEXT AS $$
DECLARE
Status TEXT;
I_Max integer := array_length(row_book, 1);
BEGIN
FOR I in 1..I_Max
LOOP
INSERT INTO books_table(Book_ID,
Row_Num,
Book_OWNER,
Book_OWNER_ID)
values
(row_book[I].V_Book_ID,
row_book[I].V_Row_Num,
row_book[I].V_Book_OWNER,
row_book[I].V_Book_OWNER_ID);
END LOOP;
STATUS:='Saved';
exception when others then
STATUS:='failure';
RETURN STATUS;
END;
$$ …Run Code Online (Sandbox Code Playgroud) create or replace function test()
returns void as $$
begin
update tbl set col1 = true where col2 = false;
-- now I want to raise exception if update query affected more than 2 rows to rollback the update
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
如何在函数中为变量选择受影响的行数?
最初,我们在默认的“公共”架构中安装了所有扩展。现在我们有越来越多的其他模式 - 因此我们希望向模式添加扩展和通用功能common。
不幸的是,AWS RDS 在尝试像这样移动现有的时会报告:
ALTER EXTENSION "hstore" set schema common;
DBCException: SQL Error [42501]: ERROR: must be owner of extension hstore
org.postgresql.util.PSQLException: PSQLException: ERROR: must be owner of extension hstore
Run Code Online (Sandbox Code Playgroud)
我正在使用也是角色成员的默认用户rds_superuser。
PostgreSQLCOPY命令是否可以选择将 CSV 列映射到的字段?
PostgreSQLCOPY命令似乎期望目标表与其列完全匹配。我错过了什么还是它实际上是如何工作的?
是否有一些替代命令可以启用它?
PostgreSQL 9.2。
[local] postgres@host=# select * from units where ip_address LIKE '192.168.43.%';
ERROR: 42883: operator does not exist: inet ~~ unknown
LINE 1: select * from units where ip_address LIKE '192.168.43.%';
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
LOCATION: op_error, parse_oper.c:722
Time: 0.309 ms
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我在尝试改进两个空间表之间的交集时遇到了困难,我想收到有关表设计、查询或 dba 配置的任何提示。
表现在teste.recorte_grade有 1,655,569 行,但这是为 900 万行表的测试制作的子样本。
CREATE TABLE teste.recorte_grade
(
id integer NOT NULL DEFAULT nextval('teste."Recorte_grade_id_seq"'::regclass),
id_gre character varying(21),
indice_gre character varying(16),
the_geom geometry(Polygon),
CONSTRAINT "Recorte_grade_pkey" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
CREATE INDEX sidx_recorte_grade_geom
ON teste.recorte_grade
USING gist
(the_geom);
Run Code Online (Sandbox Code Playgroud)
表teste2.uso_2012有 177,888 行,这是它将永远拥有的所有数据。
CREATE TABLE teste2.uso_2012
(
id integer NOT NULL,
gridcode smallint NOT NULL,
geom geometry(MultiPolygon) NOT NULL,
CONSTRAINT pk_id_uso_2012 PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
CREATE INDEX …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Postgres 从 9.3 升级到 9.5。我遇到了两个障碍,但不知道如何绕过它们。我将 9.3 PGDATA 设置为 /data/postgres,这是我的 RHEL 6 服务器上的一个单独文件系统。安装 9.5 并运行 initdb 后,我尝试使用以下命令行运行 pg_upgrade:
/usr/pgsql-9.5/bin/pg_upgrade -b /usr/pgsql-9.3/bin -B /usr/pgsql-9.5/bin -d /data/postgres -D /var/lib/pgsql/9.5/data --link -p 5432 -P 5433 -c
此命令未通过链接参数测试,因为 9.5 PGDATA 值与 9.3 不在同一文件系统上。路障 #1。
然后我尝试使用以下命令重新初始化 9.5:
initdb -D /data/postgres95
Run Code Online (Sandbox Code Playgroud)
它错误地说“数据目录不是空的!”。路障 #2。
如何继续进行此升级?我宁愿不搬到新服务器。
表中有以下数据:
ID Category Value
1234 Cat01 V001
1234 Cat02 V002
1234 Cat03 V003
1234 Cat03 V004
1234 Cat03 V005
Run Code Online (Sandbox Code Playgroud)
我想要以下输出:
ID Cat01 Cat02 Cat03
1234 V001 V002 V003
1234 V001 V002 V004
1234 V001 V002 V005
Run Code Online (Sandbox Code Playgroud)
我想要实现的输出是一种数据透视表,其中我将所有值垂直放置在一个表中,并且我希望将这些值水平放置,并将类别作为列。但是有些类别具有多个值,在这种情况下,我需要重复所有其他类别的值并为每个重复值创建一行
如何在 PostgreSQL 中完成?
postgresql ×10
performance ×2
amazon-rds ×1
array ×1
copy ×1
csv ×1
functions ×1
index-tuning ×1
pivot ×1
plpgsql ×1
postgis ×1
spatial ×1
upgrade ×1