PG::DuplicatePstatement Error prepared statement axxx already exists

  1. Reference

That’s an interesting issue we faced in production during one deployment which only contain some frontend changes.

Upon check, this happens in such scenario:

A prepared statement is generated in postgresql, but never stored in rails. 
Since the code was interrupted before storing the statement, the @counter variable 
was never incremented even though it was used to generate a prepared statement.

That pretty much described the issue, prepared statement on postgres side is a server side object that can be used to optimize performance. When the PREPARE
statement is executed, the specified statement is parsed, analyzed, and rewritten. When an EXECUTEcommand is subsequently issued, the prepared statement is planned and executed.

When the identifiers already bound to existing prepared statements but rails does not realize it, this issue will be happened.

here is the fix

https://github.com/rails/rails/pull/41356/files

def next_key 
    "a#{@counter + 1}"
end 

def next_key 
    "a#{@counter += 1}"
end 

This change make the postgres prepared statement counter before makeing a prepared statement
Thus if the statemnt is aborted in rails side, app won't end up in perpetual crash state 

Reference

  1. https://github.com/rails/rails/issues/1627
  2. https://github.com/rails/rails/pull/25827
  3. https://github.com/rails/rails/pull/17607

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 stone2paul@gmail.com

文章标题:PG::DuplicatePstatement Error prepared statement axxx already exists

文章字数:218

本文作者:Leilei Chen

发布时间:2022-07-01, 22:04:04

最后更新:2022-07-01, 22:05:31

原始链接:https://www.llchen60.com/PG-DuplicatePstatement-Error-prepared-statement-axxx-already-exists/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