标签: postgresql

为什么 != 不起作用?如何与空值进行比较?

我很难理解为什么以下查询不起作用?

select id,sold_dealer_id from myi_corporate where sold_dealer_id != 36;

当我执行以下操作时,我得到结果

select id,sold_dealer_id from myi_corporate where sold_dealer_id = 36;

然而,!=我没有得到相反的结果。

在使用 MongoDB 很长时间后,我又回到了 psql,可能已经忘记了基础知识。任何建议都非常感激

postgresql

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

在 PostgreSQL 表中设置 REPLICA IDENTITY FULL 有什么缺点

我想使用 wal2json 扩展来捕获 PostgreSQL 数据库中的更改。我需要找到新旧行之间的差异。在互联网上搜索答案后,我意识到这可以通过在感兴趣的表上设置 REPLICA IDENTITY FULL 选项来实现。

我认为这正是我所需要的,但我想知道这种设置有什么明显的缺点吗?

postgresql replication change-data-capture

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

当 LIMIT 存在时,Postgres 12.1 使用仅索引向后扫描而不是索引

我有一个中等大小的“functionCalls”表(约 4M 行),由 2 列组成,input并且function(另一个表的两个 id):

\n\n
  Column  |  Type   | Collation | Nullable | Default \n----------+---------+-----------+----------+---------\n input    | integer |           | not null | \n function | integer |           | not null | \nIndexes:\n    "functionCall_pkey" PRIMARY KEY, btree (input, function) CLUSTER\n    "functionCallSearch" btree (function, input)\nForeign-key constraints:\n    "fkey1" FOREIGN KEY (function) REFERENCES function(id) ON UPDATE CASCADE ON DELETE CASCADE\n    "fkey2" FOREIGN KEY (input) REFERENCES input(id)\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想找到与某个函数匹配的所有行,这就是我添加索引的原因functionCallSearch。这是我的查询:

\n\n
  Column  |  Type   | Collation | …
Run Code Online (Sandbox Code Playgroud)

postgresql performance postgresql-performance

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

如何锁定行进行更新而不通过连接返回数据?

我想做这样的事情:

begin;
select * from foos where owner_id=123 and unread=true order by id for update;
update foos set unread=false where owner_id=123 and unread=true;
commit;
Run Code Online (Sandbox Code Playgroud)

目标是避免两个进程同时执行 UPDATE 时出现死锁。此处详细描述了问题:Why am I get a deadlock for a single UPDATE query?

在我获取锁的语句中,我不需要有关行的任何信息。我只想锁定那些特定的行。有没有办法做到这一点(优雅或hacky),告诉postgres不要做任何实际给我数据的工作?

postgresql locking update

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

刷新 Postgres 中存储的生成列

在 PostgreSQL 中如何刷新存储的生成列的值。

例子

CREATE TABLE people (
    first_name TEXT,
    last_name TEXT


);

CREATE OR REPLACE FUNCTION name(people) RETURNS text AS
$$
SELECT $1.first_name || ' ' || $1.last_name
$$ LANGUAGE SQL STRICT
                IMMUTABLE
        ;

alter table people add column   full_name text GENERATED ALWAYS AS (name(people)) STORED;

insert into people(first_name, last_name) values('John', 'Smith');

select full_name, full_name='John Smith' as pass from people;
Run Code Online (Sandbox Code Playgroud)

如果需要更改用于生成存储列值的函数,在这种情况下name如何要求 postgres 重新计算这些字段。

例如。


CREATE OR REPLACE FUNCTION name(people) RETURNS text AS
$$
SELECT $1.first_name || ' …
Run Code Online (Sandbox Code Playgroud)

postgresql computed-column

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

如何恢复大型 postgresql 数据库?

我正在努力将备份从本地 postgresql 恢复到 AWS 中托管的另一个 postgresql 数据库(rds-aurora postgresql,无服务器)。数据大小为170GB的压缩数据。

我之前想过使用 pg_restore 因为我可以传递该-j选项,但提取文件需要花费大量的时间和空间。

我的文件格式为= tar.gz (20200204_data_tar.gz)

我尝试使用管道提取并将其传递给 pg_restore 以节省时间,但出现以下错误。注意:备份是使用 pg_start_backup 生成的。

