标签: postgresql

如何获得每组第二高的值?

从表中获取第二高值已经解决了很多次,但我正在寻找每组中的第二高值。

鉴于此表:

+----+-----+
| A  |  10 |
| A  |  20 |
| A  |  35 |  <-- This record
| A  |  42 |
| B  |  12 |
| B  |  21 |  <-- This record
| B  |  33 |
| C  |  14 |
| C  |  23 |
| C  |  38 |
| C  |  41 |  <-- This record
| C  |  55 |
+----+-----+
Run Code Online (Sandbox Code Playgroud)

我想获得标记的行。
伪代码:

select col_a, penultimate(col_b) …
Run Code Online (Sandbox Code Playgroud)

postgresql select greatest-n-per-group max postgresql-9.6

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

删除月份和日期的前导零

PostgreSQL 聊天室中的某人给了我这个语法。它用在VIEW输出 RSS feed 链接的 a 中:

to_char(tips_chronic_pain_weekly_selection.start_date, 'YYYY/MM/DD'::text)
Example output: 2021/09/13
Run Code Online (Sandbox Code Playgroud)

我不熟悉这是如何工作的。我可以删除月份和日期中的前导0吗?这是几个月前给我的,作为以下内容的缩短版本:

concat( extract( YEAR FROM tips_physical_disability_weekly_selection.start_date), '/',  extract( MONTH FROM tips_physical_disability_weekly_selection.start_date), '/',  extract( DAY FROM tips_physical_disability_weekly_selection.start_date) )
Run Code Online (Sandbox Code Playgroud)

postgresql date-format

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

使用LSN监控逻辑复制

如何通过查看 lsn 来监控 Postgresql 12 中的逻辑复制?

我所做的:检查发布者和订阅者上的一些列。

出版方:

select * from pg_stat_replication; -- 查看 REPLAY_LSN

select * from pg_replication_slots; -- 参见 CONFIRMED_FLUSH_LSN

订阅方:

select * from pg_catalog.pg_stat_subscription; -- 查看 RECEIVED_LSN 和 LATEST_END_LSN

我确保这些列中的所有值都相同。

我对么 ?或者是否有其他方法可以通过检查某些参数来查看复制工作?

postgresql replication data-synchronization postgresql-12

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

频繁运行AUTO VACUUM对PostgreSQL数据库有影响吗?

我有10-15个大表,5GB到30GB。其余的都小于1GB。有些表数据很大,每天会产生 500 000 个死元组。大约 7 天后,当死亡元组超过 350 万个时,自动清理就会开始。该数据库大约有 60 个表。这里数据库的阈值是:20%。

我想将 autovacuum 阈值从 20% 更改为 1%。对系统性能有什么影响(因为对于较小的表,它会频繁运行)。maintanance_work_mem 和 work_mem 参数值应该是什么?

postgresql postgresql-performance postgresql-13

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

为什么 PostgreSQL WINDOW 函数会影响 select 中所有行的最终排序?

假设我们有以下查询。

\n
select\n    name,\n    pos,\n    rank() over (partition by constructor) r,\n    format('%s / %s',\n        row_number() over (partition by constructor),\n        count(*) over (partition by constructor)\n    ) "pos/global"\nfrom (values\n        ('d1-c1', 1, 'c1'),\n        ('d3-c1', 3, 'c1'),\n        ('d3-c2', 3, 'c2'),\n        ('d2-c1', 2, 'c1'),\n        ('d2-c2', 2, 'c2')\n    ) t(name, pos, constructor);\n
Run Code Online (Sandbox Code Playgroud)\n

输出如下:

\n
 name  \xe2\x94\x82 pos \xe2\x94\x82 r \xe2\x94\x82 pos/global\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\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 d1-c1 \xe2\x94\x82   1 \xe2\x94\x82 1 \xe2\x94\x82 1 / 3\n d3-c1 \xe2\x94\x82   3 \xe2\x94\x82 1 \xe2\x94\x82 2 / 3\n d2-c1 \xe2\x94\x82   2 \xe2\x94\x82 1 \xe2\x94\x82 …
Run Code Online (Sandbox Code Playgroud)

postgresql window-functions

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

Postgresql 基本目录中的大文件是什么

T 开头的文件名太多

