Re: real/float example for testlibpq3

2023-07-04 Thread Daniel Gustafsson
> On 21 Mar 2023, at 14:44, Mark Wong  wrote:
> 
> On Mon, Mar 20, 2023 at 01:37:57PM -0400, Gregory Stark (as CFM) wrote:
>> On Mon, 23 Jan 2023 at 11:54, Mark Wong  wrote:
>> fficient way to communicate useful information.
>>> 
>>> Yeah, I will try to cover all the data types we ship. :)  I'll keep at
>>> it and drop the code examples.
>> 
>> I assume this isn't going to happen for this commitfest? If you think
>> it is then shout otherwise I'll mark it Returned with Feedback and
>> move it on to the next release.
> 
> Sounds good.  I actually thought I did that already, thanks for catching
> that.

This has been waiting on author since January, unless there is a new patch
ready I will close this as Returned with Feedback and you can resubmit for
another CF when there is a new patch.

--
Daniel Gustafsson





Re: real/float example for testlibpq3

2023-03-21 Thread Mark Wong
On Mon, Mar 20, 2023 at 01:37:57PM -0400, Gregory Stark (as CFM) wrote:
> On Mon, 23 Jan 2023 at 11:54, Mark Wong  wrote:
> fficient way to communicate useful information.
> >
> > Yeah, I will try to cover all the data types we ship. :)  I'll keep at
> > it and drop the code examples.
> 
> I assume this isn't going to happen for this commitfest? If you think
> it is then shout otherwise I'll mark it Returned with Feedback and
> move it on to the next release.

Sounds good.  I actually thought I did that already, thanks for catching
that.

Regards,
Mark
--
Mark Wong
EDB https://enterprisedb.com




Re: real/float example for testlibpq3

2023-03-20 Thread Gregory Stark (as CFM)
On Mon, 23 Jan 2023 at 11:54, Mark Wong  wrote:
fficient way to communicate useful information.
>
> Yeah, I will try to cover all the data types we ship. :)  I'll keep at
> it and drop the code examples.

I assume this isn't going to happen for this commitfest? If you think
it is then shout otherwise I'll mark it Returned with Feedback and
move it on to the next release.

-- 
Gregory Stark
As Commitfest Manager




Re: real/float example for testlibpq3

2023-01-23 Thread Mark Wong
On Fri, Jan 20, 2023 at 01:12:07PM -0500, Robert Haas wrote:
> On Fri, Jan 20, 2023 at 12:58 PM Tom Lane  wrote:
> > I don't mind if you write something like
> >
> >   A float4 value is a 4-byte IEEE single-precision floating point
> >   number.  It is transmitted in network byte order, so you must
> >   convert to local byte order.  (C users can do this portably
> >   using the standard ntohl() function.)
> >
> > but I'm not sure an example is worth more than such a parenthetical
> > comment.  Perhaps others disagree, though.
> 
> I don't disagree with that.
> 
> I do think that when you suggested documenting this rather than just
> adding some examples, you moved the goalposts a long way. If we're
> going to add this to the documentation, it probably ought to cover
> every data type we ship. Overall, I think that would be a better
> result than just adding a few examples for the most common data types
> to testlibpq*.c, but it's also substantially more work. I do agree
> with you that if we're going to document this, as opposed to provide
> examples, then a narrative style is more appropriate than a whole
> bunch of small sample programs; maintaining working code in the
> documentation seems like an annoying amount of maintenance and is
> probably not the most efficient way to communicate useful information.

Yeah, I will try to cover all the data types we ship. :)  I'll keep at
it and drop the code examples.

Regards,
Mark
-- 
Mark Wong
EDB: http://www.enterprisedb.com




Re: real/float example for testlibpq3

2023-01-20 Thread Robert Haas
On Fri, Jan 20, 2023 at 12:58 PM Tom Lane  wrote:
> I don't mind if you write something like
>
>   A float4 value is a 4-byte IEEE single-precision floating point
>   number.  It is transmitted in network byte order, so you must
>   convert to local byte order.  (C users can do this portably
>   using the standard ntohl() function.)
>
> but I'm not sure an example is worth more than such a parenthetical
> comment.  Perhaps others disagree, though.

