Method: PG::Connection#async_exec

Defined in:
ext/pg_connection.c

#exec(sql) ⇒ PG::Result #exec(sql) {|pg_result| ... } ⇒ Object Also known as: async_query

Sends SQL query request specified by sql to PostgreSQL. On success, it returns a PG::Result instance with all result rows and columns. On failure, it raises a PG::Error.

For backward compatibility, if you pass more than one parameter to this method, it will call #exec_params for you. New code should explicitly use #exec_params if argument placeholders are used.

If the optional code block is given, it will be passed result as an argument, and the PG::Result object will automatically be cleared when the block terminates. In this instance, conn.exec returns the value of the block.

#exec is an alias for #async_exec which is almost identical to #sync_exec . #sync_exec is implemented on the simpler synchronous command processing API of libpq, whereas #async_exec is implemented on the asynchronous API and on ruby’s IO mechanisms. Only #async_exec is compatible to Fiber.scheduler based asynchronous IO processing introduced in ruby-3.0. Both methods ensure that other threads can process while waiting for the server to complete the request, but #sync_exec blocks all signals to be processed until the query is finished. This is most notably visible by a delayed reaction to Control+C. It’s not recommended to use explicit sync or async variants but #exec instead, unless you have a good reason to do so.

See also corresponding libpq function.

Overloads:

  • #exec(sql) {|pg_result| ... } ⇒ Object

    Yields:

    • (pg_result)


4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
# File 'ext/pg_connection.c', line 4751

static VALUE
pgconn_async_exec(int argc, VALUE *argv, VALUE self)
{
  VALUE rb_pgresult = Qnil;

  pgconn_discard_results( self );
  pgconn_send_query( argc, argv, self );
  rb_pgresult = pgconn_async_get_last_result( self );

  if ( rb_block_given_p() ) {
    return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
  }
  return rb_pgresult;
}