我很难理解为什么以下查询不起作用?
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,可能已经忘记了基础知识。任何建议都非常感激
我想使用 wal2json 扩展来捕获 PostgreSQL 数据库中的更改。我需要找到新旧行之间的差异。在互联网上搜索答案后,我意识到这可以通过在感兴趣的表上设置 REPLICA IDENTITY FULL 选项来实现。
我认为这正是我所需要的,但我想知道这种设置有什么明显的缺点吗?
我有一个中等大小的“functionCalls”表(约 4M 行),由 2 列组成,input并且function(另一个表的两个 id):
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\nRun Code Online (Sandbox Code Playgroud)\n\n我想找到与某个函数匹配的所有行,这就是我添加索引的原因functionCallSearch。这是我的查询:
Column | Type | Collation | …Run Code Online (Sandbox Code Playgroud) 我想做这样的事情:
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 中如何刷新存储的生成列的值。
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 恢复到 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 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) timestamp with time zone对比timestamp without time zone:
我知道这timestamptz与在数据库中保存值时添加的时间戳无时区+(时区差)相同。也就是说,在数据库中,当存储时,它们都是无法区分的。
比如说,我决定只使用timestamp without time zone,即使是在推荐的情况下timestamp with time zone。我网站的用户可以记住,我和他们将始终使用 UTC 进行操作,即使我们碰巧处于不同的时区,效果会timestamp without time zone很好且没有缺陷吗?
我有两张桌子,一张是Category,另一张是Product。
表说明有
类别
产品
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)我想我进行了错误的数据库恢复,或者转储没有此数据,并且“类似文件夹”的结构丢失了。这是 Dbeaver 的捕获,显示了我对“类似文件夹”结构的理解。我可以轻松访问存储在数据库定义中的函数、表、模式等。我不太担心原始结构的丢失,因为它没有太多,但我确实想在需要时逐步重新创建它。例如,现在我创建了一个函数并希望在“Functions”文件夹下看到它,我该怎么做?
另外,我应该在 pg_dump 生成脚本中包含什么来备份这个结构?
谢谢。
postgresql ×10
dbeaver ×1
join ×1
locking ×1
performance ×1
replication ×1
select ×1
subquery ×1
timestamp ×1
timezone ×1
update ×1