I don't disagree with that.

I do think that when you suggested documenting this rather than just
adding some examples, you moved the goalposts a long way. If we're
going to add this to the documentation, it probably ought to cover
every data type we ship. Overall, I think that would be a better
result than just adding a few examples for the most common data types
to testlibpq*.c, but it's also substantially more work. I do agree
with you that if we're going to document this, as opposed to provide
examples, then a narrative style is more appropriate than a whole
bunch of small sample programs; maintaining working code in the
documentation seems like an annoying amount of maintenance and is
probably not the most efficient way to communicate useful information.

-- 
Robert Haas
EDB: http://www.enterprisedb.com




Re: real/float example for testlibpq3

2023-01-20 Thread Tom Lane
Mark Wong  writes:
> On Thu, Nov 03, 2022 at 09:55:22AM -0400, Tom Lane wrote:
>> Perhaps a new chapter under "IV. Client Interfaces" is the right
>> place?
>> 
>> If we wanted to get aggressive, we could move most of the nitpicky details
>> about datatype text formatting (e.g., the array quoting rules) there too.
>> I'm not set on that, but it'd make datatype.sgml smaller which could
>> hardly be a bad thing.

> I suppose figuring out exactly where to put it and how to mark it up, 
> etc., in a repeatable fashion is part of the job here.

> How does this look?

> I've simply moved things around into a new "Binary Format" section with
> the few parts that I've started for some quick feedback about whether
> this is looking like the right landing place.

I took a quick look at this. The patch cannot be applied as given
because it seems to be editing binary-format.sgml from some prior
state, but that file doesn't exist at all yet.  However, there's
enough visible in the patch to comment on.

Personally I'm not excited about supplying fragments of C code
like this.  Those seem quite useless to non-C users, and I'm not
really sure that they'd be very helpful even for C users, because
you have to make a whole lot of assumptions about what the user
wants to do with the value.  I think what *would* be helpful is
just plain prose documentation of the on-the-wire binary format.

I don't mind if you write something like

  A float4 value is a 4-byte IEEE single-precision floating point
  number.  It is transmitted in network byte order, so you must
  convert to local byte order.  (C users can do this portably
  using the standard ntohl() function.)

but I'm not sure an example is worth more than such a parenthetical
comment.  Perhaps others disagree, though.

regards, tom lane




Re: real/float example for testlibpq3

2023-01-13 Thread Mark Wong
On Thu, Nov 03, 2022 at 09:55:22AM -0400, Tom Lane wrote:
> Peter Eisentraut  writes:
> > On 01.11.22 09:15, Tom Lane wrote:
> >> Agreed that the libpq manual is not the place for this, but I feel
> >> like it will also be clutter in "Data Types".  Perhaps we should
> >> invent a new appendix or the like?  Somewhere near the wire protocol
> >> docs seems sensible.
> 
> > Would that clutter the protocol docs? ;-)
> 
> I said "near", not "in".  At the time I was thinking "new appendix",
> but I now recall that the wire protocol docs are not an appendix
> but a chapter in the Internals division.  So that doesn't seem like
> quite the right place anyway.
> 
> Perhaps a new chapter under "IV. Client Interfaces" is the right
> place?
> 
> If we wanted to get aggressive, we could move most of the nitpicky details
> about datatype text formatting (e.g., the array quoting rules) there too.
> I'm not set on that, but it'd make datatype.sgml smaller which could
> hardly be a bad thing.
> 
> > I suppose figuring out exactly where to put it and how to mark it up, 
> > etc., in a repeatable fashion is part of the job here.
> 
> Yup.

How does this look?

I've simply moved things around into a new "Binary Format" section with
the few parts that I've started for some quick feedback about whether
this is looking like the right landing place.