(T14_xxxxxxxx.1, T14_xxxxxxxx.2 .. T14_xxxxxxxx.18)
Run Code Online (Sandbox Code Playgroud)

在一个数据库的 postgresql 基本目录中。

基本目录中的数据库大小为 500GB,包含这些文件。重新启动postgresql后,这些文件消失了,base/目录中的数据库大小减少了200GB。

我阅读了以下网站。但我找不到有关这些文件的任何信息:

https://www.postgresql.org/docs/current/storage-file-layout.html#:~:text=For%20each%20database%20in%20the,system%20catalogs%20are%20stored%20there.>

另外我还使用了 pg_relation_filepath 或选择 pg_class 表。我没有找到有关这些文件的任何信息。

“base”目录中的这些大文件是什么?这些是缓存文件吗?应用程序怎么会出现这些文件呢?

postgresql postgresql-performance

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

没有行的表的 CROSS JOIN

在定义中;

SELECT * 
from T1, T2
Run Code Online (Sandbox Code Playgroud)

Cross Join:如果T1有N行,T2有M行,结果集会有N×M行。

如果 T2 没有行,则 JOIN 的结果也没有行。

但是,我想要 N 行(来自 T1),我该怎么办?

有什么建议吗?

注意:我想要笛卡尔积,表之间没有共享列。

postgresql join

0
推荐指数
1
解决办法
6476
查看次数

查询的结构与函数结果类型不匹配

我想创建一个基本函数,它接受一个输入并抛出一个具有多列和多行的表作为输出。我为此编写了以下查询:

create or replace function proc_name_1 (store text)
returns table (question text , resp numeric , gendr text , resp_count integer) 
as $$
begin 
return query 
select question_type , response, gender, sum(response_count) 
from fashtagcompute.response_count
where store_id = store 
group by question_type , response, gender ;  
end ; 
$$ 
language plpgsql ; 

CREATE FUNCTION
Run Code Online (Sandbox Code Playgroud)

函数创建成功。但是,如果我尝试执行它,则会出现以下错误

select * from proc_name_1('ab1f47ff-5c96-46fb-b975-81bf92c01b63')

ERROR: structure of query does not match function result type 
DETAIL: Returned type character varying(80) does not match expected type text in …
Run Code Online (Sandbox Code Playgroud)

postgresql

0
推荐指数
1
解决办法
8387
查看次数

如何在where子句中使用postgresql变量

# It works! 
select * from device where device.user_id in (select user_id from user);

# It doesn't work! 
select user_id into test from user;
select * from device where device.user_id in test;
Run Code Online (Sandbox Code Playgroud)

为什么第一个有效,而第二个有效?

如果我想where在上面的子句中使用变量,我该怎么做?

postgresql

0
推荐指数
1
解决办法
1578
查看次数

为什么我会收到错误消息“'&lt;列名&gt;' 必须出现在 GROUP BY 子句中或用于聚合函数中”?

我哪里错了?为什么在添加 MAX 函数时会收到该消息?

查询涉及:

WITH
   references_cve AS (
      SELECT vulnerability_id, reference
      FROM dim_vulnerability_reference
      WHERE dim_vulnerability_reference.source = 'NVD'
   )
SELECT DISTINCT ON (
      da.ip_address,
      favi.port,
      dv.severity,dp.name, dv.title,
      dv.description,
      fix,
      dv.nexpose_id,
      cve.reference,
      da.host_name,
      dos.description,
      da.last_assessed_for_vulnerabilities,
      davbs.solution_id
   )
   da.ip_address AS IPAddress,
   CASE WHEN favi.port = -1 THEN NULL ELSE favi.port END AS Port,
   dv.severity AS Severity,
   dp.name AS Protocol,
   dv.title AS VulnerabilityName,
   htmlToText(dv.description) AS Summary,
   htmlToText(dv.description) AS Description,
   htmlToText(fix) as Solution,
   'https://www.rapid7.com/db/vulnerabilities/' || dv.nexpose_id as SeeAlso,
   dv.nexpose_id AS NexposeID,
   ''||null as PluginOutput,
   cve.reference …
Run Code Online (Sandbox Code Playgroud)

postgresql group-by errors

0
推荐指数
1
解决办法
579
查看次数