小编Lau*_*lbe的帖子

如何在PostgreSQL中使用DISTINCT加速查询?

如您所见,我有非常简单的SQL语句:

SELECT DISTINCT("CITY" || ' | '  || "AREA" || ' | ' || "REGION") AS LOCATION
FROM youtube
Run Code Online (Sandbox Code Playgroud)

youtube我在查询中使用的表有大约2500万条记录.查询需要很长时间才能完成(约25秒).我正在努力加快请求.

我创建了一个索引,如下所示,但我的查询更高仍然需要相同的时间才能完成.我做错了什么?顺便说一下,在我的情况下使用"分区"是否更好?

CREATE INDEX location_index ON youtube ("CITY", "AREA", "REGION")
Run Code Online (Sandbox Code Playgroud)

EXPLAIN 收益:

Unique (cost=5984116.71..6111107.27 rows=96410 width=32)
-> Sort (cost=5984116.71..6047611.99 rows=25398112 width=32)
   Sort Key: ((((("CITY" || ' | '::text) || "AREA") || ' | '::text) || "REGION"))
   -> Seq Scan on youtube (cost=0.00..1037365.24 rows=25398112 width=32) 
Run Code Online (Sandbox Code Playgroud)

@ george-joseph QUERY PLAN你的剧本:

在此输入图像描述

sql postgresql indexing distinct

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

如何在 PostgreSQL 字符串中的单引号后添加分号?

我无法在 PostgreSQL 中创建单引号后带有分号的字符串。例如。我需要创建一个像这样的字符串:

Delhi is India's capital; It's a beautiful state
Run Code Online (Sandbox Code Playgroud)

我如何创建这样的字符串?

我尝试了以下方法:

select 'Delhi is India\'s capital; It\'s a beautiful state'
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Invalid operation: unterminated quoted string at or near "'Delhi is India\'s capital" 
Position: 8;
Run Code Online (Sandbox Code Playgroud)

sql string postgresql datetime special-characters

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

具有默认特权的PostgreSQL放置角色

我试图放弃一个角色,并切断了与该角色的所有联系,但是我无法解决一个挥之不去的问题。当我运行这个:

drop role hank
Run Code Online (Sandbox Code Playgroud)

它告诉我:

ERROR:  role "hank" cannot be dropped because some objects depend on it
DETAIL:  privileges for default privileges on new functions belonging to role brandon in schema alteryx
privileges for default privileges on new relations belonging to role brandon in schema alteryx
Run Code Online (Sandbox Code Playgroud)

该DDL存在于架构中:

ALTER DEFAULT PRIVILEGES IN SCHEMA alteryx
GRANT INSERT, SELECT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON TABLES
TO hank;

ALTER DEFAULT PRIVILEGES IN SCHEMA alteryx
GRANT EXECUTE ON FUNCTIONS TO hank;
Run Code Online (Sandbox Code Playgroud)

当我revoke在它们上执行时,命令成功执行,但是特权保持不变。 …

postgresql ddl roles database-permissions

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

PostgreSQL 错误:关系已存在 - 创建表中的外键

我正在制作一个表格如下:

CREATE TABLE creator.lists
(
    _id bigserial PRIMARY KEY NOT NULL,
    account_id bigint NOT NULL,
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    display_name text DEFAULT '',
    name text DEFAULT '',
    extra jsonb,

    FOREIGN KEY (account_id)
        REFERENCES creator.accounts (_id)
            ON DELETE CASCADE
);
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

ERROR:  relation "account_id_index" already exists
Run Code Online (Sandbox Code Playgroud)

当我跑步时:

CREATE INDEX
    account_id_index
ON
    creator.lists
(
    account_id
);
Run Code Online (Sandbox Code Playgroud)

如何在外键上创建索引?我正在运行 v11.1

请注意,我之前也对另一个表运行过类似的命令:

CREATE INDEX
    account_id_index
ON
    creator.contacts
    (
        account_id
    );
Run Code Online (Sandbox Code Playgroud)

我不认为表之间的索引名称必须是唯一的?

database postgresql indexing namespaces

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

PostgreSQL 错误:进程仍在等待数据库 0 的关系 2676 上的 AccessShareLock

我们无法连接到 PostgreSQL 实例,我们必须重新启动 PostgreSQL 服务才能连接到该实例,但几天后我们开始收到相同的错误。

日志显示以下错误

2019-02-18 08:00:03.043 UTC [5053] postgres@utilitydbLOG:  process 5053 still waiting for AccessShareLock on relation 2676 of database 0 after 1000.431 ms
2019-02-18 08:04:04.486 UTC [5170] bucardo@bucardoLOG:  process 5170 still waiting for AccessShareLock on relation 2676 of database 0 after 1001.006 ms
2019-02-18 08:15:02.802 UTC [5445] postgres@utilitydbLOG:  process 5445 still waiting for AccessShareLock on relation 2676 of database 0 after 1000.425 ms
2019-02-18 08:30:02.844 UTC [5846] postgres@utilitydbLOG:  process 5846 still waiting for AccessShareLock on relation 2676 …
Run Code Online (Sandbox Code Playgroud)

database postgresql database-locking postgresql-9.6

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

从PostgreSQL删除未命名的约束

在PostgreSQL中,我具有以下表定义

create table file(
    file_id int generated by default as identity primary key,
    file_name text UNIQUE not null
);
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何删除对的唯一约束file_name

postgresql unique-constraint

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

如何退出数据库恢复模式(当前锁定为只读模式)