tar -xzOf 20200204_data_tar.gz | pg_restore
    --host=my-test.us-east-1.rds.amazonaws.com --port=5432 --username=postgresql--dbname=mytest -j 16 --password --verbose --exit-on-error --data-only 

pg_restore: [archiver] input file does not appear to be a valid archive
Run Code Online (Sandbox Code Playgroud)

本地版本 9.3.4 新集群版本使用 10.7

有人可以建议我如何恢复这个大型数据库吗?

postgresql postgresql-9.3 postgresql-10

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

为什么PostgreSQL的系统服务和pg_ctl服务不同?

由普通用户安装 PostgreSQL 10。

启用并启动其服务:

sudo systemctl start postgresql-10
sudo systemctl enable postgresql-10
Run Code Online (Sandbox Code Playgroud)

然后使用用户登录postgres

sudo su - postgres
Run Code Online (Sandbox Code Playgroud)

将此设置添加到.bash_profile文件中:

export PATH=$PATH:/usr/pgsql-10/bin/
Run Code Online (Sandbox Code Playgroud)

通过以下方式重新启动 PostgreSQL pg_ctl

-bash-4.2$ pg_ctl restart
Run Code Online (Sandbox Code Playgroud)

然后退出到普通用户,再次检查postgresql的状态,失败。即使用这种方式重启也不能成功:

sudo systemctl restart postgresql-10
sudo systemctl stop postgresql-10
Run Code Online (Sandbox Code Playgroud)

总是如此failed

但如果我使用postgres用户来测试重新启动、停止、启动,它们都可以成功。

这两种方式控制的不是同一个过程?


添加postgresql-10服务内容

$ sudo systemctl cat postgresql-10.service

# /usr/lib/systemd/system/postgresql-10.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  If you want to customize, the
# …
Run Code Online (Sandbox Code Playgroud)

postgresql

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

如果 UTC 始终可用,则使用不带时区的时间戳有任何缺点

timestamp with time zone对比timestamp without time zone

我知道这timestamptz与在数据库中保存值时添加的时间戳无时区+(时区差)相同。也就是说,在数据库中,当存储时,它们都是无法区分的。

比如说,我决定只使用timestamp without time zone,即使是在推荐的情况下timestamp with time zone。我网站的用户可以记住,我和他们将始终使用 UTC 进行操作,即使我们碰巧处于不同的时区,效果会timestamp without time zone很好且没有缺陷吗?

postgresql timestamp timezone

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

错误:子查询必须仅返回一列

我有两张桌子,一张是Category,另一张是Product

表说明有

类别

  • 姓名
  • 描述
  • Parent_id(自引用键)(最多可达 3 级)

产品

  • 姓名
  • 描述
  • 类型
  • category_id(类别表的外键)

    我想在单个产品上显示所有类别以及相关产品。因此,为了获取关联的类别,我使用以下查询。我可以在自引用表中进行左连接,但无法获取产品数据列表,因为这是一个子查询,而子查询只会返回单个列。

        select                                                                                                                                                                                                      
                   cat1.id, ARRAY(select name, type, description from product where
                   product.category_id = cat1.id)
                   as category_1_products_data,
    
                   cat2.id, ARRAY(select name, type, description from product where
                   product.category_id = cat2.id)
                   as category_2_products_data,
    
                   cat3.id, ARRAY(select name, type, description from product where
                   product.category_id = cat3.id)
                   as category_3_products_data
        from       category cat1
        left join  category cat2
        on         cat2.parent_id = cat1.id
        left join  category cat3
        on         cat3.parent_id = cat2.id
        where      cat1.parent_id is …
    Run Code Online (Sandbox Code Playgroud)

postgresql join subquery select

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

DBeaver 中缺少 Postgres 数据库的“文件夹”结构

我想我进行了错误的数据库恢复,或者转储没有此数据,并且“类似文件夹”的结构丢失了。这是 Dbeaver 的捕获,显示了我对“类似文件夹”结构的理解。我可以轻松访问存储在数据库定义中的函数、表、模式等。我不太担心原始结构的丢失,因为它没有太多,但我确实想在需要时逐步重新创建它。例如,现在我创建了一个函数并希望在“Functions”文件夹下看到它,我该怎么做?

另外,我应该在 pg_dump 生成脚本中包含什么来备份这个结构?

谢谢。

在此输入图像描述

postgresql dbeaver

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