我有一个 Postgres 数据库,其中包含有关服务器集群的详细信息,例如服务器状态(“活动”、“待机”等)。活动服务器在任何时候都可能需要故障转移到备用服务器,我不在乎特别使用哪个备用服务器。
我想要一个数据库查询来更改备用服务器的状态 - 只有一个 - 并返回要使用的服务器 IP。选择可以是任意的:因为服务器的状态随着查询而改变,所以选择哪个备用数据库并不重要。
是否可以将我的查询限制为一次更新?
这是我到目前为止所拥有的:
UPDATE server_info SET status = 'active'
WHERE status = 'standby' [[LIMIT 1???]]
RETURNING server_ip;
Run Code Online (Sandbox Code Playgroud)
Postgres 不喜欢这样。我可以做些什么不同的事情?
表中的每一行都有一个系统列 ctid,其类型tid表示该行的物理位置:
Run Code Online (Sandbox Code Playgroud)create table t(id serial); insert into t default values; insert into t default values;
Run Code Online (Sandbox Code Playgroud)select ctid , id from t;ctid | ID :---- | -: (0,1) | 1 (0,2) | 2
dbfiddle在这里
从ctid最合适的类型(例如integer,bigint或numeric(1000,0))中获取页码的最佳方法是什么?
在我能想到的唯一的办法是非常难看。
Oracle SQL 中有一项技术可用于简化聚合查询:
聚合特定列,但使用 SELECT 列表中的简单计算列从不同列获取信息。
--Oracle
--For a given country, what city has the highest population? (where the country has more than one city)
--Include the city name as a column.
select
country,
count(*),
max(population),
any_value(city) keep (dense_rank first order by population desc) --<<--
from
cities
group by
country
having
count(*) > 1
Run Code Online (Sandbox Code Playgroud)
如上所示,以下列可以带入城市名称,即使城市名称不在 GROUP BY 中:
any_value(city) keep (dense_rank first order by population desc)
Run Code Online (Sandbox Code Playgroud)
有多种方法可以使用 SQL 来实现此类操作。我正在 PostgreSQL 中寻找一种解决方案,让我可以在计算列中完成此操作 - 所有这些都在单个 SELECT 查询中(没有子查询、联接、WITH 等)。
问题:PostgreSQL 中是否有与 Oracle …
postgresql ×3
aggregate ×1
cast ×1
concurrency ×1
data-pages ×1
datatypes ×1
group-by ×1
oracle ×1
queue ×1
update ×1