On Thu, Dec 5, 2024 at 12:16 PM Tom Lane <t...@sss.pgh.pa.us> wrote:
> Now you already snuck the camel's nose under the
> tent by including stdint.h there, and maybe these additional headers
> wouldn't do any further damage.

Even though we fixed the immediate issue (thanks), this comment stayed
with me.  I did that because I didn't want to change any interfaces at
the same time as the <stdint.h> retrofit, but I agree that it feels a
bit odd hidden in there, and doesn't appear to conform to
postgres_ext.h's self-description.  Stepping back, and I realise it's
difficult to answer with certainty, I wonder why anyone would ever
want to use postgres_ext.h directly for the definition of pg_int64
*without* being a user of libpq-fe.h.  I can't find any references to
pg_int64 (excluding forks of our code) on github; there are a few
things like proxies and suchlike that include postgres_ext.h for other
things, mostly bogusly (they also include libpq-fe.h, or they say they
want NAMEDATALEN, which isn't there anymore).

We have just three lo_*64() functions using that type and then
pg_usec_time_t.  Seems like a very narrow usage that hasn't spread,
likely only used to receive arguments, and really quite specific to
libpq-fe.h and not one of the "fundamental Postgres declarations".
Maybe we should consider moving #include <stdint.h> into libpq-fe.h?

And if we included <stdint.h> overtly, rather than covertly in
postgres_ext.h, why would we still want a third name for int64_t?  We
could change the three lo_*64() declarations to use the standard type
directly, but keep the historical typedef marked deprecated.

> But I don't see a strong argument to
> change long-standing external APIs any more than we absolutely must.

So perhaps you'll hate that idea then.  I wonder if you'll hate it
more than keeping the #include in postgres_ext.h, hence putting the
idea forward!
From 1fddfe14a1f145b23fa5c31aa1cf6afc38d074d4 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.mu...@gmail.com>
Date: Sat, 7 Dec 2024 15:55:44 +1300
Subject: [PATCH] Deprecate pg_int64.

Previously we used pg_int64 in the function prototypes of libpq-fe.h.
It was a typedef for the type that we selected for int64 in the backend.
That type is now int64_t, since commit 962da900.

Given that it didn't match the name we use in the backend anyway, let's
just use int64_t directly in public interfaces.  It is likely to be the
name that modern client applications would use for their own 64-bit
processing anyway.

Keep a typedef marked deprecated for backward compatibility, but move it
to libpq-fe.h, where the only uses of it were.
---
 doc/src/sgml/libpq.sgml         |  2 +-
 doc/src/sgml/lobj.sgml          |  6 +++---
 src/include/postgres_ext.h      |  5 -----
 src/interfaces/libpq/fe-lobj.c  | 30 +++++++++++++++---------------
 src/interfaces/libpq/libpq-fe.h | 12 ++++++++----
 src/test/examples/testlo64.c    |  7 ++++---
 6 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 01f259fd0dc..fff9dd898d0 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -522,7 +522,7 @@ switch(PQstatus(conn))
        sequence described in the documentation of
        <xref linkend="libpq-PQconnectStartParams"/>.
 <synopsis>
-typedef pg_int64 pg_usec_time_t;
+typedef int64_t pg_usec_time_t;
 
 int PQsocketPoll(int sock, int forRead, int forWrite,
                  pg_usec_time_t end_time);
diff --git a/doc/src/sgml/lobj.sgml b/doc/src/sgml/lobj.sgml
index 2b725c3c182..1783f530901 100644
--- a/doc/src/sgml/lobj.sgml
+++ b/doc/src/sgml/lobj.sgml
@@ -398,7 +398,7 @@ int lo_lseek(PGconn *conn, int fd, int offset, int whence);
      When dealing with large objects that might exceed 2GB in size,
      instead use
 <synopsis>
-pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
+int64_t lo_lseek64(PGconn *conn, int fd, int64_t offset, int whence);
 </synopsis>
      This function has the same behavior
      as <function>lo_lseek</function>, but it can accept an
@@ -434,7 +434,7 @@ int lo_tell(PGconn *conn, int fd);
      When dealing with large objects that might exceed 2GB in size,
      instead use
 <synopsis>
-pg_int64 lo_tell64(PGconn *conn, int fd);
+int64_t lo_tell64(PGconn *conn, int fd);
 </synopsis>
      This function has the same behavior
      as <function>lo_tell</function>, but it can deliver a result larger
@@ -485,7 +485,7 @@ int lo_truncate(PGconn *conn, int fd, size_t len);
      When dealing with large objects that might exceed 2GB in size,
      instead use
 <synopsis>
-int lo_truncate64(PGconn *conn, int fd, pg_int64 len);
+int lo_truncate64(PGconn *conn, int fd, int64_t len);
 </synopsis>
      This function has the same
      behavior as <function>lo_truncate</function>, but it can accept a
diff --git a/src/include/postgres_ext.h b/src/include/postgres_ext.h
index 202eb049622..473f9ba0ebc 100644
--- a/src/include/postgres_ext.h
+++ b/src/include/postgres_ext.h
@@ -23,8 +23,6 @@
 #ifndef POSTGRES_EXT_H
 #define POSTGRES_EXT_H
 
-#include <stdint.h>
-
 /*
  * Object ID is a fundamental type in Postgres.
  */
@@ -43,9 +41,6 @@ typedef unsigned int Oid;
 /* the above needs <stdlib.h> */
 
 
-/* Define a signed 64-bit integer type for use in client API declarations. */
-typedef int64_t pg_int64;
-
 /*
  * Identifiers of error message fields.  Kept here to keep common
  * between frontend and backend, and also to export them to libpq
diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c
index c17585e38b7..95a1e54290a 100644
--- a/src/interfaces/libpq/fe-lobj.c
+++ b/src/interfaces/libpq/fe-lobj.c
@@ -43,8 +43,8 @@
 
 static int	lo_initialize(PGconn *conn);
 static Oid	lo_import_internal(PGconn *conn, const char *filename, Oid oid);
-static pg_int64 lo_hton64(pg_int64 host64);
-static pg_int64 lo_ntoh64(pg_int64 net64);
+static int64_t lo_hton64(int64_t host64);
+static int64_t lo_ntoh64(int64_t net64);
 
 /*
  * lo_open
@@ -192,7 +192,7 @@ lo_truncate(PGconn *conn, int fd, size_t len)
  * returns -1 upon failure
  */
 int
-lo_truncate64(PGconn *conn, int fd, pg_int64 len)
+lo_truncate64(PGconn *conn, int fd, int64_t len)
 {
 	PQArgBlock	argv[2];
 	PGresult   *res;
@@ -381,12 +381,12 @@ lo_lseek(PGconn *conn, int fd, int offset, int whence)
  * lo_lseek64
  *	  change the current read or write location on a large object
  */
-pg_int64
-lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence)
+int64_t
+lo_lseek64(PGconn *conn, int fd, int64_t offset, int whence)
 {
 	PQArgBlock	argv[3];
 	PGresult   *res;
-	pg_int64	retval;
+	int64		retval;
 	int			result_len;
 
 	if (lo_initialize(conn) < 0)
@@ -544,10 +544,10 @@ lo_tell(PGconn *conn, int fd)
  * lo_tell64
  *	  returns the current seek location of the large object
  */
-pg_int64
+int64_t
 lo_tell64(PGconn *conn, int fd)
 {
-	pg_int64	retval;
+	int64		retval;
 	PQArgBlock	argv[1];
 	PGresult   *res;
 	int			result_len;
@@ -1019,12 +1019,12 @@ lo_initialize(PGconn *conn)
  * lo_hton64
  *	  converts a 64-bit integer from host byte order to network byte order
  */
-static pg_int64
-lo_hton64(pg_int64 host64)
+static int64_t
+lo_hton64(int64_t host64)
 {
 	union
 	{
-		pg_int64	i64;
+		int64		i64;
 		uint32		i32[2];
 	}			swap;
 	uint32		t;
@@ -1044,15 +1044,15 @@ lo_hton64(pg_int64 host64)
  * lo_ntoh64
  *	  converts a 64-bit integer from network byte order to host byte order
  */
-static pg_int64
-lo_ntoh64(pg_int64 net64)
+static int64_t
+lo_ntoh64(int64_t net64)
 {
 	union
 	{
-		pg_int64	i64;
+		int64		i64;
 		uint32		i32[2];
 	}			swap;
-	pg_int64	result;
+	int64		result;
 
 	swap.i64 = net64;
 
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index 15012c770c4..b598f3b80c1 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -20,6 +20,7 @@ extern "C"
 {
 #endif
 
+#include <stdint.h>
 #include <stdio.h>
 
 /*
@@ -222,8 +223,11 @@ typedef struct pgNotify
 	struct pgNotify *next;		/* list link */
 } PGnotify;
 
+/* deprecated name for int64_t */
+typedef int64_t pg_int64;
+
 /* pg_usec_time_t is like time_t, but with microsecond resolution */
-typedef pg_int64 pg_usec_time_t;
+typedef int64_t pg_usec_time_t;
 
 /* Function types for notice-handling callbacks */
 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
@@ -679,13 +683,13 @@ extern int	lo_close(PGconn *conn, int fd);
 extern int	lo_read(PGconn *conn, int fd, char *buf, size_t len);
 extern int	lo_write(PGconn *conn, int fd, const char *buf, size_t len);
 extern int	lo_lseek(PGconn *conn, int fd, int offset, int whence);
-extern pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
+extern int64_t lo_lseek64(PGconn *conn, int fd, int64_t offset, int whence);
 extern Oid	lo_creat(PGconn *conn, int mode);
 extern Oid	lo_create(PGconn *conn, Oid lobjId);
 extern int	lo_tell(PGconn *conn, int fd);
-extern pg_int64 lo_tell64(PGconn *conn, int fd);
+extern int64_t lo_tell64(PGconn *conn, int fd);
 extern int	lo_truncate(PGconn *conn, int fd, size_t len);
-extern int	lo_truncate64(PGconn *conn, int fd, pg_int64 len);
+extern int	lo_truncate64(PGconn *conn, int fd, int64_t len);
 extern int	lo_unlink(PGconn *conn, Oid lobjId);
 extern Oid	lo_import(PGconn *conn, const char *filename);
 extern Oid	lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
diff --git a/src/test/examples/testlo64.c b/src/test/examples/testlo64.c
index f0636a47961..6f8d96cb304 100644
--- a/src/test/examples/testlo64.c
+++ b/src/test/examples/testlo64.c
@@ -12,6 +12,7 @@
  *
  *-------------------------------------------------------------------------
  */
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -75,7 +76,7 @@ importFile(PGconn *conn, char *filename)
 }
 
 static void
-pickout(PGconn *conn, Oid lobjId, pg_int64 start, int len)
+pickout(PGconn *conn, Oid lobjId, int64_t start, int len)
 {
 	int			lobj_fd;
 	char	   *buf;
@@ -110,7 +111,7 @@ pickout(PGconn *conn, Oid lobjId, pg_int64 start, int len)
 }
 
 static void
-overwrite(PGconn *conn, Oid lobjId, pg_int64 start, int len)
+overwrite(PGconn *conn, Oid lobjId, int64_t start, int len)
 {
 	int			lobj_fd;
 	char	   *buf;
@@ -148,7 +149,7 @@ overwrite(PGconn *conn, Oid lobjId, pg_int64 start, int len)
 }
 
 static void
-my_truncate(PGconn *conn, Oid lobjId, pg_int64 len)
+my_truncate(PGconn *conn, Oid lobjId, int64_t len)
 {
 	int			lobj_fd;
 
-- 
2.47.0

Reply via email to