Regards,
Mark
diff --git a/doc/src/sgml/binary-format.sgml b/doc/src/sgml/binary-format.sgml
index a297ece784..779b606ec9 100644
--- a/doc/src/sgml/binary-format.sgml
+++ b/doc/src/sgml/binary-format.sgml
@@ -6,9 +6,102 @@
  pgsql binary format
 
  
-  This chapter describes the binary format used in the wire protocol.  There
-  are a number of C examples for the data types used in PostgreSQL.  We will
-  try to be as comprehensive as possible with the native data types.
+  This chapter describes the binary representation of the native PostgreSQL
+  data types and gives examples on how to handle each data type's binary format
+  by offering C code examples for each data types.
  
 
+ 
+  We will try to cover all of the native data types...
+ 
+
+ 
+  boolean
+
+  
+   A boolean is transmitted as single byte that, when cast to an
+   int, will be 0 for
+   false and 1 for
+   true.
+  
+
+
+
+ 
+
+ 
+  real
+
+  
+   A real is composed of 4 bytes and needs to be handled correctly
+   for byte order.
+  
+
+
+
+
+ 
+
+ 
+  timestamp without time zone
+
+  
+   A timestamp without time zone is a 64-bit data type
+   representing the number of microseconds since January 1, 2000.  It can be
+   converted into a broken-down time representation by converting the time into
+   seconds and saving the microseconds elsewhere.
+  
+
+  
+   Note that in C time is counted from January 1, 1970, so this difference
+   needs to be accounted for in addition to handling the network byte order.
+  
+
+
+
+
+  
 
diff --git a/doc/src/sgml/filelist.sgml b/doc/src/sgml/filelist.sgml
index 0d6be9a2fa..688f947107 100644
--- a/doc/src/sgml/filelist.sgml
+++ b/doc/src/sgml/filelist.sgml
@@ -51,6 +51,7 @@
 
 
 
+
 
 
 
diff --git a/doc/src/sgml/postgres.sgml b/doc/src/sgml/postgres.sgml
index 2e271862fc..705b03f4aa 100644
--- a/doc/src/sgml/postgres.sgml
+++ b/doc/src/sgml/postgres.sgml
@@ -196,6 +196,7 @@ break is not needed in a wider output rendering.
   
   
   
+  
 
  
 


Re: real/float example for testlibpq3

2022-11-29 Thread Michael Paquier
On Wed, Nov 09, 2022 at 12:47:23PM -0800, Mark Wong wrote:
> I'll take a stab at adding a new chapter and share how that looks.

This thread has stalled for three weeks, so I have marked it as
returned with feedback.  Once you have a new patch, please feel free
to submit it.
--
Michael


signature.asc
Description: PGP signature


Re: real/float example for testlibpq3

2022-11-09 Thread Mark Wong
On Thu, Nov 03, 2022 at 09:55:22AM -0400, Tom Lane wrote:
> Peter Eisentraut  writes:
> > On 01.11.22 09:15, Tom Lane wrote:
> >> Agreed that the libpq manual is not the place for this, but I feel
> >> like it will also be clutter in "Data Types".  Perhaps we should
> >> invent a new appendix or the like?  Somewhere near the wire protocol
> >> docs seems sensible.
> 
> > Would that clutter the protocol docs? ;-)
> 
> I said "near", not "in".  At the time I was thinking "new appendix",
> but I now recall that the wire protocol docs are not an appendix
> but a chapter in the Internals division.  So that doesn't seem like
> quite the right place anyway.
> 
> Perhaps a new chapter under "IV. Client Interfaces" is the right
> place?
> 
> If we wanted to get aggressive, we could move most of the nitpicky details
> about datatype text formatting (e.g., the array quoting rules) there too.
> I'm not set on that, but it'd make datatype.sgml smaller which could
> hardly be a bad thing.
> 
> > I suppose figuring out exactly where to put it and how to mark it up, 
> > etc., in a repeatable fashion is part of the job here.
> 
> Yup.

I'll take a stab at adding a new chapter and share how that looks.

Regards,
Mark

