标签: postgresql

可以使用 BRIN 索引代替 Postgres 中的表分区吗?

给定一个带有 GUID 和时间戳(以及一堆其他列)的仅附加表,该表可以增长约 50Mio 条目/年。我想保持较低的索引数量,只对由 id 查询的数据使用普通(B 树)索引。此外,数据将通过时间戳进行查询以进行分析。为此,我想按月或按年对表进行分区。

但是由于我的表只是附加的并且时间戳将非常连续(可能不是 100% 但通常我不会故意发布过去的项目)不应该 BRIN 索引基本上给我与表分区相同的功能手动创建子表而不是触发器的麻烦?(至少文档中是这样描述分区的:https : //www.postgresql.org/docs/current/static/ddl-partitioning.html

更新

也许是一些额外的背景——我考虑过按年或月对它进行分区,因为这是唯一可预测的事情。在这两种情况下,我都必须对该表执行查询,该查询将不包含任何时间戳信息,因此对于那些查询,我仍将使用 btree 索引,并且在 prtition postgresql 的情况下无法从查询中推断可以使用哪个较小的表(和索引)。

postgresql partitioning

6
推荐指数
1
解决办法
855
查看次数

Create Materialized View from foreign table

I'd like to create a simple materialized view from a table which lies in a different database. The two databases are on the same server.

What do I have to add to make the query access the foreign database and the table there?

CREATE MATERIALIZED VIEW mv_table_1 AS
  SELECT *
  FROM public.mv_table_1 --The schema & table from the different DB
WITH DATA;
Run Code Online (Sandbox Code Playgroud)

I tried using the fully qualified table name (db name before the schema name) but this results in …

postgresql materialized-view foreign-data postgresql-9.5

6
推荐指数
1
解决办法
2727
查看次数

Calculate 40 day moving average w.r.t to a field

I have a table that stores the information about user calls in a call center. The table has a call_id, date when the call was made, actual date and time of the call, call type and a score associated with the call.

My requirement is to calculate a 40 day moving average of the score with respect to the call day. The 40 day should start from the previous day from the call date. If there are no call in …

postgresql window-functions postgresql-9.4

6
推荐指数
1
解决办法
3047
查看次数

将多行合并为单行

在 PostgreSQL 9.5 中,我有一个名为的表reports

CREATE TABLE public.reports (
  id BIGSERIAL PRIMARY KEY,
  id_station character(11) NOT NULL,
  date date NOT NULL,
  element character(4) NOT NULL,
  value smallint NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

对于每个站(id_station列)和每一天(date列),我可能有多个值类型(元素列) : TMIN, TMAX, TAVG(有时这些值不存在:对于给定的一天,我可能只有 aTMIN和 a TMAX)。

这是一个(假)样本:

22;"FR069029001";"1925-01-01";"TMAX";130
23;"FR069029001";"1925-01-01";"TMIN";-25
24;"FR069029001";"1925-01-01";"TAVG";0
Run Code Online (Sandbox Code Playgroud)

我想使用此表将每个站点和每天的这些值合并在一行中:

CREATE TABLE public.reports_con (
id SERIAL PRIMARY KEY,
id_station character(11) NOT NULL,
date date NOT NULL,
tmin smallint,
tmax smallint,
tavg smallint
);
Run Code Online (Sandbox Code Playgroud)

我想达到这个结果: …

postgresql aggregate pivot aggregate-filter

6
推荐指数
1
解决办法
1万
查看次数

优化“WHERE x BETWEEN a AND b GROUP BY y”查询

CREATE TABLE test_table
(
  id uuid NOT NULL,
  "RefId" uuid NOT NULL,
  "timestampCol" timestamp without time zone NOT NULL,
  "bigint1" bigint NOT NULL,
  "bigint2" bigint NOT NULL,
  "int1" integer NOT NULL,
  "int2" integer NOT NULL,
  "bigint3" bigint NOT NULL,
  "bigint4" bigint NOT NULL,
  "bigint5" bigint NOT NULL,
  "hugeText" text NOT NULL,
  "bigint6" bigint NOT NULL,
  "bigint7" bigint NOT NULL,
  "bigint8" bigint NOT NULL,
  "denormalizedData" jsonb NOT NULL,
  "textCol" text NOT NULL,
  "smallText" text NOT NULL,
  "createdAt" timestamp with time zone …
Run Code Online (Sandbox Code Playgroud)

postgresql performance index optimization postgresql-9.4

6
推荐指数
1
解决办法
212
查看次数

返回带有附加列的表的函数

我想编写一个 PostgreSQL 函数,它返回一个表和一个附加列。有没有办法在不手动指定的情况下做到这一点RETURNS TABLE (col1 type, col2 type, ...)

例如,考虑以下函数:

CREATE FUNCTION get_users_with_most_videos_since_time(ts TIMESTAMPTZ)
  RETURNS SETOF "user" AS $$
    SELECT
      u.*,
      count(v.id) AS vids_since

    FROM "user" AS u
    INNER JOIN "video" AS v ON v.creator_id = u.id
    WHERE v.created_at > ts

    GROUP BY u.id
    ORDER BY vids_since DESC;
$$ LANGUAGE SQL;
Run Code Online (Sandbox Code Playgroud)

这失败并出现错误:

ERROR:  return type mismatch in function declared to return "user"
DETAIL:  Final statement returns too many columns.
Run Code Online (Sandbox Code Playgroud)

公平地说,我们包括了该vids_since列,该列不存在于"user"表中。

因此,要解决此问题,我想将其更改为:

CREATE FUNCTION get_users_with_most_videos_since_time(ts …
Run Code Online (Sandbox Code Playgroud)

postgresql datatypes view functions set-returning-functions

6
推荐指数
1
解决办法
4380
查看次数

PostgreSQL GIN pg_trgm 默认操作符类

我正在将 GIN 索引与pg_trgm用于索引varchar字段的模块一起使用。所以,要定义一个索引,我必须这样写:

CREATE INDEX gin_field_idx ON table_name USING gin (field gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)

如果我排除运算符类 ( git_trgm_ops) 并写入:

CREATE INDEX gin_field_idx ON table_name USING gin (field);
Run Code Online (Sandbox Code Playgroud)

PostgreSQL 将引发错误:

错误: 数据类型字符变体没有用于访问方法“gin”的默认运算符类
提示: 您必须为索引指定运算符类或为数据类型定义默认运算符类。

如何从模块定义默认操作符方法?

没有帮助。它描述了如何从扩展运算符和函数定义新的运算符类。但我必须从模块中定义默认操作符类。

任何帮助将不胜感激 :-)。

postgresql index

6
推荐指数
1
解决办法
1万
查看次数

从 Windows 客户端到 AWS RDS Postgresql 实例的 SSL 连接

我正在尝试使用安全 SSL 连接连接到托管在 AWS RDS 上的 PG 实例。我在哪里将公钥存储在 Windows 上以便它自动应用于连接?

请注意,当我打开 PGAdmin 时,它只允许我指定 .crt 和 .key 文件。有没有办法将它指向 .pem 文件?

我下载了这里引用的公钥:http : //docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts.General.SSL

在 Linux 上,将此密钥放入默认的 OpenSSL 目录是一件简单的事情,postgres 似乎可以检测到它并自行处理其余部分。

在 Windows 上,我还没有弄清楚如何执行此操作或将 .pem 文件存储在何处或如何将 PGAdmin 指向 .pem 文件。

postgresql aws ssl amazon-rds

6
推荐指数
1
解决办法
5882
查看次数

PostgreSQL 全文搜索:如何更改分类为“单词分隔符”的内容?

使用ts_debug我可以看到 Postgres 将哪些符号视为(我称之为)“单词分隔符”。

例子:

SELECT ts_debug('english', 'Hello. ABC')
Run Code Online (Sandbox Code Playgroud)

结果:

(asciiword,"Word, all ASCII",Hello,{english_stem},english_stem,{hello})
(blank,"Space symbols",". ",{},,)
(asciiword,"Word, all ASCII",ABC,{english_stem},english_stem,{abc})
Run Code Online (Sandbox Code Playgroud)

“.”(点空格)被视为单词分隔符。我的问题是,我需要 Postgres 来处理单个点“。” 也可以作为单词分隔符。

现在,如果我搜索ABC没有包含内容的条目Hello.ABC(点后没有空格),只会找到Hello. ABC.

我正在使用这个搜索查询:

SELECT description FROM incident WHERE
  to_tsvector('english', description) @@ to_tsquery('english', 'ABC')
Run Code Online (Sandbox Code Playgroud)

另一个例子:搜索IOException不会找到java.io.IOException

有没有办法将单个点(没有空格)视为单词分隔符?

postgresql full-text-search postgresql-9.4

6
推荐指数
0
解决办法
560
查看次数

在 Postgresql 9.5 中的大表上创建主键的最佳方法是什么?

我必须在 Postgtres 数据库中的一个大表(约 1 亿条记录)上创建一个主键。创建 pkey 的最佳和最快方法是什么?此列是一个序列列,我不想锁定表,因为这是高度事务性的数据库。

postgresql ddl postgresql-9.5

6
推荐指数
2
解决办法
5288
查看次数