crystal-pg icon indicating copy to clipboard operation
crystal-pg copied to clipboard

Are prepared statements supposed to bypass PARSE?

Open horrendo opened this issue 7 years ago • 4 comments

Maybe I'm doing something wrong here (or misunderstanding things) but this returns true:

puts "Prepared: #{db.prepared_statements?}"

I then ran this:

db.transaction do |trx|
  e1 = Time.measure do
    stmt = trx.connection.build("insert into junk(c1, c2, c3) values ($1, $2, $3)")
    10.times do |i|
      stmt.exec(i, ">" + i.to_s + "<", i & 2 == 0)
    end
  end
  puts "e1 = #{e1}"
  e2 = Time.measure do
    10.times do |i|
      trx.connection.exec("insert into junk(c1, c2, c3) values ($1, $2, $3)", i, ">" + i.to_s + "<", i & 2 == 0)
    end
  end
  puts "e2 = #{e2}"
  trx.rollback
end

I tailed the database log file and expected to see 1 parse followed by 10 sets of bind and execute with the first block, and 10 sets of parse, bind and execute with the second block. However, in both cases I see 10 sets of parse, bind and execute, so I'm wondering if I've misunderstood the purpose of Connection#build.

I noticed #97 so I'm wondering why the pg driver returns true for Connection#prepared_statements? ?

Thanks,

Steve

horrendo avatar Jul 07 '18 23:07 horrendo

I'm also running in to this. Are prepared statements properly supported?

jwoertink avatar Oct 11 '19 16:10 jwoertink

The entry point for prepared vs unprepared statement is at: https://github.com/will/crystal-pg/blob/master/src/pg/connection.cr#L20-L26 . They are all unprepared under the hood.

bcardiff avatar Oct 11 '19 16:10 bcardiff

So this line should probably look more like PreparedStatement.new(self, query) or whatever?

jwoertink avatar Oct 11 '19 16:10 jwoertink

Maybe. It depends on the underlying implementation or protocol. In mysql the binary and text protocol that are used for prepared or unprepared statements is very different. In sqlite all the statements are prepared. I don’t know about the internals of pg protocol.

bcardiff avatar Oct 12 '19 01:10 bcardiff