--
Mark Wong
EDB https://enterprisedb.com




Re: real/float example for testlibpq3

2022-11-03 Thread Tom Lane
Peter Eisentraut  writes:
> On 01.11.22 09:15, Tom Lane wrote:
>> Agreed that the libpq manual is not the place for this, but I feel
>> like it will also be clutter in "Data Types".  Perhaps we should
>> invent a new appendix or the like?  Somewhere near the wire protocol
>> docs seems sensible.

> Would that clutter the protocol docs? ;-)

I said "near", not "in".  At the time I was thinking "new appendix",
but I now recall that the wire protocol docs are not an appendix
but a chapter in the Internals division.  So that doesn't seem like
quite the right place anyway.

Perhaps a new chapter under "IV. Client Interfaces" is the right
place?

If we wanted to get aggressive, we could move most of the nitpicky details
about datatype text formatting (e.g., the array quoting rules) there too.
I'm not set on that, but it'd make datatype.sgml smaller which could
hardly be a bad thing.

> I suppose figuring out exactly where to put it and how to mark it up, 
> etc., in a repeatable fashion is part of the job here.

Yup.

regards, tom lane




Re: real/float example for testlibpq3

2022-11-03 Thread Peter Eisentraut

On 01.11.22 09:15, Tom Lane wrote:

Peter Eisentraut  writes:

If we want to start documenting the binary format for all data types, it
should go into the "Data Types" chapter.  There, we already document the
text format in great detail, so putting the binary format in there as
well would make sense.


Agreed that the libpq manual is not the place for this, but I feel
like it will also be clutter in "Data Types".  Perhaps we should
invent a new appendix or the like?  Somewhere near the wire protocol
docs seems sensible.


Would that clutter the protocol docs? ;-)

I suppose figuring out exactly where to put it and how to mark it up, 
etc., in a repeatable fashion is part of the job here.






Re: real/float example for testlibpq3

2022-11-01 Thread Tom Lane
Peter Eisentraut  writes:
> If we want to start documenting the binary format for all data types, it 
> should go into the "Data Types" chapter.  There, we already document the 
> text format in great detail, so putting the binary format in there as 
> well would make sense.

Agreed that the libpq manual is not the place for this, but I feel
like it will also be clutter in "Data Types".  Perhaps we should
invent a new appendix or the like?  Somewhere near the wire protocol
docs seems sensible.

regards, tom lane




Re: real/float example for testlibpq3

2022-11-01 Thread Peter Eisentraut

On 01.07.22 19:18, Mark Wong wrote:

Just wanted to do another quick check in before I go too far.  How do
does this start look?  Extending the libpq section with a code snippet
and description per data type?


If we want to start documenting the binary format for all data types, it 
should go into the "Data Types" chapter.  There, we already document the 
text format in great detail, so putting the binary format in there as 
well would make sense.


The libpq chapter could then contain some examples of applying that 
information in the context of libpq.  But the libpq documentation 
shouldn't be the primary place where the formats are documented.






Re: real/float example for testlibpq3

2022-07-01 Thread Mark Wong
On Thu, Jun 16, 2022 at 03:41:50PM -0400, Tom Lane wrote:
> Robert Haas  writes:
> > On Tue, Jun 14, 2022 at 1:40 PM Mark Wong  wrote:
> >> I've created a function for each data type with the idea that an example
> >> for handling a specific data type can be more easily reviewed by looking
> >> in a single place.
> >> I've added examples for REAL, TIMESTAMP WITHOUT TIME ZONE, and BOOLEAN
> >> to try to illustrate how testlibpq3.sql and testlibpq3.c will grow if
> >> this is a good way to go.
> 
> > I'm not sure that we want to let these test programs grow very large.
> > In particular, I don't think we should let ourselves get sucked into
> > adding an example for every data type under the sun -- if that's
> > wanted, the solution is perhaps to add documentation for the binary
> > formats, not hide impromptu documentation inside a test program.  But
> > doing this much seems OK to me.
> 
> Yeah, "hiding impromptu documentation inside a test program" is what
> this looks like, and I'm not sure that's a reasonable way to go.
> 
> (1) Who's going to think to look in src/test/examples/testlibpq3.c for
> documentation of binary formats?
> 
> (2) The useful details are likely to get buried in notational and
> portability concerns, as I think your build failure illustrates.
> 
> (3) I bet few if any packagers install these files, so that the new
> info would be unavailable to many people.
> 
> I think some new appendix in the main SGML docs would be the appropriate
> place if we want to provide real documentation.

Just wanted to do another quick check in before I go too far.  How do
does this start look?  Extending the libpq section with a code snippet
and description per data type?

Regards,
Mark
diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 37ec3cb4e5..44c60223ee 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -8874,6 +8874,109 @@ int PQisthreadsafe();
   
  
 
+ 
+  Binary Format
+  
+Various libpq functions support sending or
+receiving data in binary format.  Data in binary format requires the
+application to be handle any differences between the system and the network
+byte order.
+  
+
+  
+ demonstrates how to write a complete
+program that uses libpq functions that request
+data in binary foramt.  This example shows how to handle only a few data
+types.  This section will detail the handling of as many of the data types
+as possible.  See  for descriptions of each of 
the
+built-in data types.
+  
+
+  
+   boolean
+
+   A boolean is transmitted as single byte that, when cast to
+   an int, will be 0 for
+   false and 1 for
+   true.
+
+
+
+
+  
+
+  
+   real
+
+  A real is composed of 4 bytes and needs to be handled
+  correctly for byte order.
+
+
+
+
+  
+
+  
+   timestamp without time zone
+
+  A timestamp without time zone is a 64-bit data type
+  representing the number of microseconds since January 1, 2000.  It can be
+  converted into a broken-down time representation by converting the time
+  into seconds and saving the microseconds elsewhere.
+
+
+  Note that in C time is counted from January 1, 1970 so this discrepency
+  needs to be accounted for in addition to handling the network byte order.
+
+
+
+
+  
+
+ 
 
  
   Building libpq Programs


Re: real/float example for testlibpq3

2022-06-16 Thread Mark Wong
On Thu, Jun 16, 2022 at 03:41:50PM -0400, Tom Lane wrote:
> Robert Haas  writes:
> > On Tue, Jun 14, 2022 at 1:40 PM Mark Wong  wrote:
> >> I've created a function for each data type with the idea that an example
> >> for handling a specific data type can be more easily reviewed by looking
> >> in a single place.
> >> I've added examples for REAL, TIMESTAMP WITHOUT TIME ZONE, and BOOLEAN
> >> to try to illustrate how testlibpq3.sql and testlibpq3.c will grow if
> >> this is a good way to go.
> 
> > I'm not sure that we want to let these test programs grow very large.
> > In particular, I don't think we should let ourselves get sucked into
> > adding an example for every data type under the sun -- if that's
> > wanted, the solution is perhaps to add documentation for the binary
> > formats, not hide impromptu documentation inside a test program.  But
> > doing this much seems OK to me.
> 
> Yeah, "hiding impromptu documentation inside a test program" is what
> this looks like, and I'm not sure that's a reasonable way to go.
> 
> (1) Who's going to think to look in src/test/examples/testlibpq3.c for
> documentation of binary formats?
> 
> (2) The useful details are likely to get buried in notational and
> portability concerns, as I think your build failure illustrates.
> 
> (3) I bet few if any packagers install these files, so that the new
> info would be unavailable to many people.
> 
> I think some new appendix in the main SGML docs would be the appropriate
> place if we want to provide real documentation.

Ok, I'll leave the testlibpq3.c as it was then.  If it's worth keeping
any of those changes, then I can remove the timestamp example because of
the ntohll() portability since that is trivial.

I'll start a new appendix and share again when I have something to show.

Regards,
Mark

--
Mark Wong
EDB: http://www.enterprisedb.com




Re: real/float example for testlibpq3

2022-06-16 Thread Tom Lane
Robert Haas  writes:
> On Tue, Jun 14, 2022 at 1:40 PM Mark Wong  wrote:
>> I've created a function for each data type with the idea that an example
>> for handling a specific data type can be more easily reviewed by looking
>> in a single place.
>> I've added examples for REAL, TIMESTAMP WITHOUT TIME ZONE, and BOOLEAN
>> to try to illustrate how testlibpq3.sql and testlibpq3.c will grow if
>> this is a good way to go.

> I'm not sure that we want to let these test programs grow very large.
> In particular, I don't think we should let ourselves get sucked into
> adding an example for every data type under the sun -- if that's
> wanted, the solution is perhaps to add documentation for the binary
> formats, not hide impromptu documentation inside a test program.  But
> doing this much seems OK to me.

Yeah, "hiding impromptu documentation inside a test program" is what
this looks like, and I'm not sure that's a reasonable way to go.

(1) Who's going to think to look in src/test/examples/testlibpq3.c for
documentation of binary formats?

(2) The useful details are likely to get buried in notational and
portability concerns, as I think your build failure illustrates.

(3) I bet few if any packagers install these files, so that the new
info would be unavailable to many people.

I think some new appendix in the main SGML docs would be the appropriate
place if we want to provide real documentation.

regards, tom lane




Re: real/float example for testlibpq3

2022-06-16 Thread Robert Haas
On Tue, Jun 14, 2022 at 1:40 PM Mark Wong  wrote:
> Checking in for quick feedback to see if this refactor makes sense.
>
> I've created a function for each data type with the idea that an example
> for handling a specific data type can be more easily reviewed by looking
> in a single place.
>
> I've added examples for REAL, TIMESTAMP WITHOUT TIME ZONE, and BOOLEAN
> to try to illustrate how testlibpq3.sql and testlibpq3.c will grow if
> this is a good way to go.

I think this could be a reasonable kind of thing to do, but I think
you left the "ts" output out of the example output in the comments,
and also your code's apparently not portable, because my compiler is
OK with testlibpq3 right now, but with your patch it emits lengthy
unhappy moaning, starting with:

testlibpq3.c:88:10: error: expected ')'
uint64_t ntohll(uint64_t);
 ^
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/sys/_endian.h:140:25:
note: expanded from macro 'ntohll'
#define ntohll(x)   __DARWIN_OSSwapInt64(x)
^

Apparently macOS defines ntohll as a macro, and it's not amused by
your attempt to include a function prototype for it.

I'm not sure that we want to let these test programs grow very large.
In particular, I don't think we should let ourselves get sucked into
adding an example for every data type under the sun -- if that's
wanted, the solution is perhaps to add documentation for the binary
formats, not hide impromptu documentation inside a test program.  But
doing this much seems OK to me.

-- 
Robert Haas
EDB: http://www.enterprisedb.com




Re: real/float example for testlibpq3

2022-06-14 Thread Mark Wong
On Wed, Mar 30, 2022 at 01:16:37PM -0400, Greg Stark wrote:
> On Mon, 28 Feb 2022 at 17:50, Tom Lane  wrote:
> >
> > Chapman Flack  writes:
> > > In the current state of affairs, what's considered the ur-source of that
> > > information?
> >
> > The source code for the type's send/receive functions :-(.  One could
> > wish for something better, but no one has stepped up to produce such
> > documentation.
> 
> Fwiw the client library I heard of attempting to have good binary mode
> support was the Crystal language client
> https://github.com/will/crystal-pg. I think he was aiming for full
> coverage of the built-in data types. That might make a good reference
> implementation to write up documentation from. He probably uncovered
> some corner cases in development that one might not find from just
> inspection of the server code.

Checking in for quick feedback to see if this refactor makes sense.

I've created a function for each data type with the idea that an example
for handling a specific data type can be more easily reviewed by looking
in a single place.

I've added examples for REAL, TIMESTAMP WITHOUT TIME ZONE, and BOOLEAN
to try to illustrate how testlibpq3.sql and testlibpq3.c will grow if
this is a good way to go.

Regards,
Mark
diff --git a/src/test/examples/testlibpq3.c b/src/test/examples/testlibpq3.c
index 4f7b791388..68972f17f3 100644
--- a/src/test/examples/testlibpq3.c
+++ b/src/test/examples/testlibpq3.c
@@ -11,21 +11,56 @@
  * CREATE SCHEMA testlibpq3;
  * SET search_path = testlibpq3;
  * SET standard_conforming_strings = ON;
- * CREATE TABLE test1 (i int4, t text, b bytea);
- * INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004');
- * INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000');
+ * CREATE SCHEMA testlibpq3;
+ * SET search_path = testlibpq3;
+ * SET standard_conforming_strings = ON;
+ * CREATE TABLE test1 (
+ * i int4
+ *   , r real
+ *   , bo boolean
+ *   , ts timestamp
+ *   , t text
+ *   , b bytea
+ * );
+ * INSERT INTO test1
+ * VALUES (
+ * 1
+ *   , 3.141593
+ *   , true
+ *   , '2000-01-01 00:00:02.414213'
+ *   , 'joe''s place'
+ *   , '\000\001\002\003\004'
+ * );
+ * INSERT INTO test1
+ * VALUES (
+ * 2
+ *   , 1.618033
+ *   , false
+ *   , '2000-01-01 00:00:01.465571'
+ *   , 'ho there'
+ *   , '\004\003\002\001\000'
+ * );
  *
  * The expected output is:
  *
  * tuple 0: got
  * i = (4 bytes) 1
+ * r = (4 bytes) 3.141593
+ * bo = (1 bytes) 1
  * t = (11 bytes) 'joe's place'
  * b = (5 bytes) \000\001\002\003\004
  *
  * tuple 0: got
  * i = (4 bytes) 2
+ * r = (4 bytes) 1.618033
+ * bo = (1 bytes) 0
  * t = (8 bytes) 'ho there'
  * b = (5 bytes) \004\003\002\001\000
+ *
+ * General notes about this example:
+ *
+ * Use PQfnumber to avoid assumptions about field order in result but when
+ * getting the field values we ignore possibility they are null!
  */
 
 #ifdef WIN32
@@ -36,6 +71,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include "libpq-fe.h"
 
@@ -44,6 +80,13 @@
 #include 
 
 
+/* These macros hopefully make reading calculations for timestamps easier. */
+#define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */
+#define UNIX_EPOCH_JDATE 2440588 /* == date2j(1970, 1, 1) */
+#define SECS_PER_DAY 86400
+
+uint64_t ntohll(uint64_t);
+
 static void
 exit_nicely(PGconn *conn)
 {
@@ -51,6 +94,142 @@ exit_nicely(PGconn *conn)
exit(1);
 }
 
