libpq example doesn't work

Started by Ideriha, Takeshiover 7 years ago3 messages
#1Ideriha, Takeshi
ideriha.takeshi@jp.fujitsu.com
1 attachment(s)

Hi,

When I tried to use libpq, I found that libpq example[1]https://www.postgresql.org/docs/devel/static/libpq-example.html does not work.
That's because SELECT pg_catlog.set_config() never returns PGRES_COMMAND_OK which is expected,
but actually returns PGRES_TUPLES_OK.

Patch attached.
I changed both src/test/example and document.

[1]: https://www.postgresql.org/docs/devel/static/libpq-example.html

(By the way, I'm wondering why these examples are placed under src/test.
They are just examples and not regression tests.)

Best regards,
Takeshi Ideriha

Attachments:

0001-libpq-example-fix.patchapplication/octet-stream; name=0001-libpq-example-fix.patchDownload
From aa726147458ac616ef76cdaec3205bc219113ab0 Mon Sep 17 00:00:00 2001
From: "ideriha.takeshi" <ideriha.takeshi@jp.fujitsu.com>
Date: Wed, 27 Jun 2018 15:13:17 +0900
Subject: [PATCH] libpq example fix libpq example program doesn't work because
 SELECT pg_catlog.set_config() never returns PGRES_COMMAND_OK

---
 doc/src/sgml/libpq.sgml        | 4 ++--
 src/test/examples/testlibpq.c  | 2 +-
 src/test/examples/testlibpq2.c | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 498b8df..efda9ae 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -8440,7 +8440,7 @@ main(int argc, char **argv)
     /* Set always-secure search path, so malicous users can't take control. */
     res = PQexec(conn,
                  "SELECT pg_catalog.set_config('search_path', '', false)");
-    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+    if (PQresultStatus(res) != PGRES_TUPLES_OK)
     {
         fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
         PQclear(res);
@@ -8610,7 +8610,7 @@ main(int argc, char **argv)
     /* Set always-secure search path, so malicous users can't take control. */
     res = PQexec(conn,
                  "SELECT pg_catalog.set_config('search_path', '', false)");
-    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+    if (PQresultStatus(res) != PGRES_TUPLES_OK)
     {
         fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
         PQclear(res);
diff --git a/src/test/examples/testlibpq.c b/src/test/examples/testlibpq.c
index 92a05e5..d9c6c45 100644
--- a/src/test/examples/testlibpq.c
+++ b/src/test/examples/testlibpq.c
@@ -51,7 +51,7 @@ main(int argc, char **argv)
 	/* Set always-secure search path, so malicous users can't take control. */
 	res = PQexec(conn,
 				 "SELECT pg_catalog.set_config('search_path', '', false)");
-	if (PQresultStatus(res) != PGRES_COMMAND_OK)
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
 		fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
 		PQclear(res);
diff --git a/src/test/examples/testlibpq2.c b/src/test/examples/testlibpq2.c
index 76787fe..62ecd68 100644
--- a/src/test/examples/testlibpq2.c
+++ b/src/test/examples/testlibpq2.c
@@ -80,7 +80,7 @@ main(int argc, char **argv)
 	/* Set always-secure search path, so malicous users can't take control. */
 	res = PQexec(conn,
 				 "SELECT pg_catalog.set_config('search_path', '', false)");
-	if (PQresultStatus(res) != PGRES_COMMAND_OK)
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
 		fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
 		PQclear(res);
-- 
1.8.3.1

#2Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Ideriha, Takeshi (#1)
Re: libpq example doesn't work

On 27.06.18 08:29, Ideriha, Takeshi wrote:

When I tried to use libpq, I found that libpq example[1] does not work.
That's because SELECT pg_catlog.set_config() never returns PGRES_COMMAND_OK which is expected,
but actually returns PGRES_TUPLES_OK.

Fixed. There were additional similar cases that I fixed also.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#3Ideriha, Takeshi
ideriha.takeshi@jp.fujitsu.com
In reply to: Peter Eisentraut (#2)
RE: libpq example doesn't work

-----Original Message-----
From: Peter Eisentraut [mailto:peter.eisentraut@2ndquadrant.com]
Sent: Sunday, July 1, 2018 9:17 PM
To: Ideriha, Takeshi/出利葉 健 <ideriha.takeshi@jp.fujitsu.com>; pgsql-hackers
<pgsql-hackers@postgresql.org>
Subject: Re: libpq example doesn't work

On 27.06.18 08:29, Ideriha, Takeshi wrote:

When I tried to use libpq, I found that libpq example[1] does not work.
That's because SELECT pg_catlog.set_config() never returns
PGRES_COMMAND_OK which is expected, but actually returns PGRES_TUPLES_OK.

Fixed. There were additional similar cases that I fixed also.

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Thank you for the commit. I totally missed the large object examples.

Regards,
Takeshi Ideriha