简体   繁体   English

让Rails记录Postgres内部*全部*的方法?

[英]Way to Get Rails to Log *All* Of Postgres Internals?

Is there a way to set sql logging to be super verbose in rails? 有没有一种方法可以将SQL日志记录设置为在Rails中超级详细?

I have a uniqueness index check on the primary_key field of this table that doesn't seem to be working. 我对此表的primary_key字段进行了唯一性索引检查,但似乎无法正常工作。 I'd like to see what postgres is actually doing under the hood a bit more. 我想看看postgres在幕后实际上在做什么。 In other words, I see the INSERT statement, but I'd also like to see a list of the checks it performs before the insert. 换句话说,我看到了INSERT语句,但是我也想看到它在插入之前执行的检查的列表。

irb(main):006:0> SampleTable.create(primary_key: 'hi', archived_at: nil)


 (0.2ms)  BEGIN
  SampleTable Create (0.3ms)  INSERT INTO "sample_tables" ("primary_key", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["primary_key", "hi"], ["created_at", "2019-04-23 12:51:00.295952"], ["updated_at", "2019-04-23 12:51:00.295952"]]
   (6.0ms)  COMMIT
=> #<SampleTable id: 2, primary_key: "hi", archived_at: nil, created_at: "2019-04-23 12:51:00", updated_at: "2019-04-23 12:51:00">

If you're talking about unique index - checks are internal to postgres, rails feeds it an sql query and does not know how it is processed. 如果您在谈论唯一索引-检查是postgres的内部功能,则rails会向它提供一个sql查询,并且不知道它是如何处理的。

But note that validates :some_field, uniqueness: true is non-atomic and is prone to race conditions, because between rails' check and actual insert another insert may happen: 但是请注意, validates :some_field, uniqueness: true是非原子性的,容易出现竞争状况,因为在rails的检查和实际插入之间可能会发生另一个插入:

  1. Request A starts record creation, checks that record with same key is not present 请求A开始记录创建,检查是否存在具有相同键的记录
  2. Request B starts same procedure and also checks that key is not present 请求B开始相同的过程,并检查不存在密钥
  3. Request A proceeds to inserting 请求A继续插入
  4. Request B proceeds to inserting 请求B继续插入
  5. Both requests checked that key was not present, but you have a duplicate in db 两个请求都检查了该密钥不存在,但是您在数据库中有一个重复项

This validation should be backed by a unique index on database so that in case of race condition postgres will return error and ActiveRecord::RecordNotUnique will be thrown. 此验证应由数据库上的唯一索引支持,以便在出现竞争情况时,postgres将返回错误并抛出ActiveRecord::RecordNotUnique

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM