WIP: libpq: add a possibility to not send D(escribe) when executing a prepared statement

From: Ivan Trofimov <i(dot)trofimow(at)yandex(dot)ru>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: WIP: libpq: add a possibility to not send D(escribe) when executing a prepared statement
Date: 2023-11-21 22:58:48
Message-ID: 82c438b4-d91c-9009-65fc-593124d5a277@yandex.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi!

Currently libpq sends B(ind), D(escribe), E(execute), S(ync) when
executing a prepared statement.

The response for that D message is a RowDescription, which doesn't
change during prepared

statement lifetime (with the attributes format being an exception, as
they aren't know before execution) .

In a presumably very common case of repeatedly executing the same
statement, this leads to

both client and server parsing/sending exactly the same RowDescritpion
data over and over again.

Instead, library user could acquire a statement result RowDescription
once (via PQdescribePrepared),

and reuse it in subsequent calls to PQexecPrepared and/or its async
friends. Doing it this way saves

a measurable amount of CPU for both client and server and saves a lot of
network traffic, for example:

when selecting a single row from a table with 30 columns, where each
column has 10-symbols name, and

every value in a row is a 10-symbols TEXT, i'm seeing an amount of bytes
sent to client decreased

by a factor of 2.8, and the CPU time client spends in userland decreased
by a factor of ~1.5.

The patch attached adds a family of functions

PQsendQueryPreparedPredescribed, PQgetResultPredescribed,
PQisBusyPredescribed,

which allow a user to do just that.

If the idea seems reasonable i'd be happy to extend these to
PQexecPrepared as well and cover it with tests.

P.S. This is my first time ever sending a patch via email, so please
don't hesitate to point at mistakes

i'm doing in the process.

Attachment Content-Type Size
PQsendQueryPreparedPredescribed.patch text/x-patch 11.3 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2023-11-22 00:31:06 Re: [HACKERS] legitimacy of using PG_TRY , PG_CATCH , PG_END_TRY in C function
Previous Message Nathan Bossart 2023-11-21 22:40:06 Re: common signal handler protection