我的查询(从现有表创建新表)需要很长时间。所以我在我的办公室建立了一个远程数据库——那里有更多的内存。
我可以像往常一样使用 psql 从家里连接到我的数据库。
如何告诉远程服务器从终端执行我的查询而不必等待响应?
(postgresql-9.2 , linux 环境)
编辑:我对其他解决方案持开放态度,没有必要使用 psql
我们有一个类似的查询
SELECT COUNT(1)
FROM article
JOIN reservation ON a_id = r_article_id
WHERE r_last_modified < now() - '8 weeks'::interval
AND r_group_id = 1
AND r_status = 'OPEN';
Run Code Online (Sandbox Code Playgroud)
由于它经常遇到超时(10 分钟后),我决定调查这个问题。
该EXPLAIN (ANALYZE, BUFFERS)输出如下所示:
Aggregate (cost=264775.48..264775.49 rows=1 width=0) (actual time=238960.290..238960.291 rows=1 loops=1)
Buffers: shared hit=200483 read=64361 dirtied=666 written=8, temp read=3631 written=3617
I/O Timings: read=169806.955 write=0.154
-> Hash Join (cost=52413.67..264647.65 rows=51130 width=0) (actual time=1845.483..238957.588 rows=21644 loops=1)
Hash Cond: (reservation.r_article_id = article.a_id)
Buffers: shared hit=200483 read=64361 dirtied=666 written=8, temp read=3631 written=3617
I/O …Run Code Online (Sandbox Code Playgroud) 在从 恢复数据库时pg_dump,会产生许多错误,随后整个表都会被忽略。一个例子:
ERROR: insert or update on table "channelproducts" violates foreign key constraint "fk_rails_dfaae373a5"
DETAIL: Key (channel_id)=(1) is not present in table "channels".
Run Code Online (Sandbox Code Playgroud)
有趣的是,我注意到所有这些实例都是由于加载顺序而弹出的。channels是channelproducts按字母顺序和在文件中之后,因此我可以理解为什么 postgres 抱怨必须创建一个没有父母的孩子。
警告:外键是由 rails 4.2 自动生成的:我可以从源头上删除问题,但这仍然没有真正解决问题......
版本:PostgreSQL 9.4.4。
psql如果已经创建了数据库表和列,那么如何从外键约束的情况中恢复?
我有这个 tSQL 代码可以正常工作:
SELECT
c.logguid,
a.b.value('./PropertyValue', 'varchar(max)') asd
FROM [dnn].[dbo].[EventLog2] c
cross apply sss.nodes('/LogProperties/LogProperty[PropertyName=sql:variable("@x") and PropertyValue=sql:variable("@y")]') as a(b)
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够做的是传入许多对值的动态列表,这些值对在每个值之间进行 OR 运算,即
SELECT
c.logguid,
a.b.value('./PropertyValue', 'varchar(max)') asd
FROM [dnn].[dbo].[EventLog2] c
cross apply sss.nodes(
'/LogProperties/LogProperty[PropertyName=sql:variable("@x") and PropertyValue=sql:variable("@y")
or
PropertyName=sql:variable("@a") and PropertyValue=sql:variable("@b")
]'
) as a(b)
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我有以下问题。
SELECT * FROM tgvbn();
ERROR: function tgvbn() does not exist
LINE 1: SELECT * FROM tgvbn();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Run Code Online (Sandbox Code Playgroud)
由于官方文档的附录 A只说明了 error_codes 和条件名称,而没有说明实际的消息,因此只能猜测“没有函数匹配...”指的是42883 / undefined_function. 我可以设置log_error_verbosity = verbose在postgresql.conf-但会影响日志,不返回到客户端中的消息:
控制在服务器日志中为记录的每条消息写入的详细信息量。
好吧,我可以查阅日志以获取这条信息,但是有没有办法将其包含在错误消息中?
我想知道使用热备时如何切换2台服务器的角色?
我有 2 台服务器 :S_1和S_2,并且在时间 T 时,S_1是主服务器,并且S_2是具有流复制的热备用服务器。
稍后,我想切换S_2为主服务器和S_1热备用服务器。
我需要做什么才能转换角色?
我正在 MySQL 中创建一个触发器,我需要一些帮助。
我在 2 个不同的 Web 服务器 S1 和 S2 上有 2 个网站、2 个数据库(同名)。
这些数据库具有相同的表名。
我希望两个网站上的用户数据都相同。
因此,如果一个用户在 S1 上注册,那么该用户注册信息应该传递给 S2。
如果在 S1 上更新了用户注册信息,则应在 S2 上更新相同的信息。
这同样适用于 S2。
如何创建触发器,以便每次在 S1 上的数据库中进行插入/更新/删除时,S2 上的用户表也会自动更新。
并且每次在 S2 上的数据库中进行插入/更新/删除操作时,S1 上的用户表也会自动更新。
这可能吗?你能提供一些例子吗?
我有一个带有用户表的 heroku postgres 附加数据库。但是,当我尝试时select * from user,我得到的只是:
current_user
----------------
rtsjlhdfptlaqd
(1 row)
Run Code Online (Sandbox Code Playgroud)
表名肯定是user. 没有users桌子。我的应用程序中肯定有很多用户,但我似乎无法列出它们。
想法?
我觉得我需要json_object_agg()Postgres 9.4的功能,但我现在无法从 9.3 升级。有没有办法在 9.3 中做我想做的事?这是我的场景。我有一个click_activity看起来像的数据表
user | offer | clicks
-----|-------|--------
fred |coupons| 3
fred |cars | 1
john |coupons| 2
Run Code Online (Sandbox Code Playgroud)
但我想把它变成这个:(聚合每个用户的活动)
user | activity
-----|----------
fred | {"coupons": 3, "cars": 1}
john | {"coupons": 2}
Run Code Online (Sandbox Code Playgroud)
我认为json_object_agg()Postgres 9.4的功能可以完美地做到这一点,我只需要调用
select user, json_object_agg(offer, clicks) from click_activity group by 1
Run Code Online (Sandbox Code Playgroud)
有没有办法在 9.3 中做到这一点?谢谢!
这很好地说明了这个问题:
当 b 列是文本类型而不是数组时,以下工作:
select *
from json_to_record('{"a":1,"b":["hello", "There"],"c":"bar"}')
as x(a int, b text, d text);
a | b | d
---+--------------------+---
1 | ["hello", "There"] |
Run Code Online (Sandbox Code Playgroud)
但是,如果我将该b列定义为数组,则会出现此错误:
select *
from json_to_record('{"a":1,"b":["hello", "There"],"c":"bar"}')
as x(a int, b text[], d text)
ERROR: malformed array literal: "["hello", "There"]"
DETAIL: "[" must introduce explicitly-specified array dimensions.
Run Code Online (Sandbox Code Playgroud)
如何说服/强制json_to_record(或json_populate_record)将 JSON 数组转换为目标列类型的 Postgres 数组?
postgresql ×8
json ×2
psql ×2
client ×1
errors ×1
foreign-key ×1
index ×1
mysql ×1
pg-dump ×1
replication ×1
restore ×1
t-sql ×1
trigger ×1
xml ×1