+static void
+handle_boolean(PGresult *res, int i)
+{
+   int fnum;
+   char   *ptr;
+   int val;
+
+   fnum = PQfnumber(res, "bo");
+   ptr = PQgetvalue(res, i, fnum);
+   val = (int) *ptr;
+   printf(" bo = (%d bytes) %d\n", PQgetlength(res, i, fnum), val);
+}
+
+static void
+handle_bytea(PGresult *res, int i)
+{
+   int j;
+   int fnum;
+   char   *ptr;
+   int len;
+
+   fnum = PQfnumber(res, "b");
+   ptr = PQgetvalue(res, i, fnum);
+
+   /*
+* The binary representation of BYTEA is a bunch of bytes, which could
+* include embedded nulls so we have to pay attention to field length.
+*/
+   len = PQgetlength(res, i, fnum);
+   printf(" b = (%d bytes) ", len);
+   for (j = 0; j < len; j++) printf("\\%03o", ptr[j]);
+}
+
+static void
+handle_integer(PGresult *res, int i)
+{
+   int fnum;
+   char   *ptr;
+   int val;
+
+   fnum = PQfnumber(res, "i");
+   ptr = PQgetvalue(res, i, fnum);
+
+   /*
+* The binary representation of INT4 is in network byte order, which
+* we'd better coerce to the local byte order.
+*/
+   val = ntohl(*((uint32_t *) ptr));
+
+   printf(" i = (%d bytes) %d\n", PQgetlength(res, i, fnum), val);
+}
+
+static void
+handle_real(PGresult *res, int i)
+{
+   int fnum;
+   

Re: real/float example for testlibpq3

2022-03-30 Thread Greg Stark
On Mon, 28 Feb 2022 at 17:50, Tom Lane  wrote:
>
> Chapman Flack  writes:
> > In the current state of affairs, what's considered the ur-source of that
> > information?
>
> The source code for the type's send/receive functions :-(.  One could
> wish for something better, but no one has stepped up to produce such
> documentation.

Fwiw the client library I heard of attempting to have good binary mode
support was the Crystal language client
https://github.com/will/crystal-pg. I think he was aiming for full
coverage of the built-in data types. That might make a good reference
implementation to write up documentation from. He probably uncovered
some corner cases in development that one might not find from just
inspection of the server code.

-- 
greg




Re: real/float example for testlibpq3

2022-02-28 Thread Tom Lane
Chapman Flack  writes:
> This stimulates a question for me.

> Not just libpq, but all drivers implemented in whatever language, if they
> wish to support binary protocol, depend on knowing what the committed
> send/recv wire formats are for whichever types they mean to support.

> In the current state of affairs, what's considered the ur-source of that
> information?

The source code for the type's send/receive functions :-(.  One could
wish for something better, but no one has stepped up to produce such
documentation.

regards, tom lane




Re: real/float example for testlibpq3

2022-02-28 Thread Chapman Flack
On 02/28/22 10:19, Tom Lane wrote:
>> That will standardize the
>> way to fetch real typed values in libpq. That leads to the next
>> question. Do we need to introduce different PQget*value() for standard
>> C/SQL data types.
> 
> ... I do not want to go here.  Where would you stop?  How would you
> deal with cross-machine inconsistencies in integer widths?

This stimulates a question for me.

Not just libpq, but all drivers implemented in whatever language, if they
wish to support binary protocol, depend on knowing what the committed
send/recv wire formats are for whichever types they mean to support.

In the current state of affairs, what's considered the ur-source of that
information?

I have often seen those formats documented in code comments, usually above
the recv function in the .c file for a given adt.

Have we got any more, well, machine-readable collection of that knowledge?

Regards,
-Chap




Re: real/float example for testlibpq3

2022-02-28 Thread Tom Lane
Ashutosh Bapat  writes:
> I am wondering whether we should provide PQgetfloatvalue() which does
> what that example shows but transparently. It will return a float
> value of the given field instead of char *.

I'm against that, precisely because ...

> That will standardize the
> way to fetch real typed values in libpq. That leads to the next
> question. Do we need to introduce different PQget*value() for standard
> C/SQL data types.

... I do not want to go here.  Where would you stop?  How would you
deal with cross-machine inconsistencies in integer widths?

regards, tom lane




Re: real/float example for testlibpq3

2022-02-28 Thread Ashutosh Bapat
Hi Mark,
Fetching a "real" type field from libpq doesn't look that intuitive.
So this example is super helpful. Thanks.

I am wondering whether we should provide PQgetfloatvalue() which does
what that example shows but transparently. It will return a float
value of the given field instead of char *. That will standardize the
way to fetch real typed values in libpq. That leads to the next
question. Do we need to introduce different PQget*value() for standard
C/SQL data types.

-- 
Best Wishes,
Ashutosh Bapat

On Fri, Feb 25, 2022 at 3:12 AM Mark Wong  wrote:
>
> Hi everyone,
>
> Would adding additional examples to testlibpq3.c on handling more data
> types be helpful?  I've attached a patch adding how to handle a REAL to
> current example.
>
> Regards,
> Mark