前一段时间建立了一个从数据库,用于备份或复制远程数据库。但是,我无法再使用基于 Delphi 的 ETL 写入数据库(该 ETL 适用于另一个数据库对,但迄今为止从未用于该特定数据库对)。复制数据库是由已离开公司的其他人设置的。我有理由确信这已被设置为复制数据库,但是后来离开的员工告诉我,由于不相关的原因,复制从未起作用。使用 ETL,我们可以(使用 SQL 查询)从一个数据库读取数据,然后写回到复制数据库,或者应该能够,因为它当前是只读的。

我努力了:

  1. 维护如VACUUM
  2. 尝试删除表和整个数据库
  3. 从主数据库恢复完整备份

这些都不起作用,而且我被告知数据库是只读的。

我已经查看postgresql.conf并看到它hot_standby被选中,所以我认为(但不是 100% 确定)数据库处于某种复制模式(我从未接触过 Postgres 支持的复制,所以我不知道) 。

我已检查权限pg_hba.conf并看到其中有一些用于复制的凭据。我不确定这是否会激活数据库的“复制模式”,或者只是意味着这些凭据仅用于复制。

我已经查看了数月的日志文件(自从我们的 IT 部门在大约 5 个月前升级了整个网络以来,这一直不起作用)。我看到下面的日志文件内容,几个月来一遍又一遍地重复,没有任何其他内容。请注意,文件中列出了下面显示的 IP 地址pg_hba.conf,因此凭据有效。

正如我通过使用发现的那样,数据库处于恢复模式:

select pg_is_in_recovery();
Run Code Online (Sandbox Code Playgroud)

这向我解释了为什么它是只读的,但为什么我不能恢复数据库,或者只是简单地转储整个数据库并重新开始(它是备份,因此丢失/恢复它不是问题)?

我很想尝试修改该recovery.conf文件(存在),但我读到/相信一旦启动恢复(在我的情况下已经启动),修改文件将不起作用。

我正在使用 Postgres 的旧版本:9.2.9

此处的任何帮助将不胜感激,因为我已经在这方面扎实工作了一天多了。

日志文件条目(示例):

FATAL:  could not connect to the primary server:
FATAL:  no pg_hba.conf entry for replication connection from host "192.168.20.2", user "postgres", SSL off

FATAL:  could not connect to the primary server: …
Run Code Online (Sandbox Code Playgroud)

postgresql recovery

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

在安装了 PostgreSQL 12.1 的 MacOS catalina 上的 virtualenv 中安装 psycopg2 有什么问题吗?

我收到这条消息:

Collecting psycopg2
  Using cached https://files.pythonhosted.org/packages/84/d7/6a93c99b5ba4d4d22daa3928b983cec66df4536ca50b22ce5dcac65e4e71/psycopg2-2.8.4.tar.gz
Building wheels for collected packages: psycopg2
  Building wheel for psycopg2 (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /Users/mohsen/.virtualenvs/jalas-env/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/vh/0_hbs48171574k5y9pb97st80000gn/T/pip-install-y72dxnyb/psycopg2/setup.py'"'"'; __file__='"'"'/private/var/folders/vh/0_hbs48171574k5y9pb97st80000gn/T/pip-install-y72dxnyb/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/vh/0_hbs48171574k5y9pb97st80000gn/T/pip-wheel-51dm_zyp --python-tag cp37
       cwd: /private/var/folders/vh/0_hbs48171574k5y9pb97st80000gn/T/pip-install-y72dxnyb/psycopg2/
  Complete output (144 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.15-x86_64-3.7
  creating build/lib.macosx-10.15-x86_64-3.7/psycopg2
  copying lib/_json.py -> build/lib.macosx-10.15-x86_64-3.7/psycopg2
  copying lib/extras.py -> build/lib.macosx-10.15-x86_64-3.7/psycopg2
  copying lib/compat.py -> build/lib.macosx-10.15-x86_64-3.7/psycopg2
  copying …
Run Code Online (Sandbox Code Playgroud)

postgresql pip psycopg2 virtualenv macos-catalina

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

Oracle 与 Postgres 排序依据

我在 Oracle 和 Postgres 中运行以下查询,两者在值的排序方面显示不同的输出。

with test as (
select 'Summary-Account by User (Using Contact ID)' col1 from dual
    union all
select 'Summary-Account by User by Client by Day (Using Contact ID)' col1 from dual
)
select * from test
order by col1 desc;
Run Code Online (Sandbox Code Playgroud)

下面是甲骨文的一个

预言机输出

Postgres

with test as (
select 'Summary-Account by User (Using Contact ID)' col1
    union all
select 'Summary-Account by User by Client by Day (Using Contact ID)' col1
)
select * from test
order by col1 …
Run Code Online (Sandbox Code Playgroud)

oracle postgresql database-migration

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

查询成功返回,但 pgadmin 4 中没有结果

无论我运行什么,它都会显示“查询在 xx 秒内成功返回”。但数据输出选项卡上没有结果。

我运行什么查询并不重要,即使是基本的查询Select * from table_name也是一样的。

我必须打开一个新的查询工具窗口并运行相同的查询,直到得到结果。就算我得到了,那也是一次。当我在该选项卡上第二次执行相同的查询时,它再次失败,表示查询已成功返回。

我在 Ubuntu 22.04 上使用 pgAdmin 4。

显示问题的图像

有人可以帮帮我吗?

postgresql pgadmin-4 ubuntu-22